Codeforces 359E Neatness
dfs一下用set维护能不能走, 进入的时候点亮灯, 回溯的时候灭灯。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end() using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, sx, sy;
int on[N][N];
bool vis[N][N];
set<int> row[N], col[N];
string path; bool check(int x, int y, int vx, int vy) {
if(x + vx <= || x + vx > n) return false;
if(y + vy <= || y + vy > n) return false;
if(vis[x + vx][y + vy]) return false;
if(!vy) {
if(vx == ) return upper_bound(ALL(col[y]), x) != col[y].end();
else return lower_bound(ALL(col[y]), x) != col[y].begin();
} else {
if(vy == ) return upper_bound(ALL(row[x]), y) != row[x].end();
else return lower_bound(ALL(row[x]), y) != row[x].begin();
}
} void dfs(int x, int y) {
vis[x][y] = true;
if(!on[x][y]) {
path.push_back('');
on[x][y] = true;
row[x].insert(y);
col[y].insert(x);
}
if(check(x, y, , )) {
path.push_back('R');
dfs(x, y + );
path.push_back('L');
}
if(check(x, y, , -)) {
path.push_back('L');
dfs(x, y - );
path.push_back('R');
}
if(check(x, y, , )) {
path.push_back('D');
dfs(x + , y);
path.push_back('U');
}
if(check(x, y, -, )) {
path.push_back('U');
dfs(x - , y);
path.push_back('D');
}
if(on[x][y]) {
on[x][y] = false;
path.push_back('');
row[x].erase(y);
col[y].erase(x);
}
} int main() {
cin >> n >> sx >> sy;
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
cin >> on[i][j];
if(on[i][j]) {
row[i].insert(j);
col[j].insert(i);
}
}
}
dfs(sx, sy);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(on[i][j]) return puts("NO"), ;
cout << "YES" << "\n";
cout << path << "\n";
return ;
} /*
*/
Codeforces 359E Neatness的更多相关文章
- codeforces 359E Neatness(DFS+构造)
Simon loves neatness. So before he goes to bed, Simon wants to complete all chores in the house. Sim ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- h5 右下角浮动按钮, 纯css实现
2017年11月22日19:00:22 效果: 代码: /** 右下角跳转按钮 跳转到列表 */ #list_note_icon { position: fixed; ...
- python安装提示No module named setuptools,wget提示ERROR 403: SSL is required
在下载安装一个python工具时提示报错No module named setuptools [root@kermit supervisor-3.3.0]$ sudo python setup.py ...
- bootstrapValidator使用总结
1.根据条件动态增删检查条件 参考文章:https://blog.csdn.net/sxlzs_/article/details/78211928 #去除某字段的验证 $("#formRes ...
- 数字、ip转换python实现
# 数字 ==> ip # 数字范围[0, 255^4] >>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i ...
- Oracle 数据备份与恢复
前言 一:备份与恢复概述 二:RMAN 工具概述 1: RMAN 组件 1: RMAN命令执行器 [oracle@localhost ~]$ rman target system/nocatalog ...
- Confluence 6 CSS 编辑技巧
开始编辑空间样式表 一个空间的样式表是你开始对 CSS 进行自定义编辑的好的开始.在空间样式表中,包含了你所有可以进行修改的元素.当你对空间样式表进行编辑的时候,空间样式表的修改只会对你修改的空间有效 ...
- opencv 图像矫正
四个坐标系的转换:https://blog.csdn.net/humanking7/article/details/44756073 标定和矫正:https://blog.csdn.net/u0134 ...
- 浅谈java中bigInteger用法
1.赋值: BigInteger a=new BigInteger("1"); BigInteger b=BigInteger.valueOf(1); 2.运算: ① add(); ...
- flutter No material widget found textfield widgets require a material widget ancestor
Error states that TextField widgets require a Material widget ancestor. Simply wrapping your whole l ...
- cordova AndroidStudio3.0 升级报错问题
http://blog.csdn.net/z_Xiaozuo/article/details/78962701 ionic3 打包 安卓项目遇到的问题,当时比较冲忙升级了下android studio ...