POJ 1753 BFS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 44450 | Accepted: 19085 |
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
//求最少反转次数可以bfs,因为只有4*4所以可以把每行的状态压缩一下用四维数组来标记是否出现过此状态。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int mp[][],vis[<<][<<][<<][<<];
struct Node{
int mp[][],tim;
}no1,no2;
void Make(Node a){
int sta[]={};
for(int i=;i<;i++){
for(int j=;j<;j++)
sta[i]|=(a.mp[i][j]<<(-j));
}
vis[sta[]][sta[]][sta[]][sta[]]=;
}
bool Chack(Node a){
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a.mp[i][j]!=a.mp[][]) return false;
return true;
}
bool Vis(Node a){
int sta[]={};
for(int i=;i<;i++){
for(int j=;j<;j++)
sta[i]|=(a.mp[i][j]<<(-j));
}
if(vis[sta[]][sta[]][sta[]][sta[]]) return ;
return ;
}
Node Change(int x,int y,Node a){
for(int i=;i<;i++){
for(int j=;j<;j++){
if(i==x&&j==y){
a.mp[i][j]=!a.mp[i][j];
if(i+<=) a.mp[i+][j]=!a.mp[i+][j];
if(i->=) a.mp[i-][j]=!a.mp[i-][j];
if(j+<=) a.mp[i][j+]=!a.mp[i][j+];
if(j->=) a.mp[i][j-]=!a.mp[i][j-];
a.tim++;return a;
}
}
}
}
int Bfs(){
memset(vis,,sizeof(vis));
queue<Node>q;
no1.tim=;
q.push(no1);
Make(no1);
while(!q.empty()){
no1=q.front();q.pop();
if(Chack(no1)) return no1.tim;
for(int i=;i<;i++){
for(int j=;j<;j++){
no2=Change(i,j,no1);
if(Vis(no2)) continue;
Make(no2);
q.push(no2);
}
}
}
return -;
}
int main()
{
char s[];
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++)
no1.mp[i][j]=(s[j]=='b'?:);
}
int ans=Bfs();
if(ans==-) printf("Impossible\n");
else printf("%d\n",ans);
return ;
}
POJ 1753 BFS的更多相关文章
- POJ 1753 bfs+位运算
T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...
- [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)
先列出题目: 1.POJ 1753 POJ 1753 Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- poj 1753 2965
这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...
- POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题
http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753
下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
随机推荐
- OpenMPI源码剖析3:try_kill_peers 和 ompi_rte_abort 函数
接着上一篇的疑问,我们说道,会执行 try_kill_peers 函数,它的函数定义在 ompi_mpi_abort.c 下: // 这里注释也说到了,主要是杀死在同一个communicator的进程 ...
- LeetCode 700——二叉搜索树中的搜索
1. 题目 2. 解答 如果根节点为空,直接返回 NULL.如果根节点非空,从根节点开始循环查找,直到节点为空. 如果待查找的值大于当前节点值,节点指向右孩子: 如果待查找的值小于当前节点值,节点指向 ...
- “Hello world!”团队第三周贡献分规则
一.贡献规则制定: (1)基础分:9 , 9 , 8 , 7 , 7 , 7 , 6(按在本次编程中承担模块的重要度制定,某一模块重要度的认定通过组内开会讨论决定) (2)会议分:每人没出勤一次会议记 ...
- Beta阶段项目展示博客
Beta阶段项目展示 团队成员的简介 详细见团队简介 角色 姓名 照片 项目经理,策划 游心 策划 王子铭 策划 蔡帜 美工 赵晓宇 美工 王辰昱 开发.架构师 解小锐 开发 陈鑫 开发 李金奇 开发 ...
- iOS开发CAAnimation类动画, CATransition动画
#pragma mark - CAAnimation类.实现动画 #pragma mark ** CABasicAnimation 动画 - (IBAction)handleCABasicAnimat ...
- TCP系列39—拥塞控制—2、拥塞相关算法及基础知识
一.拥塞控制的相关算法 早期的TCP协议只有基于窗口的流控(flow control)机制而没有拥塞控制机制,因而易导致网络拥塞.1988年Jacobson针对TCP在网络拥塞控制方面的不足,提出了& ...
- css深入理解之 border
一 border-width不支持百分比值 1 不符合客观逻辑 2 w3成都一种约定吧 3 边框本身就像是一个包裹内容的界限 类似的还有outline,box-shadow text-shadow均不 ...
- union查询
select id, uid, money, FROM_UNIXTIME(created) as created, type FROM ( #type=1是 cjw_finance_bonus ...
- Thread.Sleep(0)
理解Thread.Sleep函数 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢? 思考下面这两个问题: 1.假设现在是 2008-4- ...
- 苹果IOS、安卓推送功能开发
IOS推送开发:以下是基于开源javapns推送开发1.DerInputStream.getLength(): lengthTag=111, too big.先排除是否由于打包时证书 .p12 文件被 ...