POJ-1753 Flip Game (BFS+状态压缩)
2024-10-19 00:27:23
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:
- 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).
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
bwwb
bbwb
bwwb
bwww
Sample Output
4 题目大意:在一个4x4的棋盘上,摆满了黑白两种颜色的棋子。当翻转一颗棋子的时候,与它相邻的上下左右四个棋子也随之翻转。问将整个棋盘置为同一种颜色最少需要翻转几次。
题目分析:这道题的数据量不大且固定不变,2^16个状态,又因为让找最小步骤数,所以就敲定用BFS。接下来就是状态如何表示了,很显然了,4行用四个不超过16的二进制数表示即可。至于状态转移,用位运算异或就能轻松完成。 代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int a,b,c,d,t;
node(){}
node(int a1,int b1,int c1,int d1,int t1):a(a1),b(b1),c(c1),d(d1),t(t1){}
bool operator < (const node &a) const {
return t>a.t;
}
};
char p[][];
int vis [][][][];
void bfs()
{
int temp[],a,b,c,d;
for(int i=;i<;++i){
temp[i]=;
for(int j=;j<;++j){
if(p[i][j]=='b')
temp[i]=temp[i]*+;
else
temp[i]=temp[i]*+;
}
}
priority_queue<node>q;
memset(vis,,sizeof(vis));
vis[temp[]][temp[]][temp[]][temp[]]=;
q.push(node(temp[],temp[],temp[],temp[],));
while(!q.empty()){
node u=q.top();
q.pop();
if(u.a==&&u.b==&&u.c==&&u.d==){
printf("%d\n",u.t);
return ;
}
if(!u.a&&!u.b&&!u.c&&!u.d){
printf("%d\n",u.t);
return ;
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
a^=(<<i);
b^=(<<i);
if(i>)
a^=(<<(i-));
if(i<)
a^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
b^=(<<i);
a^=(<<i);
c^=(<<i);
if(i>)
b^=(<<(i-));
if(i<)
b^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
c^=(<<i);
b^=(<<i);
d^=(<<i);
if(i>)
c^=(<<(i-));
if(i<)
c^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
d^=(<<i);
c^=(<<i);
if(i>)
d^=(<<(i-));
if(i<)
d^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
}
printf("Impossible\n");
}
int main()
{
for(int i=;i<;++i)
scanf("%s",p[i]);
bfs();
return ;
}
最新文章
- Smart_Script
- iOS 单元测试之XCTest详解(一)
- 利用GCC编译器生成动态链接库和静态链接库
- INFORMATICA 的部署实施之 BACKUP&;RESTORE
- WordPress Tweet Blender插件跨站脚本漏洞
- 用java代码发送http请求
- CSDN博客投票活动开始了
- P2598 [ZJOI2009]狼和羊的故事(网络流)
- 大面积project.pbxproj冲突问题解决
- JavaSE| 集合
- java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化
- paramiko 实现ssh登录和sftp登录
- TCP/IP 协议图--TCP/IP 基础
- tomcat启动 报org.apache.catalina.LifecycleException异常
- AngularJS 和 Electron 构建桌面应用
- windows配置redis(转)
- Tensorflow样例代码分析cifar10
- unity状态机实现
- 【week2】 构建之法 读后感及问题
- stegsolve的功能