POJ-----2965---The Pilots Brothers' refrigerator---枚举规律

The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25859 Accepted: 9979 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 rowi and all handles in columnj.

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

4*4的矩阵,16个开关,+代表开关处于关闭状态,-是打开状态,可以选取一个坐标,使以此点为中心的横排和纵排所有开关状态反转,问最少多少步使全部开关处于打开状态



由于是两种状态,所以每个坐标最多只能选取一次,选两次相当于反转两次,又回到原状态,相当于没动,所以16个点一共2^16种状态,因而最多需要16步,暴力可行

最多16步,加上不需要反转,也就是0,一共17种步数,可以依次递归i步能否满足,找出最小步数,并储存输出

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long  LL;
const int maxn = 1e3+10;
int s[5][5], sum;
bool flag;
struct node{  
    int x, y;  
} cnt[maxn];
void mov(int x, int y){ 
    s[x][y] = !s[x][y];  
    for(int i = 1; i <= 4; i++){  
        s[x][i] = !s[x][i];  
        s[i][y] = !s[i][y];  
    }  
}  
bool judge(){  
    for(int i = 1; i <= 4; i++){  
        for(int j = 1; j <= 4; j++){  
            if(!s[i][j]) return false;
        }
    }
    return true;
}  
void DFS(int x, int y, int step, int ans){  
    if(step == ans){  
        flag = judge();
        sum = ans;
        return ;  
    }  
    if(flag || x > 4) return ;
    mov(x, y);  
    if(y < 4){  
        DFS(x, y + 1, step + 1, ans);  
        cnt[step].x = x;  
        cnt[step].y = y;  
    }  
    else{  
        DFS(x + 1, 1, step + 1, ans);  
        cnt[step].x = x;  
        cnt[step].y = y;  
    }  
    mov(x, y);
    if(y < 4) DFS(x, y + 1, step, ans);
    else DFS(x + 1, 1, step, ans);
}  
int main(){ 
    char u;  
    for(int i = 1; i <= 4; i++){  
        for(int j = 1; j <= 4; j++){  
            cin >> u;
            if(u == '-') s[i][j] = 1;
            else s[i][j] = 0;
        }
    }  
    flag = 0;  
    for(int i = 0; i <= 16; i++){
        DFS(1, 1, 0, i);
        if(flag){
        	cout << sum << endl;
        	break;
		}
    }  
    if(flag){
    	for(int i = 0; i < sum; i++) cout << cnt[i].x << ' ' << cnt[i].y << endl;
	}
    return 0;  
}  


参考巨巨思路,将一个点包含它本身和与它同排同列的点反转,本身状态改变7次,与它同排同列的改变4次,其余点改变2次,偶数次相当于没有变化,所以一个点包含它本身和与它同排同列的点都反转一次相当于只反转本身,所以查找所有的+,修改整体的改变次数,最后扫描矩阵,奇数的个数就是最少步数,输出奇数点即可

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long  LL;
const int maxn = 1e5+10;
int s[5][5];
void enu(int x, int y){
	for(int i = 1; i <= 4; i++){
		s[x][i]++;
		if(i != x) s[i][y]++;
	}
}
int ans(){
	int c = 0;
	for(int i = 1; i <= 4; i++){
		for(int j = 1; j <= 4; j++){
			if(s[i][j] & 1) c++;
		}
	}
	return c;
}
int main(){
	char u;
 	for(int i = 1; i <= 4; i++){
		for(int j = 1; j <= 4; j++){
			cin >> u;
			if(u == '+') enu(i, j);
		}
	}
	printf("%d\n", ans());
	for(int i = 1; i <= 4; i++){
		for(int j = 1; j <= 4; j++){
			if(s[i][j] & 1) cout << i << ' ' << j << endl;
		}
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值