zoukankan      html  css  js  c++  java
  • CF 525D Arthur and Walls

    D. Arthur and Walls
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of the city with a nice price.

    Plan of the apartment found by Arthur looks like a rectangle n × m consisting of squares of size 1 × 1. Each of those squares contains either a wall (such square is denoted by a symbol "*" on the plan) or a free space (such square is denoted on the plan by a symbol ".").

    Room in an apartment is a maximal connected area consisting of free squares. Squares are considered adjacent if they share a common side.

    The old Arthur dream is to live in an apartment where all rooms are rectangles. He asks you to calculate minimum number of walls you need to remove in order to achieve this goal. After removing a wall from a square it becomes a free square. While removing the walls it is possible that some rooms unite into a single one.

    Input

    The first line of the input contains two integers n, m (1 ≤ n, m ≤ 2000) denoting the size of the Arthur apartments.

    Following n lines each contain m symbols — the plan of the apartment.

    If the cell is denoted by a symbol "*" then it contains a wall.

    If the cell is denoted by a symbol "." then it this cell is free from walls and also this cell is contained in some of the rooms.

    Output

    Output n rows each consisting of m symbols that show how the Arthur apartment plan should look like after deleting the minimum number of walls in order to make each room (maximum connected area free from walls) be a rectangle.

    If there are several possible answers, output any of them.

    Sample test(s)
    input
    5 5
    .*.*.
    *****
    .*.*.
    *****
    .*.*.
    output
    .*.*.
    *****
    .*.*.
    *****
    .*.*.
    input
    6 7
    ***.*.*
    ..*.*.*
    *.*.*.*
    *.*.*.*
    ..*...*
    *******
    output
    ***...*
    ..*...*
    ..*...*
    ..*...*
    ..*...*
    *******
    input
    4 5
    .....
    .....
    ..***
    ..*..
    output
    .....
    .....
    .....
    .....

     题意:将‘*’换成‘.’保证图中所有联通的‘.’组成的形状为举行,不包跨一个单独的‘.’ 然后输出变换后的图。

    思路:在2*2的方格中找只有一个*的,换成'.'。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<string>
     6 #include<queue>
     7 #include<algorithm>
     8 #include<map>
     9 #include<iomanip>
    10 #include<climits>
    11 #include<string.h>
    12 #include<numeric>
    13 #include<cmath>
    14 #include<stdlib.h>
    15 #include<vector>
    16 #include<stack>
    17 #include<set>
    18 #define FOR(x, b, e)  for(int x=b;x<=(e);x++)
    19 #define REP(x, n)     for(int x=0;x<(n);x++)
    20 #define INF 1e7
    21 #define MAXN 100010
    22 #define maxn 1000010
    23 #define Mod 1000007
    24 #define N 2010
    25 using namespace std;
    26 typedef long long LL;
    27 
    28 int n, m;
    29 char G[N][N];
    30 
    31 void DFS(int x, int y)
    32 {
    33     if (x == n || y == m || x == 0 || y == 0)return;
    34     int s = 0;
    35     REP(i, 2)
    36         REP(j, 2)
    37         s += (G[x + i][y + j] == '*');
    38     if (s == 1)
    39     {
    40         REP(i, 2)
    41             REP(j, 2)
    42             G[x + i][y + j] = '.';
    43         FOR(i, -1, 1)
    44             FOR(j, -1, 1)
    45             DFS(x + i, y + j);
    46     }
    47 }
    48 int main()
    49 {
    50     scanf("%d%d", &n, &m);
    51     FOR(i, 1, n)
    52         FOR(j, 1, m)
    53         scanf(" %c", &G[i][j]);
    54     FOR(i, 1, n)
    55         FOR(j, 1, m)
    56         DFS(i, j);
    57     FOR(i, 1, n)
    58     {
    59         FOR(j, 1, m)
    60             printf("%c", G[i][j]);
    61         puts("");
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    .NET设计模式外观模式(Façade Pattern)
    .NET设计模式创建型模式专题总结(Creational Pattern)
    MFC深入浅出消息映射的实现
    .NET设计模式代理模式(Proxy Pattern)
    .NET设计模式工厂方法模式(Factory Method)
    Web Services Security (转)
    MYSQL数据库的查询优化技术
    U盘插入拔出提示
    SQL存储过程(ASP.NET)
    在SQL Server中使用种子表生成流水号注意顺序
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4373678.html
Copyright © 2011-2022 走看看