1753 -- Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 48663 | Accepted: 20724 |
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
有一个 4*4 的矩阵,然后现在每个格子有两个颜色,要么是黑色,要么是白色,当你的手按动一个格子的时候,这个格子的上、下、左、右都会
改变原来的颜色,即由黑变白,由白变黑,现在问你的是,经过最少几次翻转之后可以使得这个 4*4 的矩阵变为全是白色的或者是全是黑色的。
#include<iostream>
using namespace std;
int color[];
int a[];
int bestx;
bool checkColor()
{
int a = color[];
for(int i=;i<;i++)
{
if(a != color[i])
{
return ;//存在不同色
}
}
return ;//同色
}
void BackTrack(int t,int pre)
{
if(checkColor() || t>=)//如果同色
{//更新解
if(checkColor())
{
if(t-<bestx)
bestx = t-;
}
return;
} else{
for(int i=pre;i<;i++)
{
if(a[i] == )//没有翻过
{
//进行翻牌
a[i] = ;
color[i] = -color[i];
if(i->=) color[i-] = -color[i-];
if(i+<=) color[i+] = -color[i+];
if(i->= && (i-)%!=) color[i-] = -color[i-];
if(i+<= && (i+)%!=) color[i+] = -color[i+]; if(t<bestx)//剪枝
BackTrack(t+,i+); a[i] = ;//回溯
color[i] = -color[i];
if(i->=) color[i-] = -color[i-];
if(i+<=) color[i+] = -color[i+];
if(i->= && (i-)%!=) color[i-] = -color[i-];
if(i+<= && (i+)%!=) color[i+] = -color[i+];
}
} } }
int main()
{
while(true)
{
bestx=;
for(int i=;i<;i++)
a[i]=;
char c;
for(int i = ;i<;i++)
{
if(i % == && i!=)
{
c = getchar();
}
c = getchar();
if(c == EOF) return ;
if(c == 'b') color[i] = ;
else if(c == 'w') color[i] = ; }
BackTrack(,);
if(bestx == )
cout<<"Impossible"<<endl;
else
cout<<bestx<<endl;
} return ;
}
1753 -- Flip Game的更多相关文章
- 枚举 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 ...
- OpenJudge/Poj 1753 Flip Game
1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- POJ 1753 Flip Game 状态压缩,暴力 难度:1
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4863 Accepted: 1983 Descript ...
- poj 1753 Flip Game
点击打开链接 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25674 Accepted: 1109 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
随机推荐
- vue 导航守卫
1.全局守卫(在所有路由展示前触发)//在main.js中 router.beforeEach((to, from, next) => { to 即将要进入的到的路由,值为路由 ...
- debian设置limits.conf
最近已经把自己的游戏框架主要功能完成得差不多了,决定将自己的开发环境从debian7升级到debian9,不然太多第三方依赖都跟不上了.debian10刚出来,MongoDB还没适配,所以暂不考虑. ...
- RabbitMQ消息分发轮询
一,前言 如果我们一个生产者,对应多个消费者,rabbitmq 会发生什么呢 二,消息分发轮询 前提条件:1个生产者 ----> 多个消费者,且no_ack=True (启动三次生产者) ① ...
- asterisk搭建goip
利用asterisk来搭建goip使用,步骤如下: 第一步:先需要搭建centos服务器并优化 搭建centos服务器就省略: 优化centos服务器,进行如下步骤: 关闭防火墙:systemctl ...
- Codeforces 1149 B - Three Religions
B - Three Religions 思路:dp dp[i][j][k]:a的前i个和b的前j个和c的前k个能构成的最前面的位置 删字符时状态不用改变,加字符时只会改变1*250*250个状态 代码 ...
- 洛谷P2664 树上游戏——点分治
原题链接 被点分治虐的心态爆炸了 题解 发现直接统计路径上的颜色数量很难,考虑转化一下统计方式.对于某一种颜色\(c\),它对一个点的贡献为从这个点出发且包含这种颜色的路径条数. 于是我们先点分一下, ...
- Beyond Compare 4 使用30天后过期续用方法
Beyond Compare 4 使用30天后过期续用方法 windows上的Beyond Compare 4软件过期了,两个方法: 方案一: 找到Beyond Compare 4安装目录,安装时默认 ...
- [BZOJ 1146] [CTSC2008]网络管理Network(树状数组+主席树)
题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...
- 【leetcode】1288. Remove Covered Intervals
题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...
- mysql关于索引的一些零碎知识点(持续更新)
1.is null可以使用索引(网上很多文章存在误导,这个确实可以使用索引),is not null无法使用索引. 2.为什么重复数据较多的列不适合使用索引? 假如索引列TYPE有5个键值,如果有1万 ...