zoukankan      html  css  js  c++  java
  • Fire Net

    Fire Net
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     题意:n*n矩阵地图放东西,每一行每一列只能放一个,除非有墙隔着。问地图最多可以放几个。
    行列关系,匹配。一行只能匹配一列,有墙隔着是另一行或另一列
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     9 //int main(int argc, char** argv) 
    10 
    11 #define N 1005
    12 
    13 struct node
    14 {
    15     int x, y;
    16 }P[N][N];
    17 
    18 int x, y, n, g[N][N], used[N], vis[N], ans;
    19 char s[N][N];
    20 
    21 int found(int u)
    22 {
    23     for(int i = 1; i <= y; i++)
    24     {
    25         if(!vis[i] && g[u][i])  // 可以放
    26         {
    27             vis[i] = 1;
    28             if(!used[i] || found(used[i]))
    29             {
    30                 used[i] = u;
    31                 return true;
    32             }
    33         }
    34     }
    35     return false;
    36 }
    37 
    38 int main()
    39 {    
    40     while(scanf("%d", &n), n)
    41     {
    42         ans = x = y = 0;
    43         memset(g, 0, sizeof(g));
    44         memset(used, 0, sizeof(used));
    45         
    46         for(int i = 0; i < n; i++)
    47             scanf("%s", s[i]);
    48             
    49         for(int i = 0; i < n; i++)
    50         {
    51             for(int j = 0; j < n; j++)
    52             {
    53                 if(s[i][j] == '.')
    54                 {
    55                     if(j == 0 || s[i][j-1] == 'X')   // 前边是墙就可以另开一行或列放东西
    56                         x++;
    57                     P[i][j].x = x;
    58                 }
    59                 if(s[j][i] == '.')
    60                 {
    61                     if(j == 0 || s[j-1][i] == 'X')
    62                         y++;
    63                     P[j][i].y = y;
    64                 }
    65             } 
    66         }
    67         
    68         for(int i = 0; i < n; i++)
    69         for(int j = 0; j < n; j++)
    70         if(s[i][j] == '.')
    71         g[P[i][j].x][P[i][j].y] = 1;  // 如果这个位置可以放东西,当前行列可以匹配
    72         
    73         for(int i = 1; i <= x; i++)
    74         {
    75             memset(vis, 0, sizeof(vis));
    76             if(found(i))
    77                 ans++;
    78         }
    79         
    80         printf("%d
    ", ans);
    81     }
    82     return 0;
    83 }
    让未来到来 让过去过去
  • 相关阅读:
    20145314郑凯杰《网络对抗技术》多文件捆绑(压缩捆绑)的实现
    20145314郑凯杰《网络对抗技术》文件绑定技术研究与实践 总纲
    20145314郑凯杰《网络对抗技术》实验9 web安全基础实践
    20145314郑凯杰《网络对抗技术》实验8 WEB基础实践
    20145314郑凯杰《网络对抗技术》实验7 网络欺诈技术防范
    实验6 信息搜集与漏洞扫描
    课程设计个人报告——基于ARM实验箱的捕鱼游戏的设计与实现
    课程设计小组报告——基于ARM实验箱的捕鱼游戏的设计与实现
    20145236《网络对抗》Exp9 web安全基础实践
    20145236《网络对抗》Exp8 WEB基础实践
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4721758.html
Copyright © 2011-2022 走看看