Poj 2965
解:状态压缩bfs,只是要优化常数,直接用ac[I, j]表示选择i行j列的状态转移,从我注释的地方能看出我为何tle
View Code
1 //The Pilots Brothers' refrigerator 2 const 3 bili:array[1..4, 1..4]of longint=((1, 1<<1, 1<<2, 1<<3), 4 (1<<4, 1<<5, 1<<6, 1<<7), 5 (1<<8, 1<<9, 1<<10, 1<<11), 6 (1<<12, 1<<13, 1<<14, 1<<15) 7 ); 8 9 wings=(1 << 16 -1)+100; 10 inf='1.txt'; 11 type 12 data=record 13 x, y: longint; 14 end; 15 var 16 ac: array[1..4, 1..4]of longint; 17 chaos: longint; 18 from, q, step: array[0..wings]of longint; 19 ans: array[0..wings]of data; 20 visit: array[0..wings]of boolean; 21 procedure init; 22 var 23 i, j, k, tmp: longint; 24 c: char; 25 begin 26 fillchar(visit, sizeof(visit), 0); 27 from[1] := 0; 28 //fillchar(from, sizeof(from), 0); 29 //fillchar(step, sizeof(step), 0); 30 31 chaos := 0; 32 for i := 1 to 4 do begin 33 for j := 1 to 4 do begin 34 read(c); 35 if c='+' then chaos := chaos or bili[i, j]; 36 end; 37 readln; 38 end; 39 for i := 1 to 4 do 40 for j := 1 to 4 do begin 41 tmp := 0; 42 for k := 1 to 4 do begin 43 tmp := tmp or bili[i, k]; 44 tmp := tmp or bili[k, j]; 45 end; 46 ac[i, j] := tmp; 47 end; 48 end; 49 50 procedure print(xx: longint); 51 begin 52 if from[xx]=0 then exit; 53 print(from[xx]); 54 with ans[xx] do begin 55 writeln(x, ' ', y); 56 end; 57 end; 58 59 procedure main; 60 var 61 i, j, k, head, tail, u, tmp: longint; 62 begin 63 head := 0; tail := 1; 64 q[tail] := chaos; visit[chaos] := true; 65 step[tail] := 0; 66 if chaos=0 then begin 67 writeln(0); 68 exit; 69 end; 70 repeat 71 inc(head); 72 u := q[head]; 73 for i := 1 to 4 do 74 for j := 1 to 4 do begin 75 tmp := u; 76 {for k := 1 to 4 do begin 77 tmp := tmp xor ac[i, k]; 78 tmp := tmp xor ac[k, j]; 79 end;} 80 tmp := tmp xor ac[i, j]; 81 if not visit[tmp] then begin 82 visit[tmp] := true; 83 inc(tail); 84 q[tail] := tmp; 85 with ans[tail] do begin 86 x := i; y := j; 87 end; 88 from[tail] := head; 89 step[tail] := step[head] + 1; 90 if tmp=0 then begin 91 writeln(step[tail]); 92 print(tail); 93 exit; 94 end; 95 end; 96 end; 97 until head = tail; 98 end; 99 100 begin 101 assign(input,inf); reset(input); 102 init; 103 main; 104 end.