POJ 1315

Posted 难得~翛宁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1315相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

POJ2778DNA Sequence(AC自动机)

POJ3691DNA repair(AC自动机,DP)

18.06.03 POJ 4126:DNA 15年程设期末05(状压DP)

如何刷新 FragmentPagerAdapter 的片段?

poj 1011 sticks 解题。

如何在片段中使用与活动中不同的菜单?