Flip Game poj1753
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32961 | Accepted: 14407 |
Description
- 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
Output
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
Source
dfs枚举
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int Map[][],a[]={,,,,-},b[]={,,,-,};
int flg,step; int judge(int Map[][])
{
int i,j;
for(int i=;i<=;i++)
for(j=;j<=;j++)
{
if(Map[i][j]!=Map[][])
return ;
}
return ;
} int flip(int r,int c)
{
for(int i=;i<;i++)
{
if(Map[r+a[i]][c+b[i]]==)
Map[r+a[i]][c+b[i]]=;
else
Map[r+a[i]][c+b[i]]=;
}
} void dfs(int r,int c,int deep)
{
int i,j;
if(deep==step)
{
flg=judge(Map);
return;
}
if(flg== || r>)
{
return;
}
flip(r,c);
if(c<)
dfs(r,c+,deep+);
else
dfs(r+,,deep+);
flip(r,c);
if(c<)
dfs(r,c+,deep);
else
dfs(r+,,deep);
return;
} int main()
{
int i,j;
char k;
memset(Map,,sizeof(Map));
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
scanf("%c",&k);
if(k=='b')
Map[i][j]=;
}
getchar();
}
for(step=;step<=;step++)
{
dfs(,,);
if(flg)
break;
}
if(flg)
printf("%d\n",step);
else
printf("Impossible\n");
return ;
}
Flip Game poj1753的更多相关文章
- dfs关于按钮问题(flip游戏POJ1753)以及和bfs的区别+板子
DFS深度搜索:之前一直和bfs的用法搞不太清楚:写了题才能慢慢参透吧,看了别的博客的代码,感觉能更好理解dfs在图中的应用: 这个题目的意思是一个人去救另一个人,找出最短的寻找路径: #includ ...
- 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 ...
- POJ1753——Flip Game
Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...
- 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 ...
随机推荐
- cvCreateImage函数说明(转载)
cvCreateImage是openCV中的一个函数.OpenCV是Intel公司支持的开源计算机视觉库. cvCreateImage:创建首地址并分配存储空间 IplImage* cvCrea ...
- ckeditor添加插入flv视频的插件
首发:个人博客,更新&纠错&回复 昨天写在网页中播放flv的博文的时候,想在博文中插入视频,但是发现无法实现.因为用的编辑器是ckeditor,决定自己写个插件插入视频.官方的教程在这 ...
- MVC架构剖析--ASP.NET MVC图解(二)
- HorizontalScrollView的配置
package com.exmple.jinritoutiao; import java.util.ArrayList;import java.util.Calendar;import java.ut ...
- java中==与equal()方法的区别
==比较的是对象的地址,也就是是否是同一个对象: equal比较的是对象的值. Integer r1 = new Integer(900);//定义r1整型对象Integer r2 = new Int ...
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- zabbix源码安装
Zabbix通过C/S模式采集数据,通过B/S模式在web端展示和配置. 被监控端:主机通过安装agent方式采集数据,网络设备通过SNMP方式采集数据 Server端:通过收集SNMP和agent发 ...
- PL/SQL显示行号和高亮当前行
PL/SQL Developer 如何显示行号: PL/SQL Developer 高亮当前行: OK!
- PHP最原始的上传文件函数
<?php $upload_file=$_FILES['upload_file']['tmp_name']; $upload_file_name=$_FILES['upload_file'][' ...
- 还原数据库,恢复SQLSERVER登录名的问题
还原SQLSERVER数据库,原来的数据库的于当前SQLSERVER同名用户就不能再登录了,原因是当前SQLSERVERD的master数据库的sysxlogins表的的sid与还原后的数据库的sys ...