zoukankan      html  css  js  c++  java
  • POJ 1315 Don't Get Rooked

    Don't Get Rooked
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 2086   Accepted: 1325

    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 <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int ans;
    char maze[10][10];
    int visited[10][10];
    int n;
    
    void DFS(int sum)
    {
        ans = max(ans, sum);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                bool bflag = true;
                for (int k = i; k < n; k++)
                {
                    if (visited[k][j])
                    {
                        bflag = false;
                        break;
                    }
                    else
                    {
                        if (maze[k][j] == 'X')
                        {
                            break;
                        }
                    }
                }
                for (int k = i - 1; k >= 0; k--)
                {
                    if (visited[k][j])
                    {
                        bflag = false;
                        break;
                    }
                    else
                    {
                        if (maze[k][j] == 'X')
                        {
                            break;
                        }
                    }
                }
                for (int k = j; k < n; k++)
                {
                    if (visited[i][k])
                    {
                        bflag = false;
                        break;
                    }
                    else
                    {
                        if (maze[i][k] == 'X')
                        {
                            break;
                        }
                    }
                }
                for (int k = j - 1; k >= 0; k--)
                {
                    if (visited[i][k])
                    {
                        bflag = false;
                        break;
                    }
                    else
                    {
                        if (maze[i][k] == 'X')
                        {
                            break;
                        }
                    }
                }
                if (bflag && maze[i][j] != 'X')
                {
                    visited[i][j] = 1;
                    DFS(sum + 1);
                    visited[i][j] = 0;
                }
            }
        }
    }
    
    int main()
    {
        while(cin>>n)
        {
            if (n == 0)
            {
                break;
            }
            ans = 0;
            for (int i = 0; i < n; i++)
            {
                cin>>maze[i];
            }
            memset(visited, 0, sizeof(visited));
            DFS(0);
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    virtual
    微软MBS intern笔试
    Ubuntu Linux Green hand
    Coding style
    abstract
    Jquery Ajax请求标准格式
    Hashtable的简单实用
    C#中GET和POST的简单区别
    WIN7 64位机与32位机有什么区别
    一个加密解密类
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3240249.html
Copyright © 2011-2022 走看看