POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22057   Accepted: 8521   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

同1753一样的代码,但是这题有一点不是很明白,就是没有Impossible的可能,
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 int handle[10][10];
 7 int flag,step;
 8 int r[20],c[20];
 9 int all_open()
10 {
11     for(int i = 1; i <= 4; i++)
12     {
13         for(int j = 1; j <= 4; j++)
14             if(!handle[i][j])
15                 return false;
16     }
17     return true;
18 }
19 void change(int row, int col)
20 {
21     handle[row][col] = !handle[row][col];  //没写这个DFS里面就是死循环了
22     for(int i = 1; i <= 4; i++)
23     {
24         handle[row][i] = !handle[row][i];
25         handle[i][col] = !handle[i][col];
26     }
27 }
28 void dfs(int row, int col, int deep)
29 {
30     if(deep == step)
31     {
32         flag = all_open();
33         return;
34     }
35     if(flag || row > 4)
36         return;
37 
38     change(row, col);
39     r[deep] = row;
40     c[deep] = col;
41     if(col < 4)
42     {
43         dfs(row, col + 1, deep + 1);
44     }
45     else
46     {
47         dfs(row + 1, 1, deep + 1);
48     }
49     change(row, col);
50     if(col < 4)
51     {
52         dfs(row, col + 1, deep);
53     }
54     else
55     {
56         dfs(row + 1, 1, deep);
57     }
58     return;
59 }
60 int main()
61 {
62     char s[10];
63     while(scanf("%s", s) != EOF)
64     {
65         memset(handle, 0, sizeof(handle));
66         for(int i = 0; i < 4; i++)
67             if(s[i] == '-')
68                 handle[1][i + 1] = 1;
69         for(int i = 2; i <= 4; i++)
70         {
71             scanf("%s", s);
72             for(int j = 0; j < 4; j++)
73                 if(s[j] == '-')
74                     handle[i][j + 1] = 1;
75         }
76 
77         flag = 0;
78         for(step = 0; step <= 16; step++)
79         {
80             dfs(1, 1, 0);
81             if(flag)
82                 break;
83         }
84         if(flag)
85         {
86             printf("%d\n", step);
87             for(int i = 0; i < step; i++)
88                 printf("%d %d\n", r[i],c[i]);
89         }
90     }
91     return 0;
92 }
View Code

 

你可能感兴趣的:(POJ2965The Pilots Brothers' refrigerator(枚举+DFS))