poj 2965 枚举+DFS
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 25343 | Accepted: 9786 | 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
4 4
思路:因为每个位置最多翻一次,所以最多翻16次,用枚举来做。(代码是看别人的,有一部分还不能完全理解。。)
#include "cstdio"
#include "algorithm"
#include "cstring"
char map[][];
int m[][];
typedef struct {
int s,t;
}road;
road a[];
int k,ans;
int P(){
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(m[i][j]==){
return ;
}
}
}
return ;
}
void G(int x,int y){
m[x][y]^=;
for(int i=;i<=;i++){
m[x][i]^=;
}
for(int i=;i<=;i++){
m[i][y]^=;
}
}
void dfs(int x,int y,int step) {
// G(x, y);
if (step == ans) {
k = P();//
return;
}
if(k||x>=){//不太理解这里为什么要判断k是否为1,个人觉得只要当ans=step时在dfs外判断即可??
return;
}
G(x, y);
if(y<){
dfs(x,y+,step+);
a[step].s=x;
a[step].t=y;
}
else {
dfs(x+,,step+);
a[step].s=x;
a[step].t=y;
}
G(x,y);
if(y<){
dfs(x,y+,step);
}
else {
dfs(x+,,step);
} }
int main(){
k=;
for(int i=;i<=;i++){
scanf("%s",map[i]);
for(int j=;j<;j++){
if(map[i][j]=='-'){
m[i][j+]=;
}
else if(map[i][j]=='+'){
m[i][j+]=;
}
}
}
for(int i=;i<=;i++){
ans=i;
dfs(,,);
if(k){
printf("%d\n",ans);
break;
}
}
for(int i=;i<ans;i++){
printf("%d %d\n",a[i].s,a[i].t);
}
return ;
}
poj 2965 枚举+DFS的更多相关文章
- POJ 1753 (枚举+DFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40632 Accepted: 17647 Descr ...
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 2965:The Pilots Brothers' refrigerator
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
随机推荐
- 洛谷 P1195 口袋的天空(最小生成树)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P1195 思路: 首先可以判断这道题是用最小生成树来做的,然后在将其合并时用ans记录一下它的总消耗, ...
- 让你迅速了解redis
(1)什么是redis? Redis 是一个基于内存的高性能key-value数据库. (2)Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数 ...
- JS的函数参数传递为值传递
function setAge(i) { alert(i);//24 i = 18; alert(i);//18,i的改变不会影响外面的age }; var age = 24; setAge(age) ...
- HTML超链接实用
1.文本链接: <a href="http://www.meng.com/" target="_blank">访问meng!</a> 2 ...
- <Android 基础(十四)> selector
介绍 A StateListDrawable is a drawable object defined in XML that uses a several different images to r ...
- 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)
#include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...
- python网络编程-paramiko模块
paramiko模块 该模块基于SSH用于连接远程服务器并执行相关操作 参考文档 SSHClient 用于连接远程服务器并执行命令 import paramiko #创建SSH对象 ssh = par ...
- C#面向对象几组关键字的详解(this,base,dynamic,var,const,readonly,is,as)
× 目录 [1]this和base的区别 [2]var和dynamic的区别 [3]const和readonly的区别 [4]is和as的区别 这几个关键字,在用法上有许多相似之处.这里主要看看细节之 ...
- Linux vi 常用指令总结
本文根据笔者,日常常用的linux下的vi指令,进行说明 一.基本操作 1.vi 文件名 进入vi 的“命令行模式”,此模式无法编辑,只能查看 需要按下键盘的“i”键,进入“编辑模式”,才能进行文件的 ...
- bootstrap-table 大量字段整体表单上传之时间处理
js 中用$('#addUserForm').serialize(),//获取表单中所有数据 传送到前台 (controller) $.ajax({ type : "POST", ...