Flip Game
2015-06-15 14:59
106 查看
Flip Game
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:
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 33519 | Accepted: 14642 |
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:
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
bwwb bbwb bwwb bwww
Sample Output
4/* 枚举第一行的情况(用二进制),进行反转,然后反转后面的行以保正前面的行符合要求 最后看最后一行是不是符合要求 */ #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <cctype> #include <queue> #include <stack> #include <algorithm> using namespace std; int a[5][5],b[5][5]; int Arr[5]; char s[5][5]; void Trans()//转化成数字 { for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { a[i][j]=s[i][j]=='b'?1:0; } } } void tran(int n)//二进制转化 { memset(Arr,0,sizeof(Arr)); int top=3; while(n) { Arr[top--]=n&1; n=n>>1; } } void Creat() { for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { b[i][j]=a[i][j]; } } } void change(int i,int j)//反转 { b[i][j]=b[i][j]==0?1:0; if(j>0) { b[i][j-1]=b[i][j-1]==0?1:0; } if(j<3) { b[i][j+1]=b[i][j+1]==0?1:0; } if(i<3) { b[i+1][j]=b[i+1][j]==0?1:0; } if(i>0) { b[i-1][j]=b[i-1][j]==0?1:0; } } int main() { int sum; int Max=1000; for(int i=0; i<4; i++) { scanf("%s",s[i]); } Trans(); for(int i=0; i<=15; i++) { tran(i); for(int j=0; j<2; j++) { Creat(); sum=0; for(int k=0; k<4; k++) { if(Arr[k]) { change(0,k); sum++; } } for(int k=1; k<4; k++) { for(int kk=0; kk<4; kk++) { if(b[k-1][kk]!=j) { change(k,kk); sum++; } } } bool flag=true; for(int k=0; k<4; k++) { if(b[3][k]!=j) { flag=false; break; } } if(flag) { if(Max>sum) { Max=sum; } if(Max==0) { break; } } else//如果不能反转成则就不可能反转成功 { break; } } } if(Max!=1000) { printf("%d\n",Max); } else { printf("Impossible\n"); } return 0; }
相关文章推荐
- swift 和 Objective-C 互相调用
- HashMap源码学习
- Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏
- [HDOJ1005]Number Sequence
- ln 软链接,硬链接 详解
- drawableLeft
- MyEclipse + Maven开发Web工程的详细配置过程
- Programming with Objective-C 学习笔记
- xenomai config
- linux 命令——14 head (转)
- FreeMarker的FTL页面中include引用UTF-8文件导致错位的问题处理
- 第十五周项目2(1)-二进制文件浏览器
- 黑马程序员_异常
- AndroidManifest笔记
- px、em、rem、%单位
- android 中layer-list的用法
- Silverlight 控件
- django shell 集合
- 网页标题栏实现走马灯效果
- Android SDK(版本2.3-5.1),搭建完好的eclipse+Android开发环境