POJ 2968 The Pilots Brothers' refrigerator(DFS)

来源:互联网 发布:网络问政的特点 编辑:程序博客网 时间:2024/06/10 00:04
The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26904 Accepted: 10375 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

61 11 31 44 14 3

4 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;} 


阅读全文
0 0
原创粉丝点击