首页 > 代码库 > Flip Game (poj 1753)枚举+二进制
Flip Game (poj 1753)枚举+二进制
Flip Game
Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it‘s black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
Consider the following position as an example: bwbw wwww bbwb bwwb Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become: bwbw bwww wwwb wwwb The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal. Input The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position. Output Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it‘s impossible to achieve the goal, then write the word "Impossible" (without quotes). Sample Input bwwb bbwb bwwb bwww Sample Output 4 Source Northeastern Europe 2000 题意:有一个4*4的方格,每个方格中放一粒棋子,这个棋子一面是白色,一面是黑色。游戏规则为每次任选16颗中的一颗,把选中的这颗以及它四周的棋子一并反过来,当所有的棋子都是同一个颜色朝上时,游戏就完成了。现在给定一个初始状态,要求输出能够完成游戏所需翻转的最小次数,如果初始状态已经达到要求输出0。如果不可能完成游戏,输出Impossible。 思路:因为方格只有4*4,直接枚举所有状态,用队列实现: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 typedef long long ll; using namespace std; struct node { int x; int step; }; queue<node>Q; int visit[70000]; //标记状态,是否出现过 int main() { int id; char ch; id=0; memset(visit,0,sizeof(visit)); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { scanf("%c",&ch); if (ch=='b') id=id^(1<<(4*i+j)); } getchar(); } if (id==0||id==65535) { printf("0\n"); return 0; } while (!Q.empty()) Q.pop(); node st,now; st.x=id; visit[id]=1; st.step=0; Q.push(st); while (!Q.empty()) { st=Q.front(); Q.pop(); if (st.x==0||st.x==65535) { printf("%d\n",st.step); return 0; } for (int i=0;i<4;i++) for (int j=0;j<4;j++) { now.x=st.x^(1<<(4*i+j)); if (i>0) now.x=now.x^(1<<(4*(i-1)+j));//上 if (i<3) now.x=now.x^(1<<(4*(i+1)+j));//下 if (j>0) now.x=now.x^(1<<(4*i+j-1));//左 if (j<3) now.x=now.x^(1<<(4*i+j+1));//右 if (!visit[now.x]) { now.step=st.step+1; Q.push(now); visit[now.x]=1; } } } printf("Impossible\n"); return 0; } /* bwwb bbwb bwwb bwww */ |
Flip Game (poj 1753)枚举+二进制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。