Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 17450 | Accepted: 6600 | 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 iand 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
Source
//164K 454MS C++ 1319B 2014-04-26 11:52:45
/* 题意:
和poj 1753 差不多,不过这里翻转和判断有点不一样,翻转是行列都要翻,判断是
要全1才行。 解法和poj1753几乎一样,就是要加个二维数组记录一下输出路径。 */
#include<stdio.h>
#include<string.h>
int g[][];
int root[][];
int flag;
inline int judge(int tg[][])
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(g[i][j]==) return ;
return ;
}
void flip(int x,int y)
{
for(int i=;i<=;i++){
g[x][i]^=;
g[i][y]^=;
}
g[x][y]^=;
}
void dfs(int x,int y,int cnt,int n)
{
if(cnt==n){
flag=judge(g);
return;
}
if(flag || y>) return;
root[cnt][]=x;
root[cnt][]=y;
flip(x,y);
if(x<) dfs(x+,y,cnt+,n);
else dfs(,y+,cnt+,n);
flip(x,y);
if(x<) dfs(x+,y,cnt,n);
else dfs(,y+,cnt,n);
}
int main(void)
{
char c[];
while(scanf("%s",c)!=EOF)
{
memset(g,,sizeof(g));
for(int i=;i<;i++) g[][i+]=c[i]=='-'?:;
for(int i=;i<;i++){
scanf("%s",c);
for(int j=;j<;j++)
g[i+][j+]=c[j]=='-'?:;
}
flag=;
int cnt=-;
for(int i=;i<=;i++){
dfs(,,,i);
if(flag){
cnt=i;break;
}
}
printf("%d\n",cnt);
for(int i=;i<cnt;i++)
printf("%d %d\n",root[i][],root[i][]);
}
return ;
} /* -+--
----
----
-+-- +---
----
----
---- -+++
+---
+---
+--- ----
----
----
---- ++++
++++
++++
++++ */