(poj1.1.2)2965(The Pilots Brothers' refrigerator——DFS+枚举)
2013-09-13 21:06
309 查看
题目大意:将题中所给的图翻转成
----
----
----
----的状态,求需要多少步??
翻转规则是:
1)你可以翻转任何一个棋子
2)该棋子所在的航和列也会跟着翻转。
解题思路:本题和1753的思路很像。。都是使用暴力+DFS+ 枚举,但翻转规则和判断方法有所改变,并且需要记录下路径。
1)判断规则变成
2)翻转规则变成
3)需要在翻转时记录路径
代码如下:
----
----
----
----的状态,求需要多少步??
翻转规则是:
1)你可以翻转任何一个棋子
2)该棋子所在的航和列也会跟着翻转。
解题思路:本题和1753的思路很像。。都是使用暴力+DFS+ 枚举,但翻转规则和判断方法有所改变,并且需要记录下路径。
1)判断规则变成
bool judge_all(){ int i,j; for(i = 1 ; i < 5 ; ++i){ for(j = 1 ; j < 5 ; ++j){ if( chess[i][j] != false){ return false; } } } return true; }
2)翻转规则变成
/** * 翻转规则是:将自己及其所在行、所在列的棋子都翻转 */ void flip(int row ,int col){ int i; for(i = 1 ; i < 5 ; ++i){ chess[row][i] = !chess[row][i]; } for(i = 1 ; i < 5 ; ++i){ chess[i][col] = !chess[i][col]; } //因为上面chess[row][col]这个棋子翻转了两次,变回了原样,所以这里还要再翻转一次 chess[row][col] = !chess[row][col]; }
3)需要在翻转时记录路径
flip(row,col); r[deep] = row;//记录路径的横坐标 c[deep] = col;//记录路径的纵坐标
代码如下:
/*
* 2965_1.cpp
*
* Created on: 2013年9月13日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
using namespace std;
bool chess[6][6] = {false};
bool flag ;
int step;
int r[20];
int c[20];
bool judge_all(){ int i,j; for(i = 1 ; i < 5 ; ++i){ for(j = 1 ; j < 5 ; ++j){ if( chess[i][j] != false){ return false; } } } return true; }
/** * 翻转规则是:将自己及其所在行、所在列的棋子都翻转 */ void flip(int row ,int col){ int i; for(i = 1 ; i < 5 ; ++i){ chess[row][i] = !chess[row][i]; } for(i = 1 ; i < 5 ; ++i){ chess[i][col] = !chess[i][col]; } //因为上面chess[row][col]这个棋子翻转了两次,变回了原样,所以这里还要再翻转一次 chess[row][col] = !chess[row][col]; }
void dfs(int row , int col , int deep){
if(deep == step){
flag = judge_all();
return ;
}
if(flag || row == 5){
return ;
}
flip(row,col); r[deep] = row;//记录路径的横坐标 c[deep] = col;//记录路径的纵坐标
if(col < 4){
dfs(row,col+1,deep+1);
}else{
dfs(row+1,1,deep+1);
}
flip(row,col);
if(col<4){
dfs(row,col+1,deep);
}else{
dfs(row+1,1,deep);
}
}
int main(){
int i,j;
char temp;
flag = false;
for(i = 1 ; i < 5 ; ++i){
for(j = 1 ; j < 5 ; ++j){
cin >> temp;
if(temp == '+'){
chess[i][j] = true;
}
}
}
for(step = 0 ; step <= 16 ; ++step){
dfs(1,1,0);
if(flag){
break;
}
}
cout<<step<<endl;
for(i = 0 ; i < step ; ++i){
cout<<r[i]<<" "<<c[i]<<endl;
}
}
相关文章推荐
- POJ 2965 The Pilots Brothers' refrigerator (DFS + 枚举)
- The Pilots Brothers' refrigerator(POJ 2965)(dfs枚举+状态压缩)
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- poj 2965 The Pilots Brothers' refrigerator——DFS(分类是枚举)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+DFS)
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- POJ 2965 The Pilots Brothers' refrigerator 枚举dfs
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- ACM--枚举+DFS/巧妙思想--POJ--2965--The Pilots Brothers' refrigerator--初期算法
- POJ2965,The Pilots Brothers' refrigerator,枚举,DFS
- 文章标题 POJ 2965 : The Pilots Brothers' refrigerator(枚举+dfs)
- poj 2965 The Pilots Brothers' refrigerator 【dfs+枚举】【双十一大礼包】【刷题计划】
- POJ 2965-The Pilots Brothers' refrigerator(贪心+枚举)
- POJ 2965-The Pilots Brothers' refrigerator(枚举&&DFS&&输出过程)
- POJ-2965-The Pilots Brothers' refrigerator-bfs枚举-位运算
- 【枚举思考题】poj 2965 The Pilots Brothers' refrigerator
- POJ:2965 The Pilots Brothers' refrigerator(二进制枚举)
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)