Neatnes

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的更多相关文章

  1. codeforces 359E Neatness(DFS+构造)

    Simon loves neatness. So before he goes to bed, Simon wants to complete all chores in the house. Sim ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. mysql的group by查询

    下面是多种写法,针对使用group by后得到最新记录的测试及结果: 说明:我在测试的时候,因为我的表数据在增加,得到最新的数据可能不同 -- 1.得到每个分组中id最小的那条记录 select * ...

  2. c++内存对齐原理

    转载自http://blog.csdn.net/it_yuan/article/details/24651347 #类中的元素 0. 成员变量   1. 成员函数   2. 静态成员变量   3. 静 ...

  3. js浏览器判断函数

    function userBrowser(){ var browserName=navigator.userAgent.toLowerCase(); if(/msie/i.test(browserNa ...

  4. C# 防止content-type修改后上传恶意文件

    以图片为例子.在上传图片的时候,使用Fiddler抓取 通过js判断文件类型是不安全的,所以通过后台来判断,代码如下: ) { HttpPostedFile file0 = Request.Files ...

  5. Java红黑树详谈

    定义 红黑树的主要是想对2-3查找树进行编码,尤其是对2-3查找树中的3-nodes节点添加额外的信息.红黑树中将节点之间的链接分为两种不同类型,红色链接,他用来链接两个2-nodes节点来表示一个3 ...

  6. 分析Vue框架源码心得

    1.在封装一个公用组件,比如button按钮,我们多个地方使用,不同类型的button调用不同的方法,我们就可以这样用 代码片段: <lin-button v-for="(item,i ...

  7. iOS 上线过程中出现的问题总结

    1:  因为使用后台播放音乐,在 info 里面添加 required background modes  的 key, 如果你的代码里面没有后套播放音频的功能, 不要添加 required back ...

  8. TeamLab安装及使用

    http://blog.csdn.net/crazin/article/details/9529061 最近想装个项目管理管理系统方便项目管理,调研了下这方面开源的软件还是比较多的,国内的有禅道,试用 ...

  9. linux之ab压力测试工具

    等待... https://www.cnblogs.com/myvic/p/7703973.html

  10. Django框架第一篇基础

    一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...