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 ...
随机推荐
- (生产)better-scroll - 下拉刷新
Options 参数 startX: 0 开始的X轴位置 startY: 0 开始的Y轴位置 scrollY: true 滚动方向为 Y 轴 scrollX: 'true' 滚动方向为 X 轴 cli ...
- ATL
Normal COM.cpp #include "resource.h" // 主符号 #include "ATLCOM_i.h" #include " ...
- 【起航计划 018】2015 起航计划 Android APIDemo的魔鬼步伐 17 App->Alarm->Alarm Service
Alarm Service和Alarm Controller 例子非常类似,只是Alarm Service是用来Schedule一个Service,而前面的例子是来Schedule一个Broadcas ...
- Azure IOT 设备固件更新技巧,看这一篇就够了
嫌长不看版 今天为大家准备的硬菜是:在 Azure IoT 中心创建 Node.js 控制台应用,进行端到端模拟固件更新,为基于 Intel Edison 的设备安装新版固件的流程.通过创建模拟设备应 ...
- C#正则表达式获取网址的域名(IP)
代码如下: string p = @"(http|https)://(?<domain>[^(:|/]*)"; Regex reg = new Regex(p, Reg ...
- GNU Nano编辑器
^表示 control建 ^X 退出 nano ^O 保存文件 ^R 插入其他文件内容至光标位置 ^W 查找字符串 ^Y 跳至前一屏 ^V 跳至后一屏 ^K 剪切光标所在行并保存到剪贴板,或剪切选中内 ...
- 谨慎使用#pragma pack
前段时间将一个项目由vc6.0转为vs2005,发现了有些对象的地址奇怪变化的问题,细查之下发现出现了#pragma pack乱用的问题,在恢复内存对齐使用了#pragma pack(pop, 1)的 ...
- STM32开发-MDK新建工程及配置
本人也是接触stm32没多久,之前用的MDK是5.1,现在用的是5.13,MDK5.0之前的版本(本人简称旧版)和之后的版本(本人简称新版)新建工程有很大区别.对于刚开始用学stm32的新手来说,基本 ...
- IOS 设置颜色的的详情
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- jwt 在.net core 2.0的使用
jwt个人觉得更适合作共享session的传递格式,本身保密性不好,容易泄露重要信息,他的格式为头.一些用户的自定义声明.前两者的加密(公私对称密钥形式) 需要引用nuget: System.Iden ...