• Codeforces Round #292 (Div. 1)


    B. Drazil and Tiles
     

    Drazil created a following problem about putting 1 × 2 tiles into an n × m grid:

    "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."

    But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ".

    Drazil found that the constraints for this task may be much larger than for the original task!

    Can you solve this new problem?

    Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 2000).

    The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.

    Output

    If there is no solution or the solution is not unique, you should print the string "Not unique".

    Otherwise you should print how to cover all empty cells with 1 × 2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

    Sample test(s)
    Input
    3 3
    ...
    .*.
    ...
    Output
    Not unique
    Input
    4 4
    ..**
    *...
    *.**
    ....
    Output
    <>**
    *^<>
    *v**
    <><>
    Input
    2 4
    *..*
    ....
    Output
    *<>*
    <><>
    Input
    1 1
    .
    Output
    Not unique
    Input
    1 1
    *
    Output
    *
    Note

    In the first case, there are indeed two solutions:


    <>^
    ^*v
    v<>

    and


    ^<>
    v*^
    <>v

    so the answer is "Not unique".


    题意:用一些1*2的瓷砖覆盖m*n的网格(有障碍),问方案是否唯一,是则输出覆盖的方案。(1 ≤ n, m ≤ 2000).

    这题像极了某次cf的一道题(判断网格中是否有环),不过那题数据比较小,直接暴力的拓扑排序即可。

    而这题为了在o(m*n)内构造出解,需要维护一个保存所有度为1的点的队列,以及一个保存所有点的度数的数组,每次将一个点出队,同时将唯一的那个与该点相邻的点删掉,然后更改删掉的点的相邻的点的度数(常数时间),度为1的话即入队。

    当发现队列空的时候,表明找不到度为1的点了,这时要不是全部覆盖完了,就是存在环或者单独的点(这种情况显然是无解或者多解),再循环判断一遍即可。


     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <queue>
     4 using namespace std;
     5 #define MAXN 2010
     6 
     7 struct Point
     8 {
     9     int x,y;
    10 };
    11 char G[MAXN][MAXN];
    12 int degree[MAXN][MAXN]={0};
    13 int m,n;
    14 bool isin(int x,int y)
    15 {
    16     return x>=0 && x<m && y>=0 && y<n;
    17 }
    18 
    19 int dx[]={1,0,-1, 0};
    20 int dy[]={0,1, 0,-1};
    21 int getDegree(int x,int y)
    22 {
    23     if(G[x][y]=='*')
    24         return 0;
    25     int sum=0;
    26     for(int i=0;i<4;i++)
    27     {
    28         int newx=x+dx[i];
    29         int newy=y+dy[i];
    30         sum+=(isin(newx,newy) && G[newx][newy]!='*');
    31     }
    32     return sum;
    33 }
    34 
    35 int main()
    36 {
    37     cin>>m>>n;
    38     for(int i=0;i<m;i++)
    39         scanf("%s",G[i]);
    40 
    41     queue<Point> Q;
    42     for(int i=0;i<m;i++)
    43         for(int j=0;j<n;j++)
    44         {
    45             degree[i][j]=getDegree(i,j);
    46             if(degree[i][j] == 1)
    47                 Q.push(Point{i,j});
    48         }
    49 
    50 //    int dx[]={1,0,-1, 0};
    51 //    int dy[]={0,1, 0,-1};
    52     char oldtype[]={'^','<','v','>'};
    53     char newtype[]={'v','>','^','<'};
    54     while(!Q.empty())
    55     {
    56         Point x=Q.front();
    57         Q.pop();
    58         degree[x.x][x.y]=0;
    59         for(int i=0;i<4;i++)
    60         {
    61             int newx=x.x+dx[i];
    62             int newy=x.y+dy[i];
    63             if(isin(newx, newy) && degree[newx][newy])
    64             {
    65                 degree[newx][newy]=0;
    66                 G[x.x][x.y]=oldtype[i];
    67                 G[newx][newy]=newtype[i];
    68                 // 更新与(newx, newy)相邻的点的度数
    69                 for(int j=0;j<4;j++)
    70                 {
    71                     int curx=newx+dx[j];
    72                     int cury=newy+dy[j];
    73                     if(isin(curx, cury) && degree[curx][cury])
    74                     {
    75                         degree[curx][cury]--;
    76                         if(degree[curx][cury]==1)
    77                             Q.push(Point{curx, cury});
    78                     }
    79                 }
    80                 break;
    81             }
    82         }
    83     }
    84 
    85     for(int i=0;i<m;i++)
    86         for(int j=0;j<n;j++)
    87         {
    88             if(G[i][j]=='.')
    89             {
    90                 puts("Not unique");
    91                 return 0;
    92             }
    93         }
    94 
    95     for(int i=0;i<m;i++)
    96         puts(G[i]);
    97 
    98     return 0;
    99 }
  • 相关阅读:
    时间安排还是很不合理
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
    还是很水啊!!!
    pragma mark
    IOS应用发布NSLog的如何注释
    设定时间让应用从后台回来是否重新启动应用
    Mac 上SVN上传.a文件
  • 原文地址:https://www.cnblogs.com/oneshot/p/4295714.html
Copyright © 2020-2023  润新知