poj 1753 Flip Game——DFS(分类是枚举)
来源:互联网 发布:知我药妆是正品吗知乎 编辑:程序博客网 时间:2024/06/02 06:02
Flip Game
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 29494
Accepted: 12762
Memory Limit: 65536KTotal Submissions: 29494
Accepted: 12762
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
第一次用c++写程序...
#include <iostream>#include <cstdlib>#include <string>#include <climits>using namespace std;char mp1[4][4];int mp2[4][4];int num=INT_MAX;int pd(){ int i,j,js=0; for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(mp2[i][j]==1) { js++; } } } if(js==16||js==0) { return 1; } return 0;}//此函数判断是否成为全白色或是全黑色void gz(int a,int b){ if(mp2[a][b]==1) { mp2[a][b]=0; } else { mp2[a][b]=1; }}void fan(int x,int y){ gz(x,y); if(x+1>=0&&x+1<4&&y>=0&&y<4) { gz(x+1,y); } if(x-1>=0&&x-1<4&&y>=0&&y<4) { gz(x-1,y); } if(x>=0&&x<4&&y+1>=0&&y+1<4) { gz(x,y+1); } if(x>=0&&x<4&&y-1>=0&&y-1<4) { gz(x,y-1); }}//fan()函数和gz()函数是来翻棋子的int dfs(int x,int y,int t){ int bx,by; if(pd()) { if(num>t) num=t; return 0; } if(x>=4||y>=4) { return 0; } bx=(x+1)%4; by=y+(x+1)/4; dfs(bx,by,t); fan(x,y); dfs(bx,by,t+1); fan(x,y);//第一个dfs()走到最末端,然后递归回去,第二个根据这个区间 //从后往前递归 return 0;}int main(){ int i,j; for(i=0;i<4;i++) { cin>>mp1[i]; } for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(mp1[i][j]=='b') { mp2[i][j]=1; } else { mp2[i][j]=0; } } } dfs(0,0,0); if(num==INT_MAX) { cout<<"Impossible\n"; } else { cout<<num<<endl; } return 0;}
0 0
- poj 1753 Flip Game——DFS(分类是枚举)
- poj 1753 Flip Game(dfs+枚举)
- poj 1753 Flip Game (dfs + 枚举)
- POJ 1753 Flip Game(dfs+枚举)
- POJ 1753 Flip Game(dfs+枚举)
- POJ 1753-Flip Game(枚举&&DFS)
- (枚举,dfs)Flip Game poj 1753
- POJ 1753 Flip Game(枚举+dfs)
- POJ 1753 FLIP GAME——枚举深度的dfs
- POJ 1753 Flip Game dfs + 枚举
- POJ 1753 Flip Game DFS枚举
- POJ 1753 Flip Game (DFS + 枚举)
- poj 1753 Flip Game【枚举+dfs】
- poj-1753Flip Game【DFS】【枚举】
- poj 1753 : Flip Game (枚举+dfs)
- POJ 1753 Flip Game(枚举+dfs)
- poj 1753 Flip Game(枚举,dfs)
- poj 1753 : Flip Game (枚举+dfs)
- sublime快速创建html头
- 没有苹果电脑如何做IOS开发
- List,LinkList,ArrayList,Map等相关存储数据结构的比较
- 两分钟彻底让你明白Android Activity生命周期(图文)!
- build GCC
- poj 1753 Flip Game——DFS(分类是枚举)
- [English]《十天突破新托福》笔记(二)——解题思路
- 使用系统iso镜像配置yum源
- JFrame与Frame区别
- ubuntu13.1下nfs服务器端的搭建
- TOJ : 1330. Mine Sweeper
- xml文件的序列化
- 10款效果惊艳的HTML5应用和源码
- 面试问题整理