天天看點

UVA Don't Get Rooked  Don't Get Rooked 

題目如下:

 Don't Get Rooked 

In chess, the rook is a piece that can move any number of squaresvertically or horizontally. In this problem we will consider smallchess boards (at most 4

UVA Don't Get Rooked  Don't Get Rooked 

4) that can also contain walls through whichrooks cannot move. The goal is to place as many rooks on a board aspossible so that no two can capture each other. A configuration ofrooks is legal provided that no two rooks are on the samehorizontal row or vertical column unless there is at least one wallseparating them.

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

UVA Don't Get Rooked  Don't Get Rooked 

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 theboard in a legal configuration.

Input 

The input file contains one or more board descriptions, followed bya line containing the number 0 that signals the end of the file. Eachboard description begins with a line containing a positive integer nthat is the size of the board; n will be at most 4. The next nlines each describe one row of the board, with a ` .' indicating anopen space and an uppercase ` X' indicating a wall. There are nospaces in the input file.

Output 

For each test case, output one line containing themaximum number of rooks that can be placed on the boardin 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
      

跟八皇後問題差不多,就是多了牆而已,這樣不一定每行每列隻有一個點,中間可能隔着牆。是以用一個函數判斷兩點之間是否有牆。用一個函數判斷一個點是否能被放置,能夠的條件是它與所在行所在列的任何一個已經放置的點之間都有牆。有了這個函數後就可以對每個點展開DFS,更新最大值。

AC的代碼如下:

#include 
   
    

using namespace std;
int n;
int vis[5][5];
char G[5][5];
bool iswallrow(int y1,int y2,int x)
{
    for(int i=y1+1; i
    
     y)
            {
                if(!iswallrow(y,i,x))
                    return false;
            }
            else
            {
                if(!iswallrow(i,y,x))
                    return false;
            }
        }
    for(int i=0; i
     
      x)
            {
                if(!iswallco(x,i,y))
                    return false;
            }
            else
            {
                if(!iswallco(i,x,y))
                    return false;
            }
        }
    return true;
}
int MAX;

void dfs(int cnt)
{
    for(int i=0; i
      
       >n)
    {
        if(!n)
            break;
        memset(G,0,sizeof G);

        for(int i=0; i
       
        >G[i][j]; } MAX=0; dfs(1); cout<
        
         <
         
        
       
      
     
    
   
           
上一篇: UVA 23 out of 5