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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- ansible笔记(5):常用模块之文件操作(二)
ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...
- HNU 2015暑期新队员训练赛2 H Blanket
把每个 bi *x + ri ( 0 <= ri <= ai)标记, 输出被标记 0 – N 次的个数 #include<iostream> #include<cstdi ...
- 获得小程序码getWXACodeUnlimit
报错47001 data format error 出现这个错误必须是Body里面的raw才可以,而且access_token参数必须写在地址后面,不能写在raw里面,不然也出错. /** * 生命周 ...
- Spring与Quartz实现定期任务
<!-- 任务调度测试实现一 : 自定义的任务对象com.bocloud.equipment.test.ExampleJob 必须继承QuartzJobBean类,实现抽象方法executeIn ...
- hive学习01词频统计
词频统计 #创建表,只有一列,列名line create table word_count ( line string) row format delimited fields terminated ...
- iOS 去除百度地图下方的 logo
UIView *mView = _mapView.subviews.firstObject; for (id logoView in mView.subviews) { if ([logoView ...
- 五.ssh远程管理服务
01. 远程管理服务知识介绍 1) SSH远程登录服务介绍说明 SSH是Secure Shell Protocol的简写,由 IETF 网络工作小组(Network Working Group)制定: ...
- requests 的基本用法
r = requests.get('www.baidu.com') r.request.headers{'User-Agent': 'python-requests/2.18.4', 'Accept- ...
- Unity3D用户手册
Unity Manual 用户手册 Welcome to Unity. 欢迎使用Unity. Unity is made to empower users to create the best int ...
- 项目中使用sass预处理器
安装sass npm install node-sass sass-loader --save 新建样式文件后缀为 .scss 在使用样式的页面引入:import 'xx.scss';