poj2965 The Pilots Brothers' refrigerator

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
这道题和翻黑白棋差不多,用深搜可以做,不同的是要记录每次的路径,可以设个数组b[][].
#include<stdio.h>
#include<string.h>
int step,flag;
char s[10][10];
int b[20][2];
int panduan(char s[10][10]){
	int i,j;
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			if(s[i][j]==‘+‘)return 0;
		}
	}
	return 1;
}

void flip(int row,int col)
{
	int i,j;
	for(i=0;i<=3;i++){
		if(s[i][col]==‘+‘)s[i][col]=‘-‘;
		else s[i][col]=‘+‘;
	}
	for(i=0;i<=3;i++){
		if(s[row][i]==‘+‘)s[row][i]=‘-‘;
		else s[row][i]=‘+‘;
	}
	if(s[row][col]==‘+‘)s[row][col]=‘-‘;
		else s[row][col]=‘+‘;
	return;
}

void dfs(int row,int col,int dep)
{
	int i,j;
	if(flag)return;
	if(dep==step){
		flag=panduan(s);return;
	}
	if(flag || row==4)return;

	flip(row,col);
	if(col<3){
		b[dep+1][0]=row;
		b[dep+1][1]=col;
		dfs(row,col+1,dep+1);
	}
	else{
		b[dep+1][0]=row;
		b[dep+1][1]=col;
		dfs(row+1,0,dep+1);
	}
	if(flag)return;

	flip(row,col);
	if(col<3){
		dfs(row,col+1,dep);
	}
	else dfs(row+1,0,dep);
	return;
}

int main()
{
	int n,m,i,j;
	while(scanf("%s",s[0])!=EOF)
	{
		for(i=1;i<=3;i++){
			scanf("%s",s[i]);
		}
		for(step=1;step<=16;step++){
			memset(b,0,sizeof(b));
			flag=0;
			dfs(0,0,0);
			if(flag)break;
		}
		printf("%d\n",step);
		for(i=1;i<=step;i++){
			printf("%d %d\n",b[i][0]+1,b[i][1]+1);
		}
	}
	return 0;
} 

poj2965 The Pilots Brothers' refrigerator

时间: 2024-10-19 18:17:32

poj2965 The Pilots Brothers' refrigerator的相关文章

poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2965 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

poj2965 The Pilots Brothers&#39; refrigerator 枚举或DFS

http://poj.org/problem?id=2965 一.枚举每一个‘+’点,对该点和该点所在的同一行和同一列所有点进行操作,开关本身状态改变了7次,开关同一行.同一列的开关状态改变了4次,其他开关状态改变了2次. 然后,用一个数组存取每个操作,如果为奇数,则证明该点为必须点,否则为不必要的. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char map[4][4]; 5 int ans[4

The Pilots Brothers&#39; refrigerator - poj 2965

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   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 handle

poj 2965 The Pilots Brothers&#39; refrigerator[ 枚举 ]

传送门:http://poj.org/problem?id=2965 思路:二进制枚举,递归输出路径.G++ 900+Ms勉强过,C++超时. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map>

POJ 2965 The Pilots Brothers&#39; refrigerator

原题链接:http://poj.org/problem?id=2965 简单的dfs,因为是要求最小值,枚举深度来得到递归终止条件,和 poj1753很像,毕竟是放在一起的. #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <cmath> #include <cstdlib> #include <vector&g

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

【递归】POJ2965-The Pilots Brothers&#39; refrigerator

和POJ1753的方法一致,唯一不同的是需要记录路径,只需添加一个全集变量的path路径即可. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int INF=100000; 5 int map[4][4]; 6 int path[4][4]; 7 int maxpath[4][4]; 8 int ans=INF; 9 10 void open(int step,int turn) 11

POJ 1965 The Pilots Brothers&#39; refrigerator 搜索

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18222   Accepted: 6936   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o