poj1753之DFS
来源:互联网 发布:freebsd和centos 编辑:程序博客网 时间:2024/06/05 00:09
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 45109 Accepted: 19321
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwbbbwbbwwbbwww
Sample Output
4
Source
Northeastern Europe 2000
题目大意:现在有一个4*4的棋盘,其中‘b’代表是黑棋,‘w’代表是白棋。现在有一种翻棋的方法,当翻某个棋时,这个棋子和它上下左右的棋子都变为相反的颜色,求最少翻几个棋子,所有的棋子的颜色能够相同。
题目分析:用DFS暴力解决就可以了,这里有一点就是对于下一次搜索可以只搜前面搜索的后面的棋子,即row*4+col<i*4+j,而且下一次搜索时每次之前要设置为已访问,防止被再次搜到,同时棋子肯定要执行翻棋子的操作,这种搜索完成之后,visit数组恢复为0,棋子恢复到未翻棋子时的情况,让其能被其他情况搜到。
代码:
#include <iostream>#include <queue>using namespace std;int map[10][10];int MIN=17;int visit[10][10]={0};int dx[]={-1,0,1, 0,0};int dy[]={ 0,1,0,-1,0};//判断是否全白或全黑bool finish(){ int n=0; for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ if(map[i][j]){ n++; } } } if(n%16==0) return true; else return false;}//翻棋子void flip(int i,int j){ for(int k=0;k<5;k++){ int x=i+dx[k]; int y=j+dy[k]; if(x>=1&&x<=4&&y>=1&&y<=4){ map[x][y]=!map[x][y]; } }}void dfs(int row,int col,int steps){ if(finish()){ //如果全白或全黑,则可结束。 if(MIN>steps) MIN=steps; return; } if(steps>=16){ //每个棋子最多只能翻一次,当其被翻两次时,相当于没被翻过。 return; } for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ if(!visit[i][j]&&row*4+col<i*4+j){ visit[i][j]=1; flip(i,j); dfs(i,j,steps+1); flip(i,j); visit[i][j]=0; } } }}int main(){ char c; for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ cin>>c; if(c=='b') map[i][j]=0; else map[i][j]=1; } } dfs(0,0,0); if(MIN<17) cout<<MIN<<endl; else cout<<"Impossible"<<endl; return 0;}
阅读全文
0 0
- poj1753之DFS
- poj1753 DFS
- POJ1753 Flip Game【DFS】
- poj1753(dfs暴力枚举)
- POJ1753 Flip Game (DFS)
- POJ1753 Flip Game DFS+枚举
- poj1753 Flip Game DFS,枚举
- 【dfs】poj1753——Flip Game
- poj1753 Flip Game(枚举Enum+dfs)
- poj1753解题报告(1):DFS算法
- POJ1753
- poj1753
- poj1753
- poj1753
- POJ1753
- poj1753
- poj1753
- poj1753
- LeetCode第四题--求两个排序好的数组的中位数
- 查找-二分查找法-数据结构(25)
- Linux基础(一)
- 多线程(2)
- 使用网络技术————新闻客户端
- poj1753之DFS
- 【2013年第四届蓝桥杯C/C++程序设计本科B组决赛 连续奇数和(结果填空) 】
- C
- 二叉树
- 通讯录1.0(可实现插入输出)
- ChannelPipeline
- 计算几何资料(不定时更新)
- win7远程桌面连接Ubuntu 14.04
- 总结(一)