poj2965 【枚举】

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
思路:dfs,加个数组保存路径
实现代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<list>
using namespace std;
#define ll long long
#define sd(x) scanf("%d",&x)
#define sdd(x,y) scanf("%d%d",&x,&y)
#define sddd(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sf(x) scanf("%s",x)
#define ff(i,x,y) for(int i = x;i <= y;i ++)
#define fj(i,x,y) for(int i = x;i >= y;i --)
#define mem(s,x) memset(s,x,sizeof(s));
#define pr(x) printf("%d",x);
const int Mod = 1e9+;
const int inf = 1e9;
const int Max = 1e5+;
void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=;y=;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;}
int gcd(int a,int b) { return (b>)?gcd(b,a%b):a; }
int lcm(int a, int b) { return a*b/gcd(a, b); }
//int mod(int x,int y) {return x-x/y*y;}
char s[];
int mp[][];
int ans = inf,i,j;
int rankx[],ranky[],fx[],fy[];
int check()
{
for(i=;i<;i++){
for(j=;j<;j++){
if(mp[i][j]!=)
return ;
}
}
return ;
} void fan(int x,int y){
for(i=;i<;i++){
mp[x][i] = !mp[x][i];
mp[i][y] = !mp[i][y];
}
mp[x][y] = !mp[x][y];
} int dfs(int x,int y,int t)
{
if(check()){
if(ans>t){
ans = t;
for(i=;i<t;i++){
rankx[i] = fx[i];
ranky[i] = fy[i];
}
}
return ;
}
if(x>=||y>=)
return ;
int nx = (x+)%;
int ny = y+(x+)/;
dfs(nx,ny,t);
fan(x,y); fx[t] = x+;
fy[t] = y+; dfs(nx,ny,t+);
fan(x,y); return ;
}
int main()
{
for(i=;i<;i++){
cin>>s;
for(j=;j<;j++){
if(s[j]=='+')
mp[i][j] = ;
else
mp[i][j] = ;
}
}
dfs(,,);
cout<<ans<<endl;
for(i=;i<ans;i++){
cout<<rankx[i]<<" "<<ranky[i]<<endl;
}
return ;
}
上一篇:JS数组整理


下一篇:iOS 微信支付SDK与微信友盟分享两者同时集成时,出现的问题与解决之路。