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.
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.
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.
3 3
...
.*.
...
Not unique
4 4
..**
*...
*.**
....
<>**
*^<>
*v**
<><>
2 4
*..*
....
*<>*
<><>
1 1
.
Not unique
1 1
*
*
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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...
- 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. ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...
- Codeforces Round #292 (Div. 1) C - Drazil and Park
C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...
- 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 ...
- #292 (div.2) D.Drazil and Tiles (贪心+bfs)
Description Drazil created a following problem about putting × tiles into an n × m grid: "The ...
- Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...
随机推荐
- Android 布局 ViewGroup
布局 res/layout 命名规则(全部小写) activity_ fragment_ item_ 基础组件 com.android.widget包下 父类View view:屏幕上一块矩阵区域 能 ...
- 标C编程笔记day06 动态分配内存、函数指针、可变长度參数
动态分配内存:头文件 stdlib.h malloc:分配内存 calloc:分配内存,并清零 realloc:调整已分配的内存块大小 演示样例: in ...
- Swift Core Data 图片存储与读取Demo
实体的模型定义: 实体的class定义: @objc(ImageEntity) class ImageEntity: NSManagedObject { @NSManaged var imageDat ...
- [转] gdb的基本工作原理
转自: http://www.spongeliu.com/linux/howgdbwork/ 还是面某M的时候,面试官问我:“用过gdb么?” 答:“用过,调了两年bug了”.“那好,给我解释下gdb ...
- Ajax提交Form表单及文件上传
刚刚申请下来的博客,写得第一篇.有点小激动,本人以前是一名工业3D设计师突然有些变故做上了JavaWeb开发: 前几天,发现了一些小问题.我在写后台管理页面时,需要上传一张图片.于是我就用很普通的Fo ...
- Python元组、列表--笔记
<Python3 程序开发指南> 序列包括元组和列表,首先,我们介绍元组. 元组--tuple 元组为有序的序列,元组和字符串一样也是固定的,不能替换或删除其中的任意数据项.如果需要修改应 ...
- C# 在右下角弹出窗口
窗口代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- java多线程心得
多并发的时候,在什么情况下必须加锁?如果不加锁会产生什么样的后果. 加锁的场景跟java的new thread和Runnable的关系是什么? 看看java的concurrentMap源码. 还有sp ...
- Debugging Failed Because Integrated Windows Authentication Is Not Enabled
To enable integrated Windows authentication Log onto the Web server using an administrator account. ...
- cocoapods出现Diff: /../Podfile.lock: No such file or directory错误
第一种解决方法: 关闭Xcode,重新执行pod install,之后再重新打开Xcode运行. 第二种解决方法: 删除以下文件: xcworkspacePodfile.lockPods文件夹~/Li ...