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! ...
随机推荐
- 动态链接库加载出错:cannot restore segment prot after reloc: Permission denied
在执行可执行程序时,出现动态链接库加载出错:cannot restore segment prot after reloc: Permission denied. 主要是由于Linux 内核中提供的强 ...
- [Guava学习笔记]Collections: 集合工具类
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861431.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- win7中CIFS挂载和解挂
1.win7挂载CIFS共享至Z盘指令(用户名:test,密码:123456): net use Z: \\192.168.8.63\ygcd\duanxiuwei 123456 /USER:test ...
- Kail安装Parallels tools
今天在安装虚拟机里面安装kail,在安装虚拟机提供的tools时候提示没有权限,如图: 后面经过自己证实首先tools是挂载的cdrom,在这样挂载下是没有写权限的(类似于windows下面读取光盘光 ...
- 中科红旗倒下,谁来挑战windows
中科红旗解散 国产操作系统从此梦断 2月10日,关门上锁的中科红旗北京总部大门上粘贴了一张最新公告,这张公告彻底击破了那些仍然坚守公司工作的员工“拯救中国红旗”的希望.该公告称:因北京中科红旗软件技术 ...
- 【转】分享10VPN
以下介绍的vpn,都是有免费流量赠送的免费vpn,完全不免费的不在之列. 免费vpn因为用的人比较多,所以高峰时段可能会有点慢,但是人少时,还是比较顺畅的.对于偶尔浏览外网,看看新闻的同学来说,免费v ...
- 解决CxGrid Filter 后,通过 Dataset 循环时得出的结果与 Grid显示不同步的问题.
// 方案1: 强制cxgrid 使用 dataset的 Filter GridMaster.DataController.Filter.AutoDataSetFilter := True; /// ...
- DOM基础总结
一.简介 1.什么是DOM 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式 ...
- Oracle中的USEREVN()
Oracle中的USEREVN() 1.USEREVN() 返回当前用户环境的信息,opt可以是: ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CL ...
- 【学习总结】 init & initWithFrame & initWithCoder
//当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame { if (se ...