The Pilots Brothers' refrigerator-DFS路径打印
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
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
4 4
/*
Author: 2486
Memory: 144 KB Time: 360 MS
Language: C++ Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1<<18;
char maps[5][5];
bool vis[maxn];
int path[maxn];//记录终于路径
int pathvis[maxn];//记录DFS过程中的路径
int Min;
int binary(int val,int x,int y) {
for(int i=0; i<16; i++) {//选取行列进行取反
if(i/4==x||i%4==y) {
val^=(1<<i);
}
}
return val;
}
void dfs(int s,int step,int x,int y) {
if(s==0) {
if(Min>step) {
Min=step;
for(int i=0;i<step;i++){
path[i]=pathvis[i];//假设比先前的步数更少就转到存储终于路径的数组中
}
}
return;
}
if(x>=4)return;
int nx,ny;
if(y+1>=4) {//向右走然后向下走
nx=x+1;
ny=0;
} else {
ny=y+1;
nx=x;
}
int fd=x*4+y;
int k=binary(s,x,y);
pathvis[step]=fd;//当前的位置进行反转
dfs(k,step+1,nx,ny);
dfs(s,step,nx,ny);
}
int main() {
while(~scanf("%s",maps[0])) {
for(int i=1; i<4; i++) {
scanf("%s",&maps[i]);
}
int s=0;
for(int i=3; i>=0; i--) {
for(int j=3; j>=0; j--) {
if(maps[i][j]=='+')s=s<<1|1;
else s<<=1;
}
}
if(s==0) {
printf("0\n");
continue;
}
Min=10000000;
dfs(s,0,0,0);
printf("%d\n",Min);
for(int i=0; i<Min; i++) {
printf("%d %d\n",path[i]/4+1,path[i]%4+1);
}
}
return 0;
}
The Pilots Brothers' refrigerator-DFS路径打印的更多相关文章
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...
- POJ 2965:The Pilots Brothers' refrigerator
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19356 ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
随机推荐
- (16) Cloudflare pki公钥基础设施
该工具组共有8个工具 1.cfssl 常用的可用指令: sign signs a certificate bundle build a certificate bundle genkey genera ...
- python 学习总结1
计算机与程序设计 一.计算机的概念 1.计算机是根据指令操作数据的设备. 2.计算机主要包括两个功能性一个是功能性另一个是计算性 功能性是对数据的操作,表现为数据计算,输入输出处理和结果存储 可编程性 ...
- Ubuntu16.04进入无限登录状态的解决办法
具体来说就是,输入密码之后又到了登录界面,无限循环(也许可能不能输入密码,这种状态我没有测试) 此方案仅适用于安装过NVIDIA显卡驱动的系统并且在登录界面会发现分辨率变了 如果你没有安装过NVIDI ...
- win7 如何将python加入环境变量
我的电脑->右键属性->高级->系统变量 path里加上安装路径,比如 ";C:\Python26;" 请加分号附在其他path后面而不是直接覆盖.
- 自动化测试-selenium IDE使用
selenium IDE结合浏览器提供脚本的录制,回放以及编辑脚本功能,以及元素的定位,可以使用selenium IDE将录制的脚本生成相应的带单元测试框架的自动化测试脚本. selenium具有录制 ...
- Charm Bracelet(01背包)
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fil ...
- Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape
Android ShapeDrawable之OvalShape.RectShape.PaintDrawable.ArcShape Android图形图像基础之OvalShape.RectShap ...
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
- HDU-1087Super Jumping! Jumping! Jumping!
Super Jumping! Jumping! Jumping! ...
- HDU 3973 线段树+字符串hash
题目大意: 不断修改字符串中的字母,然后询问区间字符串是否处于已给定的字符串集合中 这里将原来的字符串集合保存到hash表中,当然用map,set都没有问题 修改查询都用线段树实现,自己的query函 ...