Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 21217 | Accepted: 8196 | 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
Source
Northeastern Europe 2004, Western Subregion
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
using namespace std;
struct Node{
int x;
int y;
void out(){
printf("%d %d\n",x,y);
}
}ans[100];
bool mm[6][6];
char c;
int cnt;
bool flag(){
FOR(i,1,4)
FOR(j,1,4)
if(mm[i][j])
return false;
return true;
}
void change(int x,int y){
mm[x][y]=!mm[x][y];
}
void fun(int x,int y){
change(x,y);
FOR(i,1,4){
change(i,y);
change(x,i);
}
}
void dfs(int x,int y){
if(flag()){
printf("%d\n",cnt);
FOR(i,0,cnt-1)
ans[i].out();
return;
}
if(x>4)return;
fun(x,y);
ans[cnt].x=x;
ans[cnt].y=y;
cnt++;
if(y<4)dfs(x,y+1);else dfs(x+1,1);
fun(x,y);
cnt--;
if(y<4)dfs(x,y+1);else dfs(x+1,1);
}
int main(){
FOR(i,1,4)
FOR(j,1,4){
cin>>c;
if(c=='-')
mm[i][j]==false;
else mm[i][j]=true;
}
cnt=0;
dfs(1,1);
return 0;
}
/*
-+--
----
----
-+--
*/