Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 43451 | Accepted: 18737 |
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.
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
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
传送门:poj 1753
思路:首先,每一个棋子只有黑白两种状态,就很自然地想到可以用二进制的0,1 表示,而且棋盘是 4 乘 4的,就可以用一个16位二进制的数来表示棋盘的所有状态,即状态压缩。而且每一次只能翻16个棋子当中的一个,就是有16种不同的操作,对应位置上的二进制数跟1异或一下就可以取反了,我们可以先把这16个操作的数现打表出来,详情见
点击打开链接这位大神讲的比较详细,,然后就是用广度优先搜索依次求状态,并用一个visited数组来维护某状态是否已经被遍历过。
/*
*Li Wenjun
*Email:1542113545@qq.com
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <cctype>
#include <stack>
#include <list>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
#define debug 0
struct node{
int state;
int step;
};
bool visited[0Xffff];
int change[16]=
{
51200,58368,29184,12544,
35968,20032,10016,4880,
2248,1252,626,305,
140,78,39,19
};
int bfs(int start)
{
node cur,next;
queue<node>q;
cur.state = start;
cur.step = 0;
visited[cur.state] = true;
q.push(cur);
if(cur.state == 0|| cur.state ==0Xffff)
return cur.step;
while(!q.empty())
{
cur = q.front();
q.pop();
//这里的判断应该可以省略
for(int i=0;i<16;i++)
{
next.state = cur.state^change[i];
next.step = cur.step+1;
if(next.state == 0||next.state == 0Xffff)
return next.step;
if(visited[next.state])
continue;
visited[next.state] = true;
q.push(next);
}
}
return -1;
}
int main()
{
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
cin.tie(0);
cin.sync_with_stdio(false);
char a[4][4];
while(cin >> a[0])
{
for(int i=1;i<4;i++) cin >> a[i];
int start =0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i][j] == 'b')
start += 1<<((3-i)*4+(3-j));
}
}
memset(visited,false,sizeof(visited));
int ans = bfs(start);
if(ans == -1)
cout << "Impossible" << endl;
else
cout << ans << endl;
}
return 0;
}