【递归】POJ2965-The Pilots Brothers' refrigerator

和POJ1753的方法一致,唯一不同的是需要记录路径,只需添加一个全集变量的path路径即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int INF=100000;
 5 int map[4][4];
 6 int path[4][4];
 7 int maxpath[4][4];
 8 int ans=INF;
 9
10 void open(int step,int turn)
11 {
12     int tempmap[4][4];
13     if (turn>=ans) return;
14     if (step==16)
15     {
16         int sum=0;
17         for (int i=0;i<4;i++)
18             for (int j=0;j<4;j++) sum+=map[i][j];
19         if (sum==0)
20         {
21             ans=turn;
22             for (int i=0;i<4;i++)
23                 for (int j=0;j<4;j++) maxpath[i][j]=path[i][j];
24         }
25     }
26     else
27     {
28         for (int i=0;i<4;i++)
29             for (int j=0;j<4;j++) tempmap[i][j]=map[i][j];
30         for (int d=0;d<2;d++)
31         {
32             int x=step/4,y=step%4;
33             path[x][y]=d;
34             if (d==1)
35             {
36                 map[x][y]=1-map[x][y];
37                 for (int i=0;i<4;i++)
38                 {
39                     map[i][y]=1-map[i][y];
40                     map[x][i]=1-map[x][i];
41                 }
42             }
43             open(step+1,turn+d);
44             for (int i=0;i<4;i++)
45                 for (int j=0;j<4;j++) map[i][j]=tempmap[i][j];
46         }
47     }
48 }
49
50 int main()
51 {
52     for (int i=0;i<4;i++)
53     {
54         char c;
55         for (int j=0;j<4;j++)
56         {
57             scanf("%c",&c);
58             if (c==‘-‘) map[i][j]=0;
59                         else map[i][j]=1;
60         }
61         getchar();
62     }
63     open(0,0);
64     cout<<ans<<endl;
65     for (int i=0;i<4;i++)
66         for (int j=0;j<4;j++)
67             if (maxpath[i][j]==1) cout<<i+1<<‘ ‘<<j+1<<endl;
68     return 0;
69 }

【递归】POJ2965-The Pilots Brothers' refrigerator

时间: 2024-10-17 04:15:47

【递归】POJ2965-The Pilots Brothers' refrigerator的相关文章

poj2965 The Pilots Brothers&#39; refrigerator

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 refri

poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2965 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

poj2965 The Pilots Brothers&#39; refrigerator 枚举或DFS

http://poj.org/problem?id=2965 一.枚举每一个‘+’点,对该点和该点所在的同一行和同一列所有点进行操作,开关本身状态改变了7次,开关同一行.同一列的开关状态改变了4次,其他开关状态改变了2次. 然后,用一个数组存取每个操作,如果为奇数,则证明该点为必须点,否则为不必要的. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char map[4][4]; 5 int ans[4

poj 2965 The Pilots Brothers&#39; refrigerator[ 枚举 ]

传送门:http://poj.org/problem?id=2965 思路:二进制枚举,递归输出路径.G++ 900+Ms勉强过,C++超时. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map>

POJ 2965 The Pilots Brothers&#39; refrigerator

原题链接:http://poj.org/problem?id=2965 简单的dfs,因为是要求最小值,枚举深度来得到递归终止条件,和 poj1753很像,毕竟是放在一起的. #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <cmath> #include <cstdlib> #include <vector&g

poj2965The Pilots Brothers&#39; refrigerator

背景:和poj1753一样,用dfs就可以做出来,只是和1753相比较得输出一些步骤,不过这也不麻烦,直接用两个数组就可以存储了.不过记住当递归回来的时候记住把数组里面对应位置的元素清零. 思路:同上一篇1753. #include <stdio.h> #include <string.h> int q[4][4],ok=0,r[16],c[16]; int iswin(void) { for(int i=0;i<4;i++) for(int j=0;j<4;j++)

The Pilots Brothers&#39; refrigerator - poj 2965

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   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 handle

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o