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的点了,这时要不是全部覆盖完了,就是存在环或者单独的点(这种情况显然是无解或者多解),再循环判断一遍即可。


 #include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
#define MAXN 2010 struct Point
{
int x,y;
};
char G[MAXN][MAXN];
int degree[MAXN][MAXN]={};
int m,n;
bool isin(int x,int y)
{
return x>= && x<m && y>= && y<n;
} int dx[]={,,-, };
int dy[]={,, ,-};
int getDegree(int x,int y)
{
if(G[x][y]=='*')
return ;
int sum=;
for(int i=;i<;i++)
{
int newx=x+dx[i];
int newy=y+dy[i];
sum+=(isin(newx,newy) && G[newx][newy]!='*');
}
return sum;
} int main()
{
cin>>m>>n;
for(int i=;i<m;i++)
scanf("%s",G[i]); queue<Point> Q;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
degree[i][j]=getDegree(i,j);
if(degree[i][j] == )
Q.push(Point{i,j});
} // int dx[]={1,0,-1, 0};
// int dy[]={0,1, 0,-1};
char oldtype[]={'^','<','v','>'};
char newtype[]={'v','>','^','<'};
while(!Q.empty())
{
Point x=Q.front();
Q.pop();
degree[x.x][x.y]=;
for(int i=;i<;i++)
{
int newx=x.x+dx[i];
int newy=x.y+dy[i];
if(isin(newx, newy) && degree[newx][newy])
{
degree[newx][newy]=;
G[x.x][x.y]=oldtype[i];
G[newx][newy]=newtype[i];
// 更新与(newx, newy)相邻的点的度数
for(int j=;j<;j++)
{
int curx=newx+dx[j];
int cury=newy+dy[j];
if(isin(curx, cury) && degree[curx][cury])
{
degree[curx][cury]--;
if(degree[curx][cury]==)
Q.push(Point{curx, cury});
}
}
break;
}
}
} for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(G[i][j]=='.')
{
puts("Not unique");
return ;
}
} for(int i=;i<m;i++)
puts(G[i]); return ;
}

Codeforces Round #292 (Div. 1) - B. Drazil and Tiles的更多相关文章

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

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  2. 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 cre ...

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

    题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...

  4. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

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

    题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...

  6. Codeforces Round #292 (Div. 1) C - Drazil and Park

    C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...

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

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

    Description Drazil created a following problem about putting  ×  tiles into an n × m grid: "The ...

  9. Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造

    A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...

随机推荐

  1. 第二节,CCSpriteBatchNode CCSpriteFrameCache

    1,CCSpriteBatchNode 精灵集合类 其中Batch的英文含义是一批,一群的意思.他的对象常常包含了许多的精灵对象,这些精灵对象有一个共同的特点,那就是使用同一张文理图片.虽然是同一个纹 ...

  2. [CSS3] Interactive Pseudo-Classes :link :visited :hover :active

    The interactive pseudo-classes for links (and buttons) allow us to make sure the user knows what ele ...

  3. 机器学习笔记——K-means

    K-means是一种聚类算法,其要求用户设定聚类个数k作为输入參数,因此,在执行此算法前,须要预计须要的簇的个数. 如果有n个点,须要聚到k个簇中.K-means算法首先从包括k个中心点的初始集合開始 ...

  4. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  5. [转] Console命令详解,让调试js代码变得更简单

    http://www.cnblogs.com/see7di/archive/2011/11/21/2257442.html Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上 ...

  6. [转] JS nodeType返回类型

    将HTML DOM中几个容易常用的属性做下记录: nodeName.nodeValue 以及 nodeType 包含有关于节点的信息. nodeName 属性含有某个节点的名称. 元素节点的 node ...

  7. linux系统用户锁定与解锁

    1.使用passwd命令锁定与解锁账号 [root@rhel7 ~]# passwd -l lxj --- -l 锁定 Locking password for user lxj. passwd: S ...

  8. mysql02

    -- 查询课程名称 和年级的名称 -- 非等值连接查询 SELECT subjectname,gradeName FROM `subject`,grade -- 等值连接查询 SELECT subje ...

  9. HTML中常用的列表标签

  10. JAVA多态需要注意的一些问题

    public class MainTest { static class A { public int i; public void f() { System.out.println("AA ...