1 /*
2 一道腾讯马拉松的题目 这题还真是蛋疼
3 题目 真是没什么好说的 一道很裸的BFS搜索题
4 但是这个状态真是很麻烦
5 判重麻烦 搜索麻烦 这题就是麻烦
6
7 */
8 #include<cstdio>
9 #include<cstdlib>
10 #include<cstring>
11 #include<queue>
12
13 using namespace std;
14
15 #define get(a,b,c) ((a-1)*12+(b-1)*4+c)
16
17 int en,tmp[4][4],color[37],map[9][5],q[37],nowmap[4][4],newmap[4][4];
18
19 bool num[9],use[90000000],right[37],row[4],col[4],col_find[5];
20
21 char s[10];
22
23 struct rec
24 {
25 int sta,step;
26 rec(){}
27 rec(int a,int b)
28 {
29 sta=a;step=b;
30 }
31 };
32
33 queue<rec> que;
34
35 struct edge
36 {
37 int e;
38 edge *next;
39 }*v[37],ed[100];
40
41 void add_edge(int s,int e)
42 {
43 en++;
44 ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;
45 en++;
46 ed[en].next=v[e];v[e]=ed+en;v[e]->e=s;
47 }
48
49 bool check(int nows)
50 {
51 memset(num,false,sizeof(num));
52 for (int a=3;a>=1;a--)
53 for (int b=3;b>=1;b--)
54 if (a!=3 || b!=3)
55 {
56 tmp[a][b]=nows%10;
57 num[nows%10]=true;
58 nows/=10;
59 }
60 for (int a=0;a<9;a++)
61 if (!num[a])
62 {
63 tmp[3][3]=a;
64 break;
65 }
66 int cnt=0;
67 for (int a=1;a<=3;a++)
68 for (int b=1;b<=3;b++)
69 for (int c=1;c<=4;c++)
70 {
71 cnt++;
72 color[cnt]=map[tmp[a][b]][c];
73 }
74 memset(right,false,sizeof(right));
75 memset(col_find,false,sizeof(col_find));
76 for (int a=1;a<=36;a++)
77 if (!right[a])
78 {
79 if (col_find[color[a]]) return false;
80 col_find[color[a]]=true;
81 int front=1,tail=1;
82 q[1]=a;
83 right[a]=true;
84 for (;front<=tail;)
85 {
86 int now=q[front++];
87 for (edge *e=v[now];e;e=e->next)
88 if (color[e->e]==color[now] && !right[e->e])
89 {
90 right[e->e]=true;
91 q[++tail]=e->e;
92 }
93 }
94 }
95 return true;
96 }
97
98 int main()
99 {
100 freopen("c.in","r",stdin);
101 freopen("c.out","w",stdout);
102
103 for (int a=1;a<=3;a++)
104 for (int b=1;b<=3;b++)
105 {
106 add_edge(get(a,b,1),get(a,b,3));
107 add_edge(get(a,b,1),get(a,b,4));
108 add_edge(get(a,b,2),get(a,b,3));
109 add_edge(get(a,b,2),get(a,b,4));
110 if (a!=3) add_edge(get(a,b,2),get(a+1,b,1));
111 if (b!=3) add_edge(get(a,b,4),get(a,b+1,3));
112 }
113 int cnt=0;
114 for (int a=1;a<=3;a++)
115 for (int b=1;b<=3;b++)
116 {
117 scanf("%s",s+1);
118 for (int c=1;c<=4;c++)
119 if (s[c]=='R') map[cnt][c]=0;
120 else
121 {
122 if (s[c]=='G') map[cnt][c]=1;
123 else
124 {
125 if (s[c]=='B') map[cnt][c]=2;
126 else map[cnt][c]=3;
127 }
128 }
129 if (s[5]=='1') row[a]=col[b]=true;
130 cnt++;
131 }
132 int nows=1234567;
133 if (check(nows))
134 {
135 printf("0
");
136 return 0;
137 }
138 que.push(rec(nows,0));
139 use[nows]=true;
140 rec now;
141 while (que.size())
142 {
143 now=que.front();
144 que.pop();
145 int step=now.step;
146 int nows=now.sta;
147 memset(num,false,sizeof(num));
148 for (int a=3;a>=1;a--)
149 for (int b=3;b>=1;b--)
150 if (a!=3 || b!=3)
151 {
152 nowmap[a][b]=nows%10;
153 num[nows%10]=true;
154 nows/=10;
155 }
156 for (int a=0;a<9;a++)
157 if (!num[a])
158 {
159 nowmap[3][3]=a;
160 break;
161 }
162 int news=0;
163 for (int a=1;a<=3;a++)
164 {
165 if (!row[a])
166 {
167 for (int b=1;b<=3;b++)
168 for (int c=1;c<=3;c++)
169 newmap[b][c]=nowmap[b][c];
170 int x=newmap[a][1];
171 newmap[a][1]=newmap[a][2];newmap[a][2]=newmap[a][3];newmap[a][3]=x;
172 news=0;
173 for (int b=1;b<=3;b++)
174 for (int c=1;c<=3;c++)
175 if (b!=3 || c!=3) news=news*10+newmap[b][c];
176 if (!use[news])
177 {
178 use[news]=true;
179 if (check(news))
180 {
181 printf("%d
",step+1);
182 return 0;
183 }
184 que.push(rec(news,step+1));
185 }
186 x=newmap[a][1];
187 newmap[a][1]=newmap[a][2];newmap[a][2]=newmap[a][3];newmap[a][3]=x;
188 news=0;
189 for (int b=1;b<=3;b++)
190 for (int c=1;c<=3;c++)
191 if (b!=3 || c!=3) news=news*10+newmap[b][c];
192 if (!use[news])
193 {
194 use[news]=true;
195 if (check(news))
196 {
197 printf("%d
",step+1);
198 return 0;
199 }
200 que.push(rec(news,step+1));
201 }
202 }
203 if (!col[a])
204 {
205 for (int b=1;b<=3;b++)
206 for (int c=1;c<=3;c++)
207 newmap[b][c]=nowmap[b][c];
208 int x=newmap[1][a];
209 newmap[1][a]=newmap[2][a];newmap[2][a]=newmap[3][a];newmap[3][a]=x;
210 news=0;
211 for (int b=1;b<=3;b++)
212 for (int c=1;c<=3;c++)
213 if (b!=3 || c!=3) news=news*10+newmap[b][c];
214 if (!use[news])
215 {
216 use[news]=true;
217 if (check(news))
218 {
219 printf("%d
",step+1);
220 return 0;
221 }
222 que.push(rec(news,step+1));
223 }
224 x=newmap[1][a];
225 newmap[1][a]=newmap[2][a];newmap[2][a]=newmap[3][a];newmap[3][a]=x;
226 news=0;
227 for (int b=1;b<=3;b++)
228 for (int c=1;c<=3;c++)
229 if (b!=3 || c!=3) news=news*10+newmap[b][c];
230 if (!use[news])
231 {
232 use[news]=true;
233 if (check(news))
234 {
235 printf("%d
",step+1);
236 return 0;
237 }
238 que.push(rec(news,step+1));
239 }
240 }
241 }
242 }
243
244 return 0;
245 }