poj 1753 Flip Game(状压DP)
来源:互联网 发布:julius it 编辑:程序博客网 时间:2024/06/06 18:13
题目链接:http://poj.org/problem?id=1753
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 43371 Accepted: 18708
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
Northeastern Europe 2000
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
题意:给你一个4*4的格子,将白块全变成黑或者将黑全变成白,问最小的反转数,反转当前个,它的上下左右都会跟着反转
解析:枚举第一行所有状态,然后根据第一行进行翻下一行,最后判断最后是否满足全为黑(白),进而更新ans
代码:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<cmath>#include<map>#include<vector>#include<stack>#define N 6using namespace std;const int INF = 0x3f3f3f3f;int mp1[N][N], g[N][N], mp2[N][N];int nx[4] = {-1, 0, 0, 1};int ny[4] = {0, -1, 1, 0};void fun(int i, int j) //翻转它的上下左右{ g[i][j] = !g[i][j]; for(int k = 0; k < 4; k++) { int x = i + nx[k]; int y = j + ny[k]; if(x < 0 || x > 3 || y < 0 || y > 3) continue; g[x][y] = !g[x][y]; }}int solve1(int cur) //全为白{ int ans = 0; memcpy(g, mp1, sizeof(mp1)); for(int i = 0; i < 4; i++) { if((cur>>i)&1) { ans++; fun(0, i); } } for(int i = 1; i < 4; i++) { for(int j = 0; j < 4; j++) { if(g[i - 1][j]) { ans++; fun(i, j); } } } for(int i = 0; i < 4; i++) if(g[3][i]) return INF; return ans;}int solve2(int cur) //全为黑{ int ans = 0; memcpy(g, mp2, sizeof(mp2)); for(int i = 0; i < 4; i++) { if((cur>>i)&1) { ans++; fun(0, i); } } for(int i = 1; i < 4; i++) { for(int j = 0; j < 4; j++) { if(g[i - 1][j]) { ans++; fun(i, j); } } } for(int i = 0; i < 4; i++) if(g[3][i]) return INF; return ans;}int main(){ char ch; memset(mp1, 0, sizeof(mp1)); memset(mp2, 0, sizeof(mp2)); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { scanf(" %c", &ch); if(ch == 'b') mp1[i][j] = 1; else mp2[i][j] = 1; } } int n = 1<<4, ans = INF; for(int i = 0; i < n; i++) { ans = min(ans, solve1(i)); ans = min(ans, solve2(i)); } if(ans == INF) puts("Impossible"); else printf("%d\n", ans); return 0;}
0 0
- poj 1753 Flip Game (状压DP)@
- poj 1753 Flip Game(状压DP)
- (POJ 1753)Flip Game <状压+BFS>
- POJ 1753(Flip Game)
- Flip Game(POJ 1753)
- Flip Game(POJ-1753)
- 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
- 深入浅出java入门 之 变量与常量(3)
- Cell for 3dsMax demo
- osg杂记一:osg::MatrixTransform和osg::PositionAttitudeTransform异同
- 1.1.PHP7.1 女神级教程-初识女神(认识PHP 7.1)
- golang语言并发与并行——goroutine和channel的详细理解(一)
- poj 1753 Flip Game(状压DP)
- springmvc 数据传输跨域问题 access control allow origin——JSONP数据格式解决
- java的异常处理
- Linux中最危险的是个命令(很有趣呦~)(链接:http://os.51cto.com/art/201408/448756.htm)
- IOS中多个网络请求问题
- 多线程的实现方法
- (1)创建解决方案项目
- iOS学习笔记-050.Modal模态
- mybatis高级查询-一对一查询