POJ 1753 Flip Game
来源:互联网 发布:怎样用网络打电话 编辑:程序博客网 时间:2024/05/21 17:52
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 34437 Accepted: 15058
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
- 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.
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
题意:
在一个4x4的棋盘上放满了棋子,棋子有黑白两种颜色。你可以对棋子进行翻转操作,白色可以变成黑色,黑色变成白色。
具体翻转规则:
1 任取16个棋子的一个
2 你在改变该棋子的时候,它的上下左右的棋子都要改变。
做法:
直接BFS,犯了一个错误,就是,BFS时候没有判断 输入情况是否是目标情况。
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>using namespace std;string g[100];long long ans;long long gao[1000000];struct zhengling{long long x;//状态 long long rounds;//第几轮 long long k;//翻动第K棋子 zhengling(long long x1,long long r1,long long k1){x = x1;rounds = r1;k = k1;}};zhengling fan(zhengling tmpp,long long p)//翻转函数 {long long i = p/4;long long j = p%4;long long tmp = tmpp.x;//zhengling c;tmp^=(0x1<<p);if(i-1>=0)tmp^=(0x1<<(p-4));if(j-1>=0)tmp^=(0x1<<(p-1));if(i+1<4)tmp^=(0x1<<(p+4));if(j+1<4)tmp^=(0x1<<(p+1));zhengling c = zhengling(tmp,tmpp.rounds+1,p);return c;}void bfs(){queue<zhengling> q;zhengling fff = zhengling(ans,0,-1);;//fff = zhengling(ans,0,-1);q.push(fff);gao[ans] = 1;if((fff.x==0)||(fff.x == (0xFFFF))){cout<<fff.rounds<<endl;return;}while(!q.empty()){zhengling tmp = q.front();q.pop();for(int i=0;i<16;i++){if(tmp.k == i)continue;fff = fan(tmp,i);if(!gao[fff.x]){q.push(fff);gao[fff.x]=1;if((fff.x==0)||(fff.x == (0xFFFF)))break;}} if((fff.x==0)||(fff.x == (0xFFFF)))break;}if(q.empty())cout<<"Impossible\n";elsecout<<fff.rounds<<endl;}void solve(){memset(gao,0,sizeof(gao));bfs();}int main(){ans = 0;for(int i=0;i<4;i++){cin >> g[i];for(int j = 0;j<4;j++){if(g[i][j]=='b')ans|=(0x1<<(i*4+j));}}//init();solve();return 0;}
0 0
- poj 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- POJ 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- POJ-1753-Flip Game
- POJ 1753: Flip Game
- POJ 1753 Flip Game
- POJ 1753 Flip Game
- poj 1753 Flip Game
- poj 1753 Flip Game
- POJ 1753 Flip Game
- POJ-1753-Flip Game
- Poj 1753 Flip Game
- POJ 1753 Flip Game
- POJ-1753-Flip Game
- HDU 5344 MZL's xor(异或运算)
- 组合数学之母函数问题
- Java抽象类和接口
- xhprof(PHP性能分析工具)
- pyqt 用py2exe打包出错的解决办法
- POJ 1753 Flip Game
- iOS程序启动的时候隐藏状态栏,启动完成之后显示状态栏
- centos yum安装mysql
- 苹果iPhone6 Plus坠海两月被找回
- hdoj 2717 Catch That Cow
- string的erase方法
- 七把利器助你打造充满竞争力的商业模式
- 东软毕设
- HDOJ 1235 统计同成绩学生人数(水)