POJ 1753 Flip Game 暴力 深搜
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 59468 | Accepted: 24750 |
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
#include <iostream>
#include <cstdio> using namespace std; char str[];
int a[][];
int ans=; void flip(int x,int y)
{
a[x][y]=!a[x][y];
a[x-][y]=!a[x-][y];
a[x+][y]=!a[x+][y];
a[x][y-]=!a[x][y-];
a[x][y+]=!a[x][y+];
} bool same_color(void)
{
for(int i=;i<=;++i)
{
for(int j=;j<=;++j)
{
if(a[i][j]!=a[][])
{
return false;
}
}
}
return true;
} void dfs(int x,int y,int t)
{
// 判断是否同色,同色满足,返回
if(same_color())
{
if(ans>t)
{
ans=t;
}
return;
}
// 深搜截止条件
if(x>)
{
return;
} if(y==)
{
// 当前不反转
dfs(x+,,t);
// 当前反转
flip(x,y);
dfs(x+,,t+);
// 状态还原
flip(x,y);
}
else
{
dfs(x,y+,t);
flip(x,y);
dfs(x,y+,t+);
flip(x,y);
} } int main()
{
for(int i=;i<=;++i)
{
scanf("%s",str+);
for(int j=;j<=;++j)
{
// a->0,b->1
if(str[j]=='w')
{
a[i][j]=;
}
else
{
a[i][j]=;
}
//a[i][j]=str[j]-'a';
//printf("%d ",a[i][j]);
}
//printf("\n");
} dfs(,,); if(ans==)
{
printf("Impossible\n");
}
else
{
printf("%d\n",ans);
} return ;
}
POJ 1753 Flip Game 暴力 深搜的更多相关文章
- poj 1753 Flip Game(暴力枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52279 Accepted: 22018 Des ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ - 1753 Flip Game(状压枚举)
https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
- Poj(2488),按照字典序深搜
题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- POJ 1753 Flip Game (状压+暴力)
题目链接:http://poj.org/problem?id=1753 题意: 给你一个4*4的棋盘,上面有两种颜色的棋子(一种黑色,一种白色),你一次可以选择一个棋子翻转它(黑色变成白色,同理反之) ...
- POJ 1753 Flip Game 状态压缩,暴力 难度:1
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4863 Accepted: 1983 Descript ...
随机推荐
- get_字段_display()
gender_choices = ((1, '男'), (2, '女')) gender = models.IntegerField(verbose_name='性别', choices=gender ...
- 3种基础的 REST 安全机制
安全是 RESTful web service 的基石,我们主要讨论以下3种主要的方法: Basic authentication Oauth 2.0 Oauth 2.0 + JWT 1. Basic ...
- 异数OS 织梦师-云(五)-- 容器服务化,绿色拯救未来。
. 异数OS 织梦师-云(五)– 容器服务化,绿色拯救未来. 本文来自异数OS社区 github: https://github.com/yds086/HereticOS 异数OS社区QQ群: 652 ...
- python接口测试,mock模块基本使用介绍
mock简介 py3已将mock集成到unittest库中 为的就是更好的进行单元测试 简单理解,模拟接口返回参数 通俗易懂,直接修改接口返回参数的值 mock作用 解决依赖问题,达到解耦作用 当我们 ...
- Qt使用QAxObject快速批量读取Excel内容
网上各种教程用的方法主要是如下这一句: QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, ...
- 02--java--环境搭建
第一步,下载JDK 去ORACLE官网http://www.oracle.com下载 有安装版和绿色版,安装版一路下一步,绿色版解压缩压缩包就行了 安装版直接自动配置环境变量,绿色版需要自己配置环境变 ...
- JSTL (标准标签库)
JSTL(标准标签库) 作用: Web程序员能够利用JSTL和EL来开发Web程序,取代传统直接在页面上嵌入Java程序(Scripting)的做法,以提高程序的阅读性.维护性和方便性. 使用方法:J ...
- SpringBoot2.x整合JDBC及初始化data.sql和schema.sql脚本
今天在使用SpringBoot2.x版本整合JDBC时遇到了一些问题:由于我之前一直用SpringBoot1.5的版本,所以直接在yml里按照1.5的版本配置了属性,没想到2.x直接不能用了.首先是数 ...
- laravel 初学路由简单介绍
文档中的路由详细演示[初学laravel]对应laravel 的框架目录:routes/web.php 路由的格式一:Route::get($uri,$callback); 1.简单的浏览器输出 Ro ...
- Word文档分节设置页码
在一篇论文中需要将摘要和目录作为一部分设置罗马数字页码,正文部分设置阿拉伯数字页码. 大致效果如下图所示: 这里面用到了分节符,步骤如下: 1 :点击开始菜单栏下 显示/隐藏编辑标记 2:点击插入菜单 ...