-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflipgame.cpp
81 lines (69 loc) · 1.1 KB
/
flipgame.cpp
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
#include<stdio.h>
const int inf=9999;
char s[5];
int map[4][4],i,j;
int ans=inf;
int check()
{
int x=map[0][0];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(map[i][j]!=x)
{
return 0;
}
}
}
return 1;
}
void turn(int x,int y)
{
map[x][y]=!map[x][y];
if(x-1>=0)
map[x-1][y]=!map[x-1][y];
if(x+1<4)
map[x+1][y]=!map[x+1][y];
if(y-1>=0)
map[x][y-1]=!map[x][y-1];
if(y+1<4)
map[x][y+1]=!map[x][y-1];
}
int dfs(int x,int y,int t)
{
if(check())
{
if(ans>t)
ans=t;
return 0;
}
if(x>=4||y>=4) return 0;
int nx,ny;
nx=(x+1)%4;
ny=y+(x+1)/4;
dfs(nx,ny,t);
turn(x,y);
dfs(nx,ny,t+1);
turn (x,y);
return 0;
}
int main()
{
for(i=0;i<4;i++)
{
scanf("%s",s);
for(j=0;j<4;j++)
{
if(s[j]=='b')
map[i][j]=0;
else
map[i][j]=1;
}
}
dfs(0,0,0);
if(ans==inf)
printf("Impossible\n");
else printf("%d\n",ans);
return 0;
}