POJ 2965 The Pilots Brothers' refrigerator (DFS)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 15136 | Accepted: 5660 | Special Judge |
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+--
----
----
-+--
Sample Output
6
1 1
1 3
1 4
4 1
4 3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; char str[][]; int change[]={ , , ,, ,,,
,, ,,,, , , }; int vis[];
int path[]; struct node{
int status;
int step;
}; void BFS(int curstate){
queue<node> q;
while(!q.empty())
q.pop();
node cur,next;
cur.status=curstate, cur.step=;
vis[cur.status]=;
q.push(cur);
while(!q.empty()){
cur=q.front();
q.pop();
for(int i=;i<;i++){
next.status=cur.status^change[i];
next.step=cur.step+;
if(!vis[next.status]){
vis[next.status]=;
path[next.status]=i;
q.push(next);
}
if(next.status==){
printf("%d\n",next.step);
int k=next.status;
while(k!=curstate){
printf("%d %d\n",path[k]/+,path[k]%+);
k=k^change[path[k]];
}
return ;
}
}
}
}
/*
int num[16]; //即求change[]的方法 void init(){
for(int i=0;i<16;i++){
num[i]=0;
for(int j=0;j<16;j++)
if((i/4==j/4) || (i%4==j%4))
num[i]=(num[i]<<1)+1;
else
num[i]<<=1;
}
}
*/
int main(){ //freopen("input.txt","r",stdin);
/*
init();
for(int i=0;i<16;i++)
printf("%d ",num[i]);
printf("\n");
*/
while(~scanf("%s",str[]+)){
for(int i=;i<;i++)
scanf("%s",str[i]+);
int status=;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
status<<=;
if(str[i][j]=='-')
status |= ;
}
if(status==){
printf("0\n");
continue;
}
memset(vis,,sizeof(vis));
BFS(status);
}
return ;
}
4 4
Source
POJ 2965 The Pilots Brothers' refrigerator (DFS)的更多相关文章
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- POJ 2965 The Pilots Brothers' refrigerator (暴力枚举)
https://vjudge.net/problem/POJ-2965 与poj-1753相似,只不过这个要记录路径.poj-1753:https://www.cnblogs.com/fht-lito ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
随机推荐
- Configuring HDFS High Availability
Configuring HDFS High Availability 原文请訪问 http://blog.csdn.net/ashic/article/details/47024617,突袭新闻小灵儿 ...
- Objective-C:OC内部可变对象和不可变对象的深(复制)拷贝问题思考:
OC内部:可变对象和不可变对象的深(复制)拷贝问题思考: 不可变对象: 例如NSString对象,因为NSString对象是常量字符串,所以,不可以更改其内容,但是可以修改指向该字符串的指针指向 ...
- C#全屏截图
Image baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); ...
- NYOJ-712 探寻宝藏(第六届河南省程序设计大赛)
探 寻 宝 藏 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫 ...
- POJ 1564 Sum It Up (DFS+剪枝)
...
- Ubuntu14.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 说在前面的话 首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu 14.04.4 LTS ...
- 王立平-- android:layout_weight
效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill/I0 ...
- HAProxy的独门武器:ebtree
1. HAProxy和ebtree简介 HAProxy是法国人Willy Tarreau个人开发的一个开源软件,目标是应对客户端10000以上的同时连接,为后端应用服务器.数据库服务器提供高性能的负载 ...
- SpringMVC上传文件的三种方式(转载)
直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/common ...
- python mongodb ubuntu
mongodb install: sudo apt-get install mongodb Install pip 1. $ sudo apt-get install python-pip pytho ...