Nameless Site

But one day, you will stand before its decrepit gate,without really knowing why.

0%

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:

  1. Choose any one of the 16 pieces.
  2. 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).

imgConsider 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

1
2
3
4
bwwb
bbwb
bwwb
bwww

Sample Output

1
4

DFS

递归遍历所有情况,即从当前点出发,翻还是不翻,然后进入下一层递归,直到满足条件为止。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;

public class Main {
static int ans = Integer.MAX_VALUE;
static int [][] matrix = new int[4][4];

public static boolean judge(){
int first = matrix[0][0];
for(int i = 0; i < 4; i++)
for(int j = 0 ; j < 4 ;j++)
{
if(matrix[i][j] != first)
return false;
}
return true;
}

public static void change(int x,int y){
matrix[x][y] = matrix[x][y] ^ 1;
if(x - 1 >= 0)
matrix[x - 1][y] ^= 1;
if(y - 1 >= 0)
matrix[x][y - 1] ^= 1;
if(x + 1 < 4)
matrix[x + 1][y] ^= 1;
if(y + 1 < 4)
matrix[x][y + 1] ^= 1;
}


public static void dfs(int x,int y,int times){
if(judge()){
if(ans > times)
{
ans = times;
}
return ;
}
if(x >= 4 || y >= 4)
return ;
int aX,aY;
aX = (x + 1) % 4;
aY = y + (x + 1) / 4;
dfs(aX,aY,times);
change(x, y);
dfs(aX,aY,times + 1);
change(x, y);
return ;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String temp;
for(int i = 0 ;i < 4; i ++) {
temp = in.nextLine();
for (int j = 0; j < 4; j++) {
if (temp.charAt(j) == 'b') {
matrix[i][j] = 1;
}
//定义黑为1,白为0
else
matrix[i][j] = 0;
}
}

dfs(0,0,0);
if(ans == Integer.MAX_VALUE)
System.out.println("Impossible");
else System.out.println(ans);
}
}

BFS+位压缩

来源于blog

主要思想:

1、如果用一个4*4的数组存储每一种状态,不但存储空间很大,而且在穷举状态时也不方便记录。因为每一颗棋子都只有两种状态,所以可以用二进制0和1表示每一个棋子的状态,则棋盘的状态就可以用一个16位的整数唯一标识。而翻转的操作也可以通过通过位操作来完成。显然当棋盘状态id为0(全白)或65535(全黑)时,游戏结束。

2、对于棋盘的每一个状态,都有十六种操作,首先要判断这十六种操作之后是否有完成的情况,如果没有,则再对这十六种操作的结果分别再进行上述操作,显然这里就要用到队列来存储了。而且在翻转的过程中有可能会回到之前的某种状态,而这种重复的状态是不应该再次入队的,所以维护 Visit[i]数组来判断 id==i 的状态之前是否已经出现过,如果不是才将其入队。如果游戏无法完成,状态必定会形成循环,由于重复状态不会再次入队,所以最后的队列一定会是空队列。

3、由于0^1=1,1^1=0,所以翻转的操作可以通过异或操作来完成,而翻转的位置可以通过移位来确定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
#include<memory.h>

struct Node
{
int state;
int step;
};

bool visit[65536];

int change[16] = //16种状态转换,对应4*4的翻子位置
{
51200,58368,29184,12544,
35968,20032,10016,4880,
2248,1252,626,305,
140,78,39,19
};

int bfs(int state)
{
int i;
memset(visit,false,sizeof(visit)); //标记每一个状态都未访问过
queue<Node>q;
Node cur,next;
cur.state = state;
cur.step = 0;
q.push(cur);
visit[state] = true;

while(!q.empty())
{
cur = q.front();
q.pop();
if(cur.state == 0 || cur.state == 0xffff) //65535
return cur.step;
for(i=0;i<16;i++)
{
next.state = cur.state^change[i];
next.step = cur.step + 1;
if(visit[next.state])
continue;
if(next.state == 0 || next.state == 0xffff) //65535
return next.step;
visit[next.state] = true;
q.push(next);
}
}
return -1;
}

int main(void)
{
int i,j,state,ans;
char ch[5][5];
while(scanf("%s",ch[0])!=EOF)
{
for(i = 1 ; i < 4 ; ++i)
scanf("%s",ch[i]);
state = 0;
//初始化棋盘状态
for(i = 0 ; i < 4 ; ++i)
{
for(j = 0 ; j < 4 ; ++j)
{
state <<= 1;
if(ch[i][j] == 'b')
state += 1;
//state ^= (1<<((3-i)*4+(3-j)));
}
}
ans = bfs(state);
if(ans == -1)
puts("Impossible");
else
printf("%d\n",ans);
}
return 0;
}

DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
代码二:DFS+Bit
本题由于要输出每次翻转的棋子,因此不适宜用BFS,应该使用DFS输出完整路径
*/
#include<iostream>
#include<cstdio>
using namespace std;

int chess; //棋盘状态
int step;
bool flag=false;
int ri[16],cj[16];

bool isopen(void)
{
if(chess == 0xFFFF)
return true;
else
return false;
}

void flip(int bit)
{
chess=chess^(0x1<<bit); //对翻转位取反
int row=bit/4;
int col=bit%4;
for(int c=0;c<4;c++)
chess=chess^(0x1<<(row*4+c)); //对全行取反
for(int r=0;r<4;r++)
chess=chess^(0x1<<(r*4+col)); //对全列取反
return;
}

void dfs(int bit,int deep)
{
if(deep==step)
{
flag=isopen();
return;
}

if(flag || bit>15)
return;

int row=ri[deep]=bit/4;
int col=cj[deep]=bit%4;

flip(bit);
if(col<4)
dfs(bit+1,deep+1);
else
dfs((bit+4)/4*4,deep+1);

flip(bit);
if(col<4)
dfs(bit+1,deep);
else
dfs((bit+4)/4*4,deep);
return;
}

int main(void)
{
char temp;
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cin>>temp;
if(temp=='-')
chess=chess^(1<<(i*4+j));
}
}

/*DFS*/
for(step=0;step<=16;step++)
{
dfs(0,0);
if(flag)
break;
}

printf("%d\n",step);
for(i=0;i<step;i++)
printf("%d %d\n",ri[i]+1,cj[i]+1);
return 0;
}