码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1315

时间:2018-04-10 21:55:31      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:which   vertica   namespace   ace   dir   i++   open   his   ini   

Description

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 4x4) 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 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.

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

思路就是dfs+回溯,但是回溯时候标记的消去要注意,别把上层或上层以上的标记消去。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<stdlib.h>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<ctype.h>
#define zuida 100000
using namespace std;
#define INF 0x3f3f3f3f
char zh[10][10];
int vis[10][10];
int maxn=0;int n;
void dfs(int a,int b,int c)
{
    int i,j;
    maxn=max(maxn,c);
    for(i=a;i<n;i++)
    for(j=0;j<n;j++)
    {
        if(zh[i][j]==.&&!vis[i][j])
        {
            int k;
            for(k=i;k<n;k++)
            {
                if(zh[k][j]==X)break;
                vis[k][j]++;
            }
            for(k=i;k>=0;k--)
            {
                if(zh[k][j]==X)break;
                vis[k][j]++;
            }
            for(k=j;k<n;k++)
            {
                if(zh[i][k]==X)break;
                vis[i][k]++;
            }
            for(k=j;k>=0;k--)
            {
                if(zh[i][k]==X)break;
                vis[i][k]++;
            }

            dfs(i,j,c+1);

            for(k=i;k<n;k++)
            {
                if(zh[k][j]==X)break;
                vis[k][j]--;
            }
            for(k=i;k>=0;k--)
            {
                if(zh[k][j]==X)break;
                vis[k][j]--;
            }
            for(k=j;k<n;k++)
            {
                if(zh[i][k]==X)break;
                vis[i][k]--;
            }
            for(k=j;k>=0;k--)
            {
                if(zh[i][k]==X)break;
                vis[i][k]--;
            }

        }
    }
}
int main()
{

    while(cin>>n&&n)
    {
        maxn=0;
        int i,j;
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
        {
            getchar();
            for(j=0;j<n;j++)
            cin>>zh[i][j];
        }
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                dfs(i,j,0);
        cout<<maxn<<endl;
    }
}

大佬标记的简化用了一个函数

const int Dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};  
  
int N,Ans,HashMap[10][10];  
  
char Map[10][10];  
  
void UpdateHashMap(int Row,int Col,int Val)  
{  
    for(int n=0;n<4;++n)  
    {  
        int tmprow=Row;  
        int tmpcol=Col;  
        while(1)  
        {  
            if(!(0<=tmprow&&tmprow<N))   { break; }  
            if(!(0<=tmpcol&&tmpcol<N))   { break; }  
            if(Map[tmprow][tmpcol]!=.) { break; }  
            HashMap[tmprow][tmpcol]+=Val;  
            tmprow+=Dir[n][0];  
            tmpcol+=Dir[n][1];  
        }  
    }  
}  

 

POJ 1315

标签:which   vertica   namespace   ace   dir   i++   open   his   ini   

原文地址:https://www.cnblogs.com/SparkPhoneix/p/8782699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!