• HDU


    Description

    Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. 

    A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening. 

    Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets. 

    The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through. 

    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 blockhouses 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 map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration. 

    Input

    The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, 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 blockhouses that can be placed in the city 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

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045

    ********************************************

    题意:n乘n 的矩阵,一般同一行同一列不能放两个'O',如果有'X'分割开来则可以,问你在'.'处最多可以放多少个'O'。

    分析:直接暴力搜索。

    AC代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<queue>
     5 #include<algorithm>
     6 #include<time.h>
     7 #include<stack>
     8 #include<vector>
     9 using namespace std;
    10 #define N 120000
    11 #define INF 0x3f3f3f3f
    12 
    13 vector<int >Q;
    14 
    15 int sum,n;
    16 char maps[10][10];
    17 
    18 int p(int x,int y)
    19 {
    20     for(int i=x;i>=0;i--)
    21     {
    22         if(maps[i][y]=='O')
    23             return 0;
    24         if(maps[i][y]=='X')
    25             break;
    26     }
    27 
    28     for(int i=y;i>=0;i--)
    29     {
    30         if(maps[x][i]=='O')
    31             return 0;
    32         if(maps[x][i]=='X')
    33             break;
    34     }
    35     return 1;
    36 }
    37 
    38 void dfs(int x,int y)
    39 {
    40     if(x==n*n)
    41     {
    42         sum=max(sum,y);
    43         return ;
    44     }
    45     else
    46     {
    47         int row=x/n;
    48         int col=x%n;
    49         if(maps[row][col]=='.'&&p(row,col))
    50         {
    51             maps[row][col]='O';
    52             dfs(x+1,y+1);
    53             maps[row][col]='.';
    54         }
    55         dfs(x+1,y);
    56     }
    57 }
    58 
    59 int main()
    60 {
    61     int i;
    62 
    63     while(scanf("%d", &n),n)
    64     {
    65         sum=0;
    66 
    67         for(i=0;i<n;i++)
    68             scanf("%s", maps[i]);
    69 
    70         dfs(0,0);
    71 
    72         printf("%d
    ", sum);
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    浅看网络结构与TCP/IP协议栈
    moectf-re WP
    开启博客之旅
    初始C++类和对象
    最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件
    Eclipse+pydev环境搭建
    单例模式应用 | Shared_ptr引用计数管理器
    随笔——统计单词个数
    随笔——算法笔记(未整理)
    C++ | 智能指针初探
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5799357.html
Copyright © 2020-2023  润新知