Flip Game(POJ_1753)
来源:互联网 发布:新速特软件站怎么下载 编辑:程序博客网 时间:2024/05/22 07:54
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:
1.Choose any one of the 16 pieces.2.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
Source
Northeastern Europe 2000
这道题当初做的时候是一行行地翻过来的。。。想了n种情况。。。写了4k的代码。。。现在再看那个代码真是无比的佩服我自己。。。附上当年的渣代码。。
咱的代码(可直接略过
#include <iostream>#include <cstdio>using namespace std;int a[5][5],c[5][5];char b;int ans=9999999,sum=0;void flip(int aaa){ for(int i=1; i<4; i++) { for(int j=0; j<4; j++) { if(a[i-1][j]==aaa) { sum++; a[i][j]=-a[i][j]; a[i-1][j]=-a[i-1][j]; if(j>0) a[i][j-1]=-a[i][j-1]; if(j<3) a[i][j+1]=-a[i][j+1]; if(i<3) a[i+1][j]=-a[i+1][j]; } } } int j; for(j=0; j<4; j++) if(a[3][j]==aaa) break; if(j==4) ans=min(ans,sum); // cout<<endl<<sum<<endl<<endl; sum=0;}void huifu()//恢复原始状态{ for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { a[i][j]=c[i][j]; } }}void print(){ for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { cout<<a[i][j]; } cout<<endl; }}int main(){ for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { cin>>b; if(b=='b') { a[i][j]=1; } else { a[i][j]=-1; } c[i][j]=a[i][j]; } }//0 flip(1); huifu(); flip(-1);// print();//1 for(int l=0; l<4; l++)//4种 { huifu(); sum=1; a[0][l]=-a[0][l];//第一行及其相连翻转 if(l!=0) a[0][l-1]=-a[0][l-1]; if(l!=3) a[0][l+1]=-a[0][l+1]; a[1][l]=-a[1][l];// print(); flip(1); huifu(); sum=1; a[0][l]=-a[0][l];//第一行及其相连翻转 if(l!=0) a[0][l-1]=-a[0][l-1]; if(l!=3) a[0][l+1]=-a[0][l+1]; a[1][l]=-a[1][l]; flip(-1); }//2 for(int i=0; i<3; i++) //第一个被翻转 { for(int j=i+1; j<4; j++) //第二个被翻转 { huifu(); sum=2; a[0][i]=-a[0][i];//第一个及其相连翻转 if(i!=0) a[0][i-1]=-a[0][i-1]; if(i!=3) a[0][i+1]=-a[0][i+1]; a[1][i]=-a[1][i]; a[0][j]=-a[0][j];//第二个及其相连翻转 if(j!=0) a[0][j-1]=-a[0][j-1]; if(j!=3) a[0][j+1]=-a[0][j+1]; a[1][j]=-a[1][j];// print(); flip(1); huifu(); sum=2; a[0][i]=-a[0][i];//第一个及其相连翻转 if(i!=0) a[0][i-1]=-a[0][i-1]; if(i!=3) a[0][i+1]=-a[0][i+1]; a[1][i]=-a[1][i]; a[0][j]=-a[0][j];//第二个及其相连翻转 if(j!=0) a[0][j-1]=-a[0][j-1]; if(j!=3) a[0][j+1]=-a[0][j+1]; a[1][j]=-a[1][j]; flip(-1); } } //3 for(int i=0; i<2; i++)//一 { for(int j=i+1; j<3; j++)//二 { for(int k=j+1; k<4; k++)//三 { huifu(); sum=3; a[0][i]=-a[0][i];//第一个及其相连翻转 if(i!=0) a[0][i-1]=-a[0][i-1]; if(i!=3) a[0][i+1]=-a[0][i+1]; a[1][i]=-a[1][i]; a[0][j]=-a[0][j];//第二个及其相连翻转 if(j!=0) a[0][j-1]=-a[0][j-1]; if(j!=3) a[0][j+1]=-a[0][j+1]; a[1][j]=-a[1][j]; a[0][k]=-a[0][k];//第三个及其相连翻转 if(k!=0) a[0][k-1]=-a[0][k-1]; if(k!=3) a[0][k+1]=-a[0][k+1]; a[1][k]=-a[1][k]; // print(); flip(1); huifu(); sum=3; a[0][i]=-a[0][i];//第一个及其相连翻转 if(i!=0) a[0][i-1]=-a[0][i-1]; if(i!=3) a[0][i+1]=-a[0][i+1]; a[1][i]=-a[1][i]; a[0][j]=-a[0][j];//第二个及其相连翻转 if(j!=0) a[0][j-1]=-a[0][j-1]; if(j!=3) a[0][j+1]=-a[0][j+1]; a[1][j]=-a[1][j]; a[0][k]=-a[0][k];//第三个及其相连翻转 if(k!=0) a[0][k-1]=-a[0][k-1]; if(k!=3) a[0][k+1]=-a[0][k+1]; a[1][k]=-a[1][k]; flip(-1); } } }//4 huifu(); sum=4; for(int i=0; i<4; i++) { a[0][i]=-a[0][i]; if(i!=0) a[0][i-1]=-a[0][i-1]; if(i!=3) a[0][i+1]=-a[0][i+1]; a[1][i]=-a[1][i]; }// print(); flip(1); huifu(); sum=4; for(int i=0; i<4; i++) { a[0][i]=-a[0][i]; if(i!=0) a[0][i-1]=-a[0][i-1]; if(i!=3) a[0][i+1]=-a[0][i+1]; a[1][i]=-a[1][i]; } flip(-1); if(ans<9999999) cout<<ans<<endl; else cout<<"Impossible"<<endl; return 0;}
别人家的代码
咱就是这么渣,别人不管用什么方法代码都很短。。。看到了一个解题报告感觉还不错,附上链接~
http://www.cnblogs.com/Griselda/archive/2013/07/16/3192883.html
- Flip Game(POJ_1753)
- Flip Game(POJ_1753)
- Poj_1753 Flip Game(状态压缩,bfs)
- poj_1753 Flip Game(dfs+枚举)/(bfs+位运算)
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- flip game
- Flip Game
- Flip Game
- FLIP GAME
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flex HTTPService
- Linux下的tar压缩解压缩命令详解
- Mac下composer的使用
- PHP Warning: PHP Startup: Unable to load dynamic library 'C:/PHP/ext\\php_ldap.dll' - The specified
- Ubuntu硬盘分区挂载
- Flip Game(POJ_1753)
- 黑马程序员_集合_map集合以及集合工具类Collections
- The difference between film faced plywood and wood I joist beam
- Flex HTTPService
- asp.net+swfupload 多图片批量上传(附源码下载)
- 编译依赖、链接依赖和发布库信息隐藏
- HTML服务器控件和WEB服务器控件的区别和联系介绍
- nodejs中如何创建和加载模块
- es all version