poj 2965 -- The Pilots Brothers' refrigerator

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17620   Accepted: 6676   Special Judge

Description

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+--
----
----
-+--

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4

这个题用位运算搜索(枚举)即可,只不过多了一个打印路径。递归打印就行。

 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   ThePilotsBrothersRefrigerator.cpp
 4  *       Creat time :   2014-05-13 19:58
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M (1<<17)
15 using namespace std;
16 int cnt[M],number;
17 struct node
18 {
19     int x,y,n;
20 }node[M];
21 int dir[4][4]={
22     0xf888,0xf444,0xf222,0xf111,
23     0x8f88,0x4f44,0x2f22,0x1f11,
24     0x88f8,0x44f4,0x22f2,0x11f1,
25     0x888f,0x444f,0x222f,0x111f
26 };
27 void Print(int s)
28 {
29     if(number == node[s].n)
30         return ;
31     int t = node[s].n;
32     Print(t);
33     printf("%d %d\n",node[t].x+1,node[t].y+1);
34 }
35 int BFS(int num)
36 {
37     queue<int>pq;
38     cnt[num] = 1;
39     pq.push(num);
40     while(!pq.empty()){
41         int v = pq.front();
42         pq.pop();
43         for(int i = 0; i < 4; i++){
44             for(int j = 0; j < 4; j++){
45                 int t = v^dir[i][j];
46                 if(t == 0x0000){
47                     cnt[t] = cnt[v];
48                     node[t] = {i,j,v};
49                     return cnt[t];
50                 }
51                 if(!cnt[t]){
52                     cnt[t] = cnt[v]+1;
53                     node[t] = {i,j,v};
54                     pq.push(t);
55                 }
56             }
57         }
58     }
59     return -1;
60 }
61 int main(int argc,char *argv[])
62 {
63     char str[6];
64     while(cin>>str){
65         clr(cnt,0);
66         clr(node,0);
67         number = 0;
68         for(int i = 0; str[i]; i++){
69             number = number << 1;
70             if(str[i] == '+'){
71                 number |= 1;
72             }
73         }
74         for(int i = 0; i < 3; i++){
75             cin>>str;
76             for(int j = 0; str[j]; j++){
77                 number = number << 1;
78                 if(str[j] == '+'){
79                     number |= 1;
80                 }
81             }
82         }
83         int steps = BFS(number);
84         printf("%d\n",steps);
85         Print(0);
86         printf("%d %d\n",node[0].x+1,node[0].y+1);
87     }
88     return 0;
89 }
View Code

 

posted @ 2014-05-14 10:17  ZeroCode_1337  阅读(163)  评论(0编辑  收藏  举报