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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- web@css样式进阶--图形字体、动画、显隐....
1.图形字体<i class="fa fa-heart"></i> 操作类名,需<link rel="stylesheet" hr ...
- GIT入门文档
集中式(SVN): 集中式版本控制系统,版本库是集中存放在中央服务器的,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器. 集中式版本控制系 ...
- 【原创】大数据基础之Hive(5)hive on spark
hive 2.3.4 on spark 2.4.0 Hive on Spark provides Hive with the ability to utilize Apache Spark as it ...
- 自定义session,cookie
第一种情况:没有设置缓存:执行相对应的setitem等方法进行,保存到字典里面 cookies_dic={}print(cookies_dic)class Session(): def __init_ ...
- Stetho简化Android调试(二)
Stetho简化Android调试(一) 一文中讲述了如何使用Stetho结合Chrome远程调试Android App. Stetho给我们调试带来很大的便利,效率显著提升的同时也产生一个问题:如果 ...
- Linux平台 Oracle 18c RAC安装
Linux平台 Oracle 18c RAC安装Part1:准备工作 2018-08-04 22:20 by AlfredZhao, 1065 阅读, 0 评论, 收藏, 编辑 一.实施前期准备工作 ...
- 神经网络之dropout层
一:引言 因为在机器学习的一些模型中,如果模型的参数太多,而训练样本又太少的话,这样训练出来的模型很容易产生过拟合现象.在训练bp网络时经常遇到的一个问题,过拟合指的是模型在训练数据上损失函数比较小, ...
- Confluence 6 为登录失败编辑,禁用和配置验证码
在默认的情况下,验证码将会在失败登录次数达到的时候显示. 如果为登录失败编辑,禁用和配置验证码: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧 ...
- ionic3 国际化
http://www.cnblogs.com/huangenai/p/6868173.html 按上面这个网站的步骤整一遍, 但是ionic3 会报错 所以 在 import { NgModule ...
- PHP中json_encode()使用须知,JSON数组和JSON对象
⊰ 偷偷的告诉你,这是一个很不谨慎就会踩得坑 ⊱ 如下代码 (看了一下很简单,没毛病啊,老铁) $arr = array( '0'=>'a','1'=>'b','2'=>'c',' ...