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 ...
随机推荐
- [bzoj3143] [洛谷P3232] [HNOI2013] 游走
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- CSS-12-盒子模型
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ubuntu安装openjdk8
sudo apt-get update sudo apt-get install openjdk-8-jdk 通过 which java 找到java安装路径 添加环境变量 sudo vim ~/.b ...
- Exception | java.security.NoSuchProviderException: no such provider: BC
背景 今天在用PGP key做JWT签名和验签的时候,转换报了如下错误: org.bouncycastle.openpgp.PGPException: exception on setup: java ...
- docker学习笔记1认识docker
简介 Docker是一个开源的应用容器,开发者可以打包其应用以及依赖到一个可移植的容器当中.当然容器与容器之间不存在任何接口,完全独立.最大程度的解决了我的软件只能不能在你的电脑上运行的尴尬局面.开发 ...
- Java爬虫框架之WebMagic
一.介绍 WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个高效.易维护的爬虫. 二.如何学习 1.查看官网 官网地址为:http://webmagic.io ...
- 实验13:VLAN/TRUNK/VTP/
实验10-1: 划分VLAN Ø 实验目的通过本实验,读者可以掌握如下技能:(1) 熟悉VLAN 的创建(2) 把交换机接口划分到特定VLAN Ø 实验拓扑 实验步骤要配置VLAN,首先要 ...
- Netty学习(1):IO模型之BIO
概述 Netty其实就是一个异步的.基于事件驱动的框架,其作用是用来开发高性能.高可靠的IO程序. 因此下面就让我们从Java的IO模型来逐步深入学习Netty. IO模型 IO模型简单来说,就是采用 ...
- JsonCpp中文unicode编码问题
用JsonCpp解析C++中文unicode编码,转换问题,今天找打了一种解决办法 这个问题画了不少时间,贴出来供大家参考 在json_reader.cpp中找到codePointToUTF8函数,添 ...
- 位运算在状态压缩DP中的应用
一.判断一个数字X的i位是不是1 方法: << (i-)) & x > ) 原理: 1左移(i-1)位,相当于制造了一个就i位上是1其他位都是0的一个二进制数.将这个数 ...