Don't Get Rooked uva 回溯

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

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

解决方案:回溯法,比直接枚举快很多

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char Map[10][10];
bool vis[10][10];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,Max;
bool confict(int i,int j){

    for(int e=0;e<4;e++)
    {
        int dx=i+dir[e][0];
        int dy=j+dir[e][1];
        while(dx>=0&&dx<n&&dy>=0&&dy<n){
            if(Map[dx][dy]=='X') break;
            if(vis[dx][dy]) return true;
            dx+=dir[e][0];
            dy+=dir[e][1];
        }
    }
    return false;
}///判断是否有冲突
void dfs(int x,int y,int sum){
   for(int i=x;i<n;i++)
    for(int j=0;j<n;j++)
   {
       if(!vis[i][j]&&Map[i][j]=='.'&&!confict(i,j))
       {
           vis[i][j]=true;
           dfs(i,j,sum+1);
           vis[i][j]=false;
       }
   }
   if(sum>Max) Max=sum;

}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%s",Map[i]);
        memset(vis,false,sizeof(vis));
        Max=0;
        dfs(0,0,0);
        cout<<Max<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值