`
473687880
  • 浏览: 484784 次
文章分类
社区版块
存档分类
最新评论

Don't Get Rooked UVA 639

阅读更多



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 islegalprovided 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 integernthat is the size of the board;nwill be at most 4. The nextnlines 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



Miguel A. Revilla
2000-01-17

和八皇后类似,使用回溯,只是判断是否冲突的方法不一样。

#include<iostream>
#include<cstring>

using namespace std;

char grid[6][6],vis[6][6];
int maxnum,n,num;

bool judge(int posx,int posy)
{
    int i,j;
    for(i=posx;i<n;i++)
    {
        if(vis[i][posy]==1&&grid[i][posy]=='.') return false;
        else if(grid[i][posy]=='X') break;
    }
    for(i=posx;i>=0;i--)
    {
        if(vis[i][posy]==1&&grid[i][posy]=='.') return false;
        else if(grid[i][posy]=='X') break;
    }
    for(i=posy;i<n;i++)
    {
        if(vis[posx][i]==1&&grid[posx][i]=='.') return false;
        else if(grid[posx][i]=='X') break;
    }
    for(i=posy;i>=0;i--)
    {
        if(vis[posx][i]==1&&grid[posx][i]=='.') return false;
        else if(grid[posx][i]=='X') break;
    }
    return true;
}

void search(int posx,int posy)
{
    int i,j;
    num++;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(judge(i,j)&&grid[i][j]=='.'&&vis[i][j]==0)
            {
                vis[i][j]=1;
                search(i,j);
                num--;
                vis[i][j]=0;
            }
        }
    }
    if(num>maxnum) maxnum=num;
}

int main()
{
    while(cin>>n&&n)
    {
        int i,j,k;
        memset(grid,0,sizeof(grid));
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                cin>>grid[i][j];
        maxnum=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(grid[i][j]!='X')
                {
                    memset(vis, 0, sizeof(vis));
                    vis[i][j]=1;
                    num=0;
                    search(i,j);
                    vis[i][j]=0;
                }
            }
        }
        cout<<maxnum<<endl;
    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics