poj 1753 Flip Game
来源:互联网 发布:怎么在淘宝买东西便宜 编辑:程序博客网 时间:2024/06/06 04:48
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 40117 Accepted: 17421
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
这一题我是看着题解做的,
下面是代码
#include <stdio.h>#include <algorithm>#include <iostream>using namespace std;char ch;bool a[6][6];int i;int fx[5] = {0,1,-1,0,0};int fy[5] = {0,0,0,1,-1};int fid;int isgoal() //看是否全为同一个颜色{ int w,j; for(j = 1;j <= 4; j++) { for(w = 1;w <= 4; w++) { if(a[j][w] != a[1][1]) { return 0; } } } return 1;}void flip(int x,int y) //把所有上下左右改变{ int j; for(j = 0;j < 5; j++) { int xx = x+fx[j]; int yy = y+fy[j]; a[xx][yy] = !a[xx][yy]; }}void dfs(int x,int y,int d){ if(d == i) //只有当前步数才能比较 { fid = isgoal(); return; } if(fid == 1) return ; if(x == 5) return ; flip(x,y); if(y < 4) dfs(x,y+1,d+1); //步数的增加 else dfs(x+1,1,d+1); flip(x,y); if(y < 4) dfs(x,y+1,d); //返回 else dfs(x+1,1,d);}int main(){ int j; fid = 0; for(i = 1;i <= 4; i++) //数组存颜色 { for(j = 1;j <= 4; j++) { cin >> ch; if(ch == 'b') { a[i][j] = true; } } } for(i = 0;i <= 16; i++) //i是进行的步数 { dfs(1,1,0); //进行深搜 if(fid) break; } if(fid) printf("%d\n",i); else printf("Impossible\n");}
代码菜鸟,如有错误,请多包涵!!
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
- 安卓二级缓存
- navicat for mysql的下载、安装与基本使用
- 多线程知识3-线程间通信
- Spark集群启动python shell错误: Could not resolve hostname localhost: Temporary failure
- 相关事件
- poj 1753 Flip Game
- poj 3368 Frequent values (RMQ或线段树)
- 多种CSMA策略的比较
- lua源码的研究入门之makefile
- iOS 常用的第三方库
- Label--自定义可调整内边距的Label
- 《GeekBand》c++面向对象高级编程02
- 谨以此文献给所有对目前工作不满意的宝宝们!
- BZOJ 1853: [Scoi2010]幸运数字