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 ...
随机推荐
- ipcs, ipcrm 命令
ipcs命令 是linux/uinx上提供关于一些进程间通信方式的信息,包括共享内存,消息队列,信号 ipcs用法 ipcs -a 是默认的输出信息 打印出当前系统中所有的进程间通信方式的信息 ip ...
- HDFS集群balance(2)-- 架构概览
转载请注明博客地址:http://blog.csdn.net/suileisl HDFS集群balance,对应版本balance design 6 如需word版本,请QQ522173163联系索要 ...
- 自定义控件(视图)2期笔记05:自定义控件之继承自View(滑动开关)
1. 开关按钮点击效果,如下: 2. 继承已有View实现自定义View 3. 下面通过一个案例实现滑动开关的案例: (1)新建一个新的Android工程,命名为" 开关按钮", ...
- NYOJ -37回文字符串
这道题看了好大会没有思路,上网一搜发现这么简单,但是我为什么就想不到呢,??就是求和它的逆序之后的字符串最长公共子序列,然后用总的长度减去它就行了.原因是是因为只要是在公共子序列里面,那么他就是对称的 ...
- unable to convert MySQL date/time value to System.DateTime
今天 用C# MySql做项目的时候 遇到了 unable to convert MySQL date/time value to System.DateTime 这样的异常错误,这个原因是因为:表里 ...
- C# 在右下角弹出窗口
窗口代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- SQL 去除小数点后无效 0 的方法
select convert(float,10.0000) 就是这么简单
- 让 IE 支持HTML5 placeholder
HTML5 新增的placeholder属性已经得到现代浏览器的支持,旨在提供简单的API可以为文本输入框设置 描述输入字段预期值的提示信息(hint). 这是W3C在标准化的过程中对用户体验的更多考 ...
- 全世界最详细的一步一步搭建RAC步骤(一)---安装操作系统RHEL4.6【weber出品】
全文搭建RAC分为3步骤 <--安装操作系统RHEL4.6> <--配置ASM+裸设备> <--安装集群软件> <--安装数据库软件> ...
- 关于.net 对excel操作的方法
asp.net打印文件用的最多的一般2种,word和excel,今天在这里整洁一下关于打印excel的几种方式及优缺点 第一种:直接打印html代码,然后将输出类型伪装成excel文件(excel是可 ...