POJ 1753 Flip Game
来源:互联网 发布:混沌优化算法优缺点 编辑:程序博客网 时间:2024/06/03 16:26
这是一个枚举的题,在同学的帮助下终于弄懂了啊。。。一点一点的搜索就可以过。。。。。。
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 24504 Accepted: 10531
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 <stdio.h>#define MIN 1 << 28int a[17],b,w,min,count;void up(int i){ if(i-4>=0) { if(a[i-4]==0) { a[i-4]=1; w--; b++; } else { a[i-4]=0; w++; b--; } }}void down(int i){ if(i+4<16) { if(a[i+4]==0) { a[i+4]=1; w--; b++; } else { a[i+4]=0; w++; b--; } }}void left(int i){ if(i-1>=0&&i/4==(i-1)/4) { if(a[i-1]==0) { a[i-1]=1; w--; b++; } else { a[i-1]=0; w++; b--; } }}void right(int i){ if(i+1<16&&i/4==(i+1)/4) { if(a[i+1]==0) { a[i+1]=1; w--; b++; } else { a[i+1]=0; w++; b--; } }}void dfs(int i){ int j; if(i == -1) { for(j = i+1; j < 16; j++) { dfs(j); } } else { if((b == 0 || w == 0) && min>count) { min = count; return; } if(i<16 && i>=0) { if(a[i] == 0) { a[i] = 1; b++; w--; } else { a[i] = 0; b--; w++; } } up(i); down(i); left(i); right(i); count++; if((b == 0 || w == 0) && min>count) { min = count; } else { for(j = i+1; j < 16; j++) { dfs(j); } } if(i<16 && i>=0) { if(a[i] == 0) { a[i] = 1; b++; w--; } else { a[i] = 0; b--; w++; } } up(i); down(i); left(i); right(i); count--; }}int main(){ int i, j, t; char s[10][10]; while(~scanf("%s",s[0])) { b = w = t = 0; min = MIN; for(i = 1; i < 4; i++) scanf("%s",s[i]); for(i = 0; i < 4; i++) for(j = 0; j < 4; j++) { if(s[i][j] == 'b') { a[t++] = 1; b++; } else if(s[i][j] == 'w') { a[t++] = 0; w++; } } dfs(-1); if(min<MIN) { printf("%d\n",min); } else { printf("Impossible\n"); } } return 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
- 内核模块的Makeflie编写
- PHP中$_SERVER的详细参数与说明
- 快速构建MMO服务器框架(六)asio稍微复杂一些的例子:高并发echo
- Android程序的目录结构
- 提高vmware虚拟机运行速度
- POJ 1753 Flip Game
- android 核心技术分析-全篇
- 女性必读的十本书
- spring中bean的设计模式
- #pragma once与 #ifndef的区别
- Struts 2验证框架出错:403 for URL:http:////www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd
- 如何编译一个内核 - Ubuntu方式
- android 自定义 radiobutton 文字颜色随选中状态而改变
- button和submit的区别及使用js实现页面跳转的方式