您的位置:首页 > 其它

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

2016-08-05 21:00 513 查看
题目链接:http://codeforces.com/problemset/problem/516/B

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

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

//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 2e3 + 5;
int n, m, tx[] = {-1, 0, 1, 0}, ty[] = {0, -1, 0, 1};
char str

;
int du[N*N];
vector <int> G[N*N];
inline bool judge(int x, int y) {
if(x >= 0 && x < n && y >= 0 && y < m && str[x][y] == '.')
return true;
return false;
}
inline void get(int x, int y) {
for(int i = 0 ; i < 4 ; ++i) {
int xx = x + tx[i], yy = y + ty[i];
if(judge(xx, yy)) {
G[x*m + y].push_back(xx*m + yy);
du[x*m + y]++;
}
}
}
inline void change(int x, int y) {
if(m == 1) {
if(x < y) {
str[x][0] = '^';
str[y][0] = 'v';
} else {
str[x][0] = 'v';
str[y][0] = '^';
}
} else {
if(x - y == -m) {
str[x/m][x%m] = '^';
str[y/m][y%m] = 'v';
} else if(x - y == m) {
str[x/m][x%m] = 'v';
str[y/m][y%m] = '^';
} else if(x - y == -1) {
str[x/m][x%m] = '<';
str[y/m][y%m] = '>';
} else {
str[x/m][x%m] = '>';
str[y/m][y%m] = '<';
}
}
}

int main()
{
int cnt = 0, op = 0;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i) {
scanf("%s", str[i]);
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(judge(i, j)) {
get(i, j);
++op;
}
}
}
queue <int> que;
for(int i = 0; i < n*m; ++i) {
if(du[i] == 1)
que.push(i);
}
while(!que.empty()) {
int u = que.front();
que.pop();
du[u]--;
for(int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
du[v]--;
if(du[v] > 0) {
cnt++;
for(int j = 0; j < G[v].size(); ++j) {
du[G[v][j]]--;
if(du[G[v][j]] == 1) {
que.push(G[v][j]);
}
}
du[v] = 0;
change(u, v);
} else if(du[v] == 0) {
cnt++;
change(u, v);
}
}
}
if(cnt * 2 == op) {
for(int i = 0; i < n; ++i) {
printf("%s\n", str[i]);
}
} else {
printf("Not unique\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: