POJ1753——Flip Game
Flip Game
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).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
题目大意:
一个四乘四的棋盘,上面填满了白或黑子,每次可以选择一个子翻转颜色,翻转的同时其上下左右也同时翻转,输出最少次数,使棋盘所有颜色相同。(考虑不可能的情况)
大体思路:
有16个格子,每个格子有黑白两种状态。所以可以用一串二进制来表示状态。
转换成十进制的后,每种状态都可表示为范围为0-65535的唯一整数。
用bfs来进行搜索,第一次出现0或65535(全白或全黑)就停止搜索,返回dis。
使用位运算异或来翻转棋子,0^1=1,1^1=0
#include<stdio.h>
int vis[]= {},dis[];//0-65535 vis为标记是否已经加入队列 dis表示结点所在层数
int c=,queue[*];//c用来存最终次数,queue为bfs的队列
int fz(int a,int xy)//通过位运算来翻转棋盘 xy表示翻转的中间子
{
int tmp[]= {,,,,},i;
tmp[]=tmp[]<<(xy-);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy-);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy-);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy+);
for (i=; i<=; i++)
a=a^tmp[i];
return a;
}
int bfs(int a)
{
int i,t,front=,rear=,tmp=,ok;
dis[a]=,vis[a]=;
queue[front]=a;
while (front<rear)
{
for (i=; i<=; i++)
{
tmp=fz(queue[front],i);
if (vis[tmp]==)
{
queue[rear]=tmp;
vis[tmp]=;
dis[rear++]=dis[front]+;
if (tmp==||tmp==)
{
c=dis[front]+;//找到结果 将步数存入c
return ;
}
}
}
front++;
}
return ;//队列搜素完毕后 仍未找到 则impossible
}
int main()
{
char tmp[];
int k,a,i,t=;
for (i=; i<=; i++)
{
scanf("%c",&tmp[i]);
if (i%==&&i!=) getchar();
}
k=,a=;
for (i=; i>=; i--)//处理输入,转换成整数
{
if (tmp[i]=='b') a+=k;
k*=;
}
if (a==||a==)//判断初始状态是否为完成态
{
printf("0\n");
return ;
}
t=bfs(a);
if (t==) printf("Impossible\n",c);
else printf("%d\n",c);
return ;
}
POJ1753——Flip Game的更多相关文章
- poj1753 Flip Game(BFS+位压缩)
题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...
- poj1753,Flip Game,ArrayDeque<Node>
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30449 Accepted: 13232 Descr ...
- POJ1753 Flip Game(bfs、枚举)
链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...
- poj1753 Flip Game
题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...
- POJ-1753 Flip Game---二进制枚举子集
题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...
- POJ-1753 Flip Game (BFS+状态压缩)
Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...
- POJ1753 Flip Game(位运算+暴力枚举)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推
题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)
题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...
随机推荐
- GTEST-ASSERT出错
ASSERT_TRUE(1==1); ASSERT_EQ(1,1); 会给出以下错误: "cfunctest_normal.cpp", line 121.9: 1540-0258 ...
- qml实现自定义标题栏按钮
自定义的标题栏按钮是由Rectangle来实现的,在Rectangle中需要4张图片,分别在鼠标进入按钮区.鼠标离开按钮区(正常状态下).鼠标按下和鼠标释放时所加载的图片.下面是实现自定义按钮的代码( ...
- javascript多线程简介
讲多线程之前,我们先了解一下JS的事件机制 浏览器运行时,脚本必须定期让位给UI进程进行来维持网页的响应,闲置太长时间的脚本可能会被浏览器当成失控脚本,进而造成假死或弹窗 事件触发的设计javascr ...
- Http UDP还是TCP
http://1024monkeys.wordpress.com/2014/04/01/game-servers-udp-vs-tcp/ 在编写网络游戏的时候,到底使用UDP还是TCP的问题迟早都要面 ...
- php入门变量之字符串
字符串只是一块用引号括起来的字符:字母.数字.空格.标点符号,等等. 下面列出的全都是字符串: 'Huige' "In watermelon sugar" '100' 'Augus ...
- Oracle RAC LoadBalance
LoadBalance 就是把负载平均的分配到集群中的各个节点,从而提高整体的吞吐能力. Oracle 10g RAC 提供了两种不同的方法来分散负载: 通过Connection Balancing, ...
- python--gevent协程及协程概念
何为协程 协程,又称微线程.英文名Coroutine. 协程最大的优势就是协程极高的执行效率.因为子程序切换不是线程切换,而是由程序自身控制,因此,没有线程切换的开销,和多线程比,线程数量越多,协程的 ...
- 拥抱ARM妹子第二季 之 序:我和春天有个约会 - 生命的萌芽
春姑年轻轻的吻了一下小穆妹纸的额头!从沉睡中苏醒的小穆妹纸,缓缓伸了个懒腰--- 啊-- 睡得真香! 等--等-等-!好像和童话故事里的情节不一样,应该由王子我来亲吻睡梦中的妹纸才能醒!!-- 强 ...
- EXTJS 4.2 资料 控件之 Store 用法
最近工作,发现在Extjs中自定义Store的功能挺多,特意在此做笔记,几下来,具体代码如下: 1.定义Store //定义Store var ItemSelectorStore = new Ext. ...
- SharpDeveloeper开发ASP.NET MVC汗流浃背
今天好不容易休息了一天,上网狂了一圈,突然想起了以前的一个轻量级的开发工具"SharpDeveloper",于是就下载试着来开发一下ASP.NET,但是老魏没有想到的是,虽然官方提 ...