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 ...
随机推荐
- LeetCode 240——搜索二维矩阵 II
1. 题目 2. 解答 2.1. 方法一 从矩阵的左下角开始比较 目标值等于当前元素,返回 true: 目标值大于当前元素,j 增 1,向右查找,排除掉此列上边的数据(都比当前元素更小): 目标值小于 ...
- Python高级编程-多线程
(一)进程线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行 ...
- Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again YUM报错
1.挂盘 ----- 2.# mount /dev/sr0 /media/ mount: block device /dev/sr0 is write-protected, mounting ...
- 简单DP
1.一只小蜜蜂 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input输入数据的第一行是一个整数N,表 ...
- 算法与数据结构5.2 Bubble Sort
★实验任务 给定一个 1~N 的排列 P,即 1 到 N 中的每个数在 P 都只出现一次. 现在要 对排列 P 进行冒泡排序,代码如下: for (int i = 1; i <= N; ++i) ...
- Scrum 冲刺博客,项目总结
1.各个成员在 Alpha 阶段认领的任务 数据库环境的搭建,连接数据库:张陈东芳 数据库语句sql语句:张陈东芳 商品实体类的实现:吴敏烽 获取所有商品信息的实现:吴敏烽 根据商品编号获得商品资料: ...
- Mysql8 忘记Root密码(转)
第一步:修改配置文件免密码登录mysql vim /etc/my.cnf 1.2 在 [mysqld]最后加上如下语句 并保持退出文件: skip-grant-tables 1.3 重启mysql服务 ...
- fcntl函数详解
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control头文件: #include <unistd.h> #include & ...
- Centos安装TFTP/NFS/PXE服务器网络引导安装系统
客户端网卡要求支持以PXE启动,配置都在服务端进行,通过PXE网络启动安装系统流程: 客户端以PXE启动发送DHCP请求: 服务器DHCP应答,包括客户端的IP地址,引导文件所在TFTP服务器: 客户 ...
- MySQL & export
MySQL & export mysql export table form command line https://cn.bing.com/search?q=mysql%20export% ...