poj 2965 The Pilots Brothers' refrigerator
2013-03-20 14:10
441 查看
这个枚举+DFS是借鉴大牛的
在此扩充了一下路径的保存
然后想提一个很无语的错误,在测试样例的时候,总是没有任何结果输出来,看了程序压根一点错误都没有(没看出任何逻辑硬伤)
没办法,断点调试,输出中间结果,然后在测试到step=6的时候,发现经过flip变化之后,map[1][1]还是true,并没有变,觉得异常奇怪
看了该row col对应的此行和此列,其他的变换都没有错误,就当前的交点还是原来的值,于是才反应过来,在进行翻转的时候,交点翻了两次
回到了原来的状态,于是马上写个判断语句,AC
小错误真是要人命啊
不过也从侧面看到了,自己写的程序一定要追踪其结果和程序中变量状态的变化,不仅要能分析时间和空间的复杂度,而且还要学会用一些小工具
来测电脑的运行情况,比如CPU状态,内存状态,每个函数跑的时候,CPU曲线如何变化,内存,缓存,页面的变化情况,要使程序在手上跑的非常得心应手
这对以后写一些更高级应用的程序时会有很大的帮助
源代码如下:
在此扩充了一下路径的保存
然后想提一个很无语的错误,在测试样例的时候,总是没有任何结果输出来,看了程序压根一点错误都没有(没看出任何逻辑硬伤)
没办法,断点调试,输出中间结果,然后在测试到step=6的时候,发现经过flip变化之后,map[1][1]还是true,并没有变,觉得异常奇怪
看了该row col对应的此行和此列,其他的变换都没有错误,就当前的交点还是原来的值,于是才反应过来,在进行翻转的时候,交点翻了两次
回到了原来的状态,于是马上写个判断语句,AC
小错误真是要人命啊
不过也从侧面看到了,自己写的程序一定要追踪其结果和程序中变量状态的变化,不仅要能分析时间和空间的复杂度,而且还要学会用一些小工具
来测电脑的运行情况,比如CPU状态,内存状态,每个函数跑的时候,CPU曲线如何变化,内存,缓存,页面的变化情况,要使程序在手上跑的非常得心应手
这对以后写一些更高级应用的程序时会有很大的帮助
源代码如下:
#include<iostream> using namespace std; bool map[6][6],find=false; int step; struct { int row; int col; }r[17]; //记录路径 bool isgoal() { int i,j; for(i=1;i<=4;i++) for(j=1;j<=4;j++) if(map[i][j]!=true) return false; return true; } void flip(int row,int col) { int i,j; for(i=1;i<5;i++) map[i][col]=!map[i][col]; for(j=1;j<5;j++) { if(j==col) //这个地方一定要注意,不起眼但致命 continue; map[row][j]=!map[row][j]; } } void dfs(int row,int col,int dep) { if(dep==step) { find=isgoal(); return ; } if(find||row==5) return ; flip(row,col); if(col<4) { dfs(row,col+1,dep+1); r[dep].row=row; r[dep].col=col; } else { dfs(row+1,1,dep+1); r[dep].row=row; r[dep].col=col; } flip(row,col); if(col<4) dfs(row,col+1,dep); else dfs(row+1,1,dep); } int main() { int i,j; char c; for(i=1;i<=4;i++) for(j=1;j<=4;j++) { cin>>c; if(c=='+') map[i][j]=false; else map[i][j]=true; } for(step=0;step<=16;step++) { dfs(1,1,0); if(find) break; } if(find) { cout<<step<<endl; for(i=0;i<step;i++) cout<<r[i].row<<" "<<r[i].col<<endl; } return 0; }
相关文章推荐
- POJ:2965 The Pilots Brothers' refrigerator(二进制枚举)
- poj 2965 The Pilots Brothers' refrigerator
- Poj 2965 The Pilots Brothers' refrigerator
- Poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator(BFS+二进制判重)
- POJ2965,The Pilots Brothers' refrigerator,枚举,DFS
- 解题报告:POJ 2965 The Pilots Brothers' refrigerator 两种做法
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965.The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator 【POJ--2965】
- POJ 2965 (超简单代码47毫秒AC) The Pilots Brothers' refrigerator
- poj2965——The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj2965_The Pilots Brothers' refrigerator(bfs)
- poj2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- poj2965 The Pilots Brothers' refrigerator
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)