poj1315象棋(递归)

Don't Get Rooked

Description

In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 4x4) that can also contain walls through which rooks cannot move. The goal is to place as many rooks on a board as possible so that no two can capture each other. A configuration of rooks is legal provided that no two rooks are on the same horizontal row or vertical column unless there is at least one wall separating them. 

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of rooks in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways. 

Your task is to write a program that, given a description of a board, calculates the maximum number of rooks that can be placed on the board in a legal configuration. 

Input

The input contains one or more board descriptions, followed by a line containing the number 0 that signals the end of the file. Each board description begins with a line containing a positive integer n that is the size of the board; n will be at most 4. The next n lines each describe one row of the board, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input.

Output

For each test case, output one line containing the maximum number of rooks that can be placed on the board in a legal configuration.

Sample Input

4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0

Sample Output

5
1
5
2
4

#include<iostream>
#include<cstring>
using namespace std;
char board[5][5];
int visited[5][5];//visited数组表示此位置是否被访问过
int cnt,n,maxn;
bool place(int x,int y)
{
	for(int i=x+1;i<n&&board[i][y]!='X';i++)
	{
		if(visited[i][y]) 
		return false;
	}
	for(int i=x-1;i>=0&&board[i][y]!='X';i--)
	{
		if(visited[i][y])  
		return false;
	}
	for(int i=y+1;i<n&&board[x][i]!='X';i++)
	{
		if(visited[x][i])  
		return false;
	}
	for(int i=y-1;i>=0&&board[x][i]!='X';i--)
	{
		if(visited[x][i]) 
		return false;
	}
	return true;
 } 
void placed(int cnt)
{
	if(cnt>=maxn) maxn=cnt;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(!visited[i][j]&&board[i][j]=='.'&&place(i,j)) 
			{
			visited[i][j]=1;
			placed(cnt+1);
			visited[i][j]=0;
		}
		}
	}
	
}
int main()
{
	while(cin>>n&&n)
	{
		maxn=0; 
		memset(visited, 0, sizeof(visited));
		memset(board, 0, sizeof(board));
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin>>board[i][j];
			}
		}
		placed(0);
		cout<<maxn<<endl;
	}
	return 0;
}

  memset()函数原型是extern void *memset(void *buffer, int c, int count)      

  buffer:为指针或是数组 , c:是赋给buffer的值 , count:是buffer的长度.

  这个函数在多用于清空数组.如:原型是memset(buffer, 0, sizeof(buffer))

  memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;

      例:char a[100];memset(a, '/0', sizeof(a));

  memset可以方便的清空一个结构类型的变量或数组。

  如:

 struct sample_struct
 {
      char csName[16];
      int iSeq;
      int iType;
 };

对于变量:
struct sample_strcut stTest;

一般情况下,清空stTest的方法:

stTest.csName[0]='/0';
stTest.iSeq=0;
stTest.iType=0;

用memset就非常方便:
memset(&stTest,0,sizeof(struct sample_struct));

如果是数组:

struct sample_struct TEST[10];

memset(TEST,0,sizeof(struct sample_struct)*10);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值