POJ 1753 Flip Game
来源:互联网 发布:淘宝收藏图片在线制作 编辑:程序博客网 时间:2024/06/16 20:03
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 31424 Accepted: 13656
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
稀里哗啦写了一大堆,最后发现就是十六步枚举,一维二维就无所谓了!!!
#include<iostream>using namespace std;int chess[17];int k;int judge(){int i;for(i=2;i<=16;i++)if(chess[i]!=chess[i-1]) return 0;return 1;}void convert(int i){chess[i]^=1;if(i%4!=1) chess[i-1]^=1;if(i%4!=0) chess[i+1]^=1;if(i>4) chess[i-4]^=1;if(i<13) chess[i+4]^=1;}void bfs(int j,int now,int total)//现在到第j个棋子了,翻转了now次,总共翻转了total次。{int i;if(now==total){k=judge();return;}for(i=j+1;i<=16;i++){convert(i);bfs(i,now+1,total);if(k) return;convert(i);//还原。}}int main(){int i;char ch;for(i=1;i<=16;i++)//输入,用0代替w,用1代替b。{cin>>ch;if(ch=='w') chess[i]=0;else chess[i]=1;}k=judge();if(k) cout<<0<<endl;else{for(i=1;i<=16;i++)//最多翻转16次,枚举。{k=0;bfs(0,0,i);if(k) break;}if(k) cout<<i<<endl;else cout<<"Impossible\n";}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
- Android自定义属性,format详解
- linux用户的相互切换
- 【Android基础】Spinner组件使用
- 算法导论之计数排序
- URAL 1326. Bottle Taps(简单的状压dp)
- POJ 1753 Flip Game
- 多字节字符与宽字符重新认识
- 在多线程中使用静态方法是否有线程安全问题
- Spring 学习记录 冷兵器时代的故事
- C#解决UI主线程不响应
- nth_element 用法简介
- poj 3820 牡丹江区域赛 (树的直径+bfs)
- 想移植一个recovery
- php 与 html实现 订餐网页