zoukankan      html  css  js  c++  java
  • 515D

    D. Drazil and Tiles
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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".

    题意:将每一对  .   换成闭合的菱形。竖直方向是^v,水平方向是<>。如果不能确定唯一的图像则输出 “Not unique” 。

    思路:

       求出每一个.上下左右的连通度数,如果为1,则该点放置的符号是唯一确定的,且与该点相邻的点放置相应的符号,将该点连通度数标记为0,然后更新相邻点的连通度数(相邻点的连通度减一)。判断是否还有不能确定的点。

      1 #include "iostream"
      2 #include "string"
      3 #include "queue"
      4 #include "algorithm"
      5 #include "queue"
      6 #include "string.h"
      7 #include "cstdio"
      8 using namespace std;
      9 
     10 char tile[2][5] = {{"^v<>"},{"v^><"}};
     11 int dx[] = { -1, 1, 0, 0};
     12 int dy[] = { 0, 0, -1, 1};
     13 char G[2010][2010];
     14 int deg[2010][2010];
     15 int n, m;
     16 int cnt;
     17 int x, y, nx, ny, tx, ty;
     18 queue< pair<int,int> > q;
     19 
     20 int get_deg(int x,int y) 
     21 {
     22        int ans = 0;
     23        if (G[x + 1][y] == '.') ans++;
     24        if (G[x - 1][y] == '.') ans++;
     25        if (G[x][y + 1] == '.') ans++;
     26        if (G[x][y - 1] == '.') ans++;
     27        return ans;
     28 }
     29 
     30 void show ()
     31 {
     32      for (int i = 1;i <= n; ++ i) {
     33                 for (int j = 1;j <= m; ++ j)
     34                 cout << G[i][j];
     35                 cout << endl;
     36             }
     37             //cout << "************************************" << endl;
     38 }
     39 
     40 bool bfs()
     41 {
     42      int i;
     43      pair<int,int> tmp;
     44      while (!q.empty()) {
     45            tmp = q.front();
     46            q.pop();
     47            x = tmp.first;
     48            y = tmp.second;
     49            if (cnt == 0) return true;
     50            for (i = 0;i < 4; ++ i) {
     51                nx = x + dx[i];
     52                ny = y + dy[i];
     53                if (G[nx][ny] == '.') {
     54                   G[x][y] = tile[1][i];
     55                   G[nx][ny] = tile[0][i];
     56                   deg[x][y] = 0;
     57                   deg[nx][ny] = 0;
     58                   cnt -= 2;
     59                   for (int i = 0;i < 4; ++ i) {
     60                       tx = nx + dx[i];
     61                       ty = ny + dy[i];
     62                       if (G[tx][ty] == '.') {
     63                          deg[tx][ty] --;
     64                          if (deg[tx][ty] == 1)
     65                             q.push(make_pair(tx,ty));
     66                       } 
     67                   }
     68                }
     69                
     70            }
     71      }
     72      if (cnt)
     73         return false;
     74      return true;
     75 }
     76 
     77 
     78 int main()
     79 {
     80     cin >> n >> m;
     81     for (int i = 1;i <= n;++ i)
     82         cin >> G[i]+1;
     83     for (int i = 1;i <= n; ++ i) {
     84         for (int j = 1;j <= m; ++ j) {
     85             if (G[i][j] == '.' ) {
     86                cnt++;
     87                deg[i][j] = get_deg(i,j);
     88                if (deg[i][j] == 1)
     89                q.push(make_pair(i,j));              
     90             }
     91         }
     92     }
     93     bool ok = true;
     94     ok = bfs();
     95     if (ok) {
     96             show();
     97     }
     98     else 
     99          puts("Not unique");
    100     //system("pause");
    101     return 0;
    102 }
  • 相关阅读:
    2014年广州区域赛k题解
    2014年广州区域赛e题解
    2014年广州区域赛i题解
    最大化平均值问题
    codeforces 976e 题解
    maven
    机器学习入门
    拟合
    插值
    熵权法
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4299051.html
Copyright © 2011-2022 走看看