Time Limit: 1000MS | Memory Limit: 65536K | |||
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 题意:就是让我们翻转其中的某些符号,翻转该符号时,该行和该列的符号全要变号,使得整张图的符号全为‘-’ 首先附上我的代码: #include <iostream> #include <cstring> #include <cstdio> #include <iomanip> #include <algorithm> using namespace std; int visit[6][6],step,flag; int a[17],b[17]; bool check()//该函数检查是否已经达到题目要求 { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=4;j++) { if(visit[i][j]!=0) { return false; } } } return true; } void flip(int x,int y)//翻转函数,注意翻转行和列时,翻转原点的位置只能翻转一次 { int i; for(i=1;i<=4;i++) { visit[x][i]=!visit[x][i]; } for(i=1;i<=4;i++) { if(i!=x)//行已全部翻转,所以列翻转时,该点就不用再次翻转了 { visit[i][y]=!visit[i][y]; } } } void dfs(int x,int y,int tmp) { if(tmp==step)//如果步数找到,看是否满足题目要求 { flag=check(); return; } if(x==5 || flag) return;//剪枝,如果行搜到5,那么就不能再搜了 flip(x,y); if(y<4)//我采用的是顺序搜索,所以比较耗时,还好没超时 { dfs(x,y+1,tmp+1); }else{ dfs(x+1,1,tmp+1); } a[tmp]=x,b[tmp]=y;//如果搜索的步数能够加1,那么肯定是行的通的,所以记录路径 flip(x,y);//如果上述的翻转不能满足条件,那么就把它还原回去 if(y<4) { dfs(x,y+1,tmp);//移动位置后,继续搜索 }else{ dfs(x+1,1,tmp); } } int main() { char c[6][6]; int i,j; for(i=1;i<=4;i++) { for(j=1;j<=4;j++) { cin>>c[i][j]; if(c[i][j]=='+') { visit[i][j]=1; } } } for(step=0;step<=16;step++)//因为整个图最多就是16个位置,那么我们最多就是搜索16个位置就好了 { dfs(1,1,0); if(flag) break; } cout<<step<<endl; for(i=0;i<step;i++) { cout<<a[i]<<" "<<b[i]<<endl; } return 0; } 我个人的代码比较耗时,用了700多ms,下面介绍大神的代码,思路比我的好多了,我们要把‘+’翻转为‘-’,那么操作的次数必定要是奇数,这个思路超好#include<iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; int visit[6][6]; int main() { char c; int i,j; for(i=1;i<=4;i++) { for(j=1;j<=4;j++) { cin>>c; if(c=='+')//如果该点是‘+’,那么它肯定是要被翻转的,所以该行和该列的操作次数加1 { for(int p=1;p<=4;p++) { visit[i][p]+=1; visit[p][j]+=1; } visit[i][j]-=1;//行和列的操作次数重复了一次,所以要减去一次 } } } int cnt=0; int output[16][2]; for(i=1;i<=4;i++) { for(j=1;j<=4;j++)
{ if(visit[i][j]%2==1)//只有当操作次数为奇数时,‘+’号才会变为‘-’号,所以我们就需要记录路径 { output[cnt][0]=i; output[cnt][1]=j; cnt++; } } } cout<<cnt<<endl; for(i=0;i<cnt;i++) { cout<<output[i][0]<<" "<<output[i][1]<<endl; } return 0; }