Poj 1753

题目大意:翻转棋游戏,在一个4*4的棋盘上,每个格子有两个颜色黑或白,把该个格子翻转(黑变白白变黑),同时它的上下左右4个格子也会如此,求把整个棋盘的颜色统一的方案,没有输出impossible

解:状态压缩bfs,弱死了,首先是标记二掉了,一种状态出现过就不用取消了,二是忘记输出无解,wa+tle..,弱弱弱弱弱弱弱弱弱

View Code
 1 //Flip Game
 2 const
 3         win1=65535;
 4         inf='1.txt';
 5         len=win1+100;
 6 var
 7         ac: array[0..5, 0..5]of longint=(
 8                                         (-1,-1,-1,-1,-1,-1),
 9                                         (-1, 1, 2, 3, 4, -1),
10                                         (-1, 5, 6, 7, 8, -1),
11                                         (-1, 9, 10, 11, 12, -1),
12                                         (-1, 13, 14, 15, 16, -1),
13                                         (-1, -1, -1, -1, -1, -1)
14                                         );
15         chaos: longint;
16         ans: longint;
17         q, step: array[0..len]of longint;
18         visit: array[0..win1]of boolean;
19 procedure init;
20 var
21         i, j: longint;
22         c: char;
23 begin
24   fillchar(visit, sizeof(visit), 0);
25   chaos := 0;
26   for i := 1 to 4 do begin
27     for j := 1 to 4 do begin
28       read(c);
29       if c='b' then chaos := chaos or (1 << (ac[i, j]-1));
30     end;
31     readln;
32   end;
33 end;
34 
35 procedure main;
36 var
37         head, tail, u, i, j, tmp: longint;
38 begin
39   head := 0; tail := 1;
40   q[tail] := chaos;
41   step[tail] := 1;
42   visit[chaos] := true;
43   ans := -1;
44   if (chaos=0)or(chaos=win1) then begin ans := 0; exit; end;
45   repeat
46     inc(head); if head=len then head := 1;
47     u := q[head];
48     for i := 1 to 4 do
49       for j := 1 to 4 do begin
50         //tmp
51         tmp := u;
52         if i>1 then tmp := tmp xor (1 << (ac[i-1, j]-1));
53         if j<4 then tmp := tmp xor (1 << (ac[i, j+1]-1));
54         if i<4 then tmp := tmp xor (1 << (ac[i+1, j]-1));
55         if j>1 then tmp := tmp xor (1 << (ac[i, j-1]-1));
56         tmp := tmp xor (1<<(ac[i, j]-1));
57         //tmp
58 
59         if visit[tmp]=false then begin
60           if (tmp=win1)or(tmp=0) then begin
61             ans := step[head];
62             exit;
63           end;
64           visit[tmp] := true;
65           inc(tail); if tail=len then tail := 1;
66           step[tail] := step[head]+1;
67           q[tail] := tmp;
68         end;
69       end;
70   until head=tail;
71 end;
72 
73 procedure print;
74 begin
75   if ans>-1 then writeln(ans)
76     else writeln('Impossible');
77 end;
78 
79 begin
80   assign(input,inf); reset(input);
81   init;
82   main;
83   print;
84 end.
posted @ 2012-05-04 18:04  F.D.His.D  阅读(169)  评论(0编辑  收藏  举报