POJ 2968 The Pilots Brothers' refrigerator(DFS)
来源:互联网 发布:网络问政的特点 编辑:程序博客网 时间:2024/06/10 00:04
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
61 11 31 44 14 34 4
这个题的题意是一个冰箱有好多把手,让那些把手都是开的状态需要那几步,输出步数,和相应的步数,转动某一个把手时
这个把手对应的一行和一列都改变状态。16个把手,DFS解决。
#include<stdio.h>#include<string.h>int map[50][2];int ans,flag;int mapp[10][10];int min(int a,int b){return a>b?b:a;}int find()//查询下当前是否已经可以打开了,我本来用的是+ —号用来判断直接就TL 后来改成01就AC{for(int i=0;i<4;i++){for(int j=0;j<4;j++){if(mapp[i][j] == 0)return 0;}}return 1;}void exchange(int a,int b)//翻转这些把手的状态,当前把手会改变两次 所以需要提前改变依次。{int i;mapp[a][b] = !mapp[a][b];for(i=0;i<4;i++){mapp[a][i] = !mapp[a][i];mapp[i][b] = !mapp[i][b];} } void dfs(int a,int b,int c){int flag1=find();//查询状态if(flag1)//可以打开记录当前的步数 返回{flag=c;return ;}if(flag || a>=4 || b>=4)//越界 return ;exchange(a,b);//改变状态 if(b<3)//当前不是最后一列 所以继续往后一列DFS {dfs(a,b+1,c+1);map[c][0]=a;//记录路径map[c][1]=b;}else//当前是最后一列{dfs(a+1,0,c+1);map[c][0]=a;map[c][1]=b;}exchange(a,b);//状态变回来 当前位置不改变继续往下走 if(b<3)dfs(a,b+1,c);elsedfs(a+1,0,c);}int main(){int i;char ch[10][10];for(i=0;i<4;i++){scanf("%s",ch[i]);for(int j=0;j<4;j++){if(ch[i][j] == '+')mapp[i][j]=0;elsemapp[i][j]=1;}}flag=0;dfs(0,0,0);if(flag){printf("%d\n",flag);for(i=0;i<flag;i++)printf("%d %d\n",map[i][0]+1,map[i][1]+1);}return 0;}
- POJ 2968 The Pilots Brothers' refrigerator(DFS)
- poj The Pilots Brothers' refrigerator(DFS)
- POJ 2965 The Pilots Brothers' refrigerator(DFS)
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- poj 2965 The Pilots Brothers' refrigerator(dfs)
- poj 2965 The Pilots Brothers' refrigerator(dfs)
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- POJ 2965 The Pilots Brothers' refrigerator(回溯查找,dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+DFS)
- POJ 2965 The Pilots Brothers' refrigerator (DFS + 枚举)
- (枚举,dfs)The Pilots Brothers' refrigerator poj 2965
- The Pilots Brothers' refrigerator(dfs)
- The Pilots Brothers' refrigerator(POJ-2965)
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- 百度地图多点聚合(标注与信息窗口)最佳视野
- JS编程-寻找字符串中出现最多的字符以及重复次数
- 2017/8/5训练日记(最短路径算法之Dijkstra算法和Bellman-Ford算法)
- 事务管理之事务的基本概念总览
- HDUOJ {A}+{B}
- POJ 2968 The Pilots Brothers' refrigerator(DFS)
- HDU4876(深搜+剪枝+全排列)
- mybatis级联映射
- 【react】利用shouldComponentUpdate钩子函数优化react性能以及引入immutable库的必要性
- django允许外部访问
- 常用HTML标签学习笔记1
- python系列之numpy模块学习
- 解决unity无法添加环境音效的问题,曲线调整深度理解。(最新版unity2017)
- python opencv入门 更多关于轮廓的函数(20)