Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

题目链接:http://codeforces.com/problemset/problem/516/B

一个n*m的方格,‘*‘不能填。给你很多个1*2的尖括号,问你是否能用唯一填法填满方格。

类似topsort,‘.‘与上下左右的‘.‘,的相连。从度为1的点作为突破口。

  1 //#pragma comment(linker, "/STACK:102400000, 102400000")
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cstdio>
  7 #include <vector>
  8 #include <queue>
  9 #include <cmath>
 10 #include <ctime>
 11 #include <list>
 12 #include <set>
 13 #include <map>
 14 using namespace std;
 15 typedef long long LL;
 16 typedef pair <int, int> P;
 17 const int N = 2e3 + 5;
 18 int n, m, tx[] = {-1, 0, 1, 0}, ty[] = {0, -1, 0, 1};
 19 char str[N][N];
 20 int du[N*N];
 21 vector <int> G[N*N];
 22 inline bool judge(int x, int y) {
 23     if(x >= 0 && x < n && y >= 0 && y < m && str[x][y] == ‘.‘)
 24         return true;
 25     return false;
 26 }
 27 inline void get(int x, int y) {
 28     for(int i = 0 ; i < 4 ; ++i) {
 29         int xx = x + tx[i], yy = y + ty[i];
 30         if(judge(xx, yy)) {
 31             G[x*m + y].push_back(xx*m + yy);
 32             du[x*m + y]++;
 33         }
 34     }
 35 }
 36 inline void change(int x, int y) {
 37     if(m == 1) {
 38         if(x < y) {
 39             str[x][0] = ‘^‘;
 40             str[y][0] = ‘v‘;
 41         } else {
 42             str[x][0] = ‘v‘;
 43             str[y][0] = ‘^‘;
 44         }
 45     } else {
 46         if(x - y == -m) {
 47             str[x/m][x%m] = ‘^‘;
 48             str[y/m][y%m] = ‘v‘;
 49         } else if(x - y == m) {
 50             str[x/m][x%m] = ‘v‘;
 51             str[y/m][y%m] = ‘^‘;
 52         } else if(x - y == -1) {
 53             str[x/m][x%m] = ‘<‘;
 54             str[y/m][y%m] = ‘>‘;
 55         } else {
 56             str[x/m][x%m] = ‘>‘;
 57             str[y/m][y%m] = ‘<‘;
 58         }
 59     }
 60 }
 61
 62 int main()
 63 {
 64     int cnt = 0, op = 0;
 65     scanf("%d %d", &n, &m);
 66     for(int i = 0; i < n; ++i) {
 67         scanf("%s", str[i]);
 68     }
 69     for(int i = 0; i < n; ++i) {
 70         for(int j = 0; j < m; ++j) {
 71             if(judge(i, j)) {
 72                 get(i, j);
 73                 ++op;
 74             }
 75         }
 76     }
 77     queue <int> que;
 78     for(int i = 0; i < n*m; ++i) {
 79         if(du[i] == 1)
 80             que.push(i);
 81     }
 82     while(!que.empty()) {
 83         int u = que.front();
 84         que.pop();
 85         du[u]--;
 86         for(int i = 0; i < G[u].size(); ++i) {
 87             int v = G[u][i];
 88             du[v]--;
 89             if(du[v] > 0) {
 90                 cnt++;
 91                 for(int j = 0; j < G[v].size(); ++j) {
 92                     du[G[v][j]]--;
 93                     if(du[G[v][j]] == 1) {
 94                         que.push(G[v][j]);
 95                     }
 96                 }
 97                 du[v] = 0;
 98                 change(u, v);
 99             } else if(du[v] == 0) {
100                 cnt++;
101                 change(u, v);
102             }
103         }
104     }
105     if(cnt * 2 == op) {
106         for(int i = 0; i < n; ++i) {
107             printf("%s\n", str[i]);
108         }
109     } else {
110         printf("Not unique\n");
111     }
112     return 0;
113 }

时间: 2024-10-19 13:14:35

Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)的相关文章

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

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 a

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes 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 occupie

Codeforces Round #292 (Div. 1) - B. Drazil and Tiles

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 tile

Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)

题目地址:codeforces 292 B 用队列维护度数为1的点,也就是可以唯一确定的点,然后每次找v1,v2,并用v2来更新与之相连的点,如果更新后的点度数为1,就加入队列.若最后还有为"."的,说明无解或解不唯一. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

Codeforces Round #292 (Div. 1)---A. Drazil and Factorial

Drazil is playing a math game with Varda. Let's define for positive integer x as a product of factorials of its digits. For example, . First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This n

Codeforces Round #292 (Div. 2) -- B. Drazil and His Happy Friends

B. Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his f

Codeforces Round #292 (Div. 2) C. Drazil and Factorial 515C

C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil is playing a math game with Varda. Let's define  for positive integer x as a product of factorials of its dig

#292 (div.2) D.Drazil and Tiles (贪心+bfs)

Description 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

Codeforces Round #292 (Div. 2 Div. 1)

比赛链接:http://codeforces.com/contest/515  http://codeforces.com/contest/516 A. Drazil and Date time limit per test 1 second memory limit per test 256 megabytes Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. D