【ACM训练】枚举

传送门: poj1753

Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 55896   Accepted: 23364

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: 

  1. Choose any one of the 16 pieces. 
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).


Consider the following position as an example: 

bwbw 
wwww 
bbwb 
bwwb 
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become: 

bwbw 
bwww 
wwwb 
wwwb 
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal. 

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

 

解题思路:数组很小直接深搜,子集树深搜

#include
#include
#include
#include
#include
#include
using namespace std;

char s[20][20];
int num=200;

bool check()
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
	{
		if(s[i][j]!=s[0][0]) return false;
	}
	return true;
}


void dfs(int x,int y,int cnt)
{
	int tx=x+(y+1)/4;
	int ty=(y+1)%4;
	if(x>=4)
	{
		if(check())
		{
			if(cnt=0)
	{
		if(s[x-1][y]=='w') s[x-1][y]='b';
	else s[x-1][y]='w';
	}
	if(y+1<4)
	{
		if(s[x][y+1]=='w') s[x][y+1]='b';
	else s[x][y+1]='w';
	}
	if(y-1>=0)
	{
		if(s[x][y-1]=='w') s[x][y-1]='b';
	else s[x][y-1]='w';
	}
	dfs(tx,ty,cnt+1);
	if(s[x][y]=='w') s[x][y]='b';
	else s[x][y]='w';
	if(x+1<4)
	{
		if(s[x+1][y]=='w') s[x+1][y]='b';
	else s[x+1][y]='w';
	}
	if(x-1>=0)
	{
		if(s[x-1][y]=='w') s[x-1][y]='b';
	else s[x-1][y]='w';
	}
	if(y+1<4)
	{
		if(s[x][y+1]=='w') s[x][y+1]='b';
	else s[x][y+1]='w';
	}
	if(y-1>=0)
	{
		if(s[x][y-1]=='w') s[x][y-1]='b';
	else s[x][y-1]='w';
	}
	dfs(tx,ty,cnt);
	return ;
}

int main()
{
	for(int i=0;i<4;i++)
		scanf("%s",s[i]);
	
	dfs(0,0,0);
	if(num==200) printf("Impossible\n");
	else printf("%d\n",num);
	return 0;
}

 

传送门: poj2965

The Pilots Brothers' refrigerator

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32335   Accepted: 12515   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

解题思路:和上一道题的思路类似,为一区别就是要输出中间的过程,使用一个数组标记即可。

#include
#include
#include
#include
#include
#include
using namespace std;

char s[20][20];
int num=200;
int a[20];
int ans[20];

bool check()
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
	{
		if(s[i][j]!='-') return false;
	}
	return true;
}


void dfs(int x,int y,int cnt)
{
	int tx=x+(y+1)/4;
	int ty=(y+1)%4;
	if(x>=4)
	{
		if(check())
		{
			if(cnt

 

你可能感兴趣的:(【ACM训练】)