uva 639 Don't Get Rooked

来源:互联网 发布:网页版的淘宝 编辑:程序博客网 时间:2024/06/16 00:19

原题:
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 × 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的棋盘,棋盘上面写X看作一堵墙,其他的地方放置棋子,其中棋子可以横向和纵向相互攻击,但是如果有墙隔着,那么就不会攻击。现在问你在不相互攻击的情况下,棋盘最多能放多少个棋子?

#include <bits/stdc++.h>using namespace std;fstream in,out;const double inf=99999999;struct node{    int x,y;};char board[5][5];int n,ans,tmp;bool judge(int x,int y)//判断横向纵向是否有墙或者有其他棋子{    for(int i=x;i<n;i++)    {        if(board[i][y]=='#')            return false;        if(board[i][y]=='X')            break;    }    for(int i=0;i<x;i++)    {        if(board[i][y]=='#')            return false;        if(board[i][y]=='X')            break;    }    for(int i=y;i<n;i++)    {        if(board[x][i]=='#')            return false;        if(board[x][i]=='X')            break;    }    for(int i=0;i<y;i++)    {        if(board[x][i]=='#')            return false;        if(board[x][i]=='X')            break;    }    return true;}void dfs(int x,int y,int num){    ans=max(ans,num);    for(int i=0;i<n;i++)    {        for(int j=0;j<n;j++)        {            if(board[i][j]=='.'&&judge(i,j))            {                board[i][j]='#';                dfs(i,j,num+1);                board[i][j]='.';            }        }    }}int main(){    ios::sync_with_stdio(false);//  in.open("data.txt");//  out.open("output.txt");    while(cin>>n,n)    {        cin.get();        for(int i=0;i<n;i++)            cin>>board[i];        ans=0;        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                if(board[i][j]=='.'&&judge(i,j))                {                    board[i][j]='#';                    dfs(i,j,1);                    board[i][j]='.';                }            }        }        cout<<ans<<endl;    }//    in.close();//    out.close();    return 0;}

解答:
差不多是个裸的DFS题目,直接搜索即可。

0 0