uva 639 Don't Get Rooked

题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=580


题目描述:


 Don't Get Rooked 

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 4$\times$4) 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 file 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 file.

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



题意:

在有隔墙的至多4*4棋盘中 放置车,让他们不能互相攻击,问最多能放多少个车。


题解:

八皇后问题,至少是类似的,至多4*4,所以直接暴力深搜就行,与八皇后不同的是,八皇后是按行来枚举  放置棋子的,这里由于有了墙的存在,所有就要按一个一个空

来枚举放置棋子的情况。

这里还将棋盘做了点小处理,以便于棋子的放置,因为放一个棋子,那么其实放这个棋子的空其实就变成墙了,然后根据车的行走方式,可以将此点作为十字中心,展开

以传染的方式将周围是空的棋格全部感染成墙(  直到遇到真的墙或是到达期盼边缘为止)。这样在一定程度上减少了枚举棋子放置的数据量。


代码:

/*

topcoder codeforce acm

uva 10474 - Where is the Marble?

if we place a rook then we can place some places to the wall because these places must not be placed at the other rooks

first line we could try the first line first col then DFS it
we could also use multiple start point to DFS

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

int Max=0;//the count of the placed rooks
int n=0;//the size of the board
char c='\0';
char Board[10][10]={'\0'};

/*Output the Board*/
int OutputBoard()
{
	int i=0,j=0;
	for(i=0;i<=n-1;i++)
	{
		for(j=0;j<=n-1;j++)
		{
			printf("%c",Board[i][j]);
		}
		printf("\n");
	}
	return(0);
}

/*//place the 1(first) rook to the position (i,j)*/
int DFS(int x,int y,int cnt)
{
	if(cnt>Max)
	{
		Max=cnt;
	}
	if(x>=n||y>=n)
	{
		return(0);
	}
	else
	{
		//place the '.' to the 'X'
		int i=0,j=0,i1=0,j1=0;
		i=x;
		j=y;
		Board[x][y]='X';
		//vertical extension
		for(i=x+1;i<=n-1;i++)
		{
			if(Board[i][y]=='.')//Board is a gloable in the recursive structure
			{
				Board[i][y]='X';
			}
			else
			{
				break;
			}
		}
		for(i1=x-1;i1>=0;i1--)
		{
			if(Board[i1][y]=='.')//Board is a gloable in the recursive structure
			{
				Board[i1][y]='X';
			}
			else
			{
				break;
			}
		}
		//horizontal extension
		for(j=y+1;j<=n-1;j++)
		{
			if(Board[x][j]=='.')
			{
				Board[x][j]='X';
			}
			else
			{
				break;
			}
		}
		for(j1=y-1;j1>=0;j1--)
		{
			if(Board[x][j1]=='.')
			{
				Board[x][j1]='X';
			}
			else
			{
				break;
			}
		}
		
		int is=0,js=0;
		for(is=0;is<=n-1;is++)
		{
			for(js=0;js<=n-1;js++)
			{
				if((is==x&&js>y)||(is>x))
				{
					if(Board[is][js]=='.')
					{
						//printf("%d %d %d\n",x,y,cnt);
	//					printf("%d %d %d\n",is,js,cnt);
	//					OutputBoard();
						DFS(is,js,cnt+1);
					}
				}
			}
		}
		
		//recover the global var
		//vertical extension
		for(i=i-1;i>=x+1;i--)
		{
			Board[i][y]='.';
		}
		for(i1=i1+1;i1<=x-1;i1++)
		{
			Board[i1][y]='.';
		}
		//horizontal extension
		for(j=j-1;j>=y+1;j--)
		{
			Board[x][j]='.';
		}
		for(j1=j1+1;j1<=y-1;j1++)
		{
			Board[x][j1]='.';
		}
		Board[x][y]='.';
	}
	return(0);
}

/*for test*/
int test()
{
	return(0);
}

/*main process*/
int MainProc()
{
	while(scanf("%d",&n)!=EOF&&n>0)
	{
		getchar();
		int i=0,j=0;
		Max=0;
		for(i=0;i<=n-1;i++)
		{
			for(j=0;j<=n;j++)//the nth is the '\n'
			{
				c=getchar();
				if(c=='\n')
				{
					break;
				}
				else
				{
					Board[i][j]=c;
				}
			}
		}
		for(i=0;i<=n-1;i++)
		{
			for(j=0;j<=n-1;j++)
			{
				if(Board[i][j]=='.')
				{
					if(i<=n-1&&j<=n-1)
					{
						DFS(i,j,1);//place the 1(first) rook to the position (i,j)
						//OutputBoard();
					}
				}
			}
		}
		printf("%d\n",Max);
	}
	return(0);
}

int main()
{
	MainProc();
	return(0);
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值