hdu-2209 翻纸牌游戏
生活随笔
收集整理的這篇文章主要介紹了
hdu-2209 翻纸牌游戏
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=2209?
題解:把所有的狀態(tài)看成01二進制,狀態(tài)壓縮,比較簡單的bfs+狀態(tài)壓縮
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace std; #define M 1<<20+1 bool vis[M]; int l; struct node{int st,move; }; int bfs(int res){memset(vis,false,sizeof(vis));node p,q;queue<node> Q;p.move = 0;p.st = res;Q.push(p);while(!Q.empty()){q = Q.front();Q.pop();if(!q.st) return q.move;for(int i = 0;i < l;i++){p = q;if(!i) p.st = p.st ^ 3;//翻轉(zhuǎn)第一個,只影響第二個else p.st = p.st ^ (7 <<(i-1));//中間翻轉(zhuǎn)p.st &= (1<<l) - 1;//防溢出,不影響其結果if(vis[p.st]) continue;vis[p.st] = true;p.move = p.move + 1;Q.push(p);}}return -1; } int main(){char str[30];while(~scanf("%s",str)){l = strlen(str);int sum = 0;for(int i = 0;i < l;i++)sum = sum * 2 + str[i] - '0';int ans = bfs(sum);if(ans != -1) cout << ans << endl;else cout << "NO" << endl;}return 0; }總結
以上是生活随笔為你收集整理的hdu-2209 翻纸牌游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里涉江:你真的会学习吗?从结构化思维说
- 下一篇: Mobvista首席架构师蔡超:工作感悟