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. 使用percona-xtrabackup工具对mysql数据库的备份方案

    使用percona-xtrabackup工具对mysql数据库的备份方案 需要备份mysql的主机 172.16.155.23存放备份mysql的主机 172.16.155.22 目的:将155.23 ...

  2. (一)七种AOP实现方法

    在这里列表了我想到的在你的应用程序中加入AOP支持的所有方法.这里最主要的焦点是拦截,因为一旦有了拦截其它的事情都是细节. Approach 方法 Advantages 优点 Disadvantage ...

  3. linux-history显示历史命令执行时间

    vim  ~/.bashrc 或者 ~/.bash_profile 增加:export HISTTIMEFORMAT="%F %T  " 一次会话的话先执行 然后使用history ...

  4. 修改html中button显示的文字

    1. <input type="button">  实现密码输入框的可见和隐藏 直接修改value属性即可 <script type="text/jav ...

  5. Modbus库开发笔记之十一:关于Modbus协议栈开发的说明

    对于Modbus协议栈的整个开发内容,前面已经说得很清楚了,接下来我们说明一下与开发没有直接关系的内容. 首先,关于我为什么开发这个协议栈的问题.我们的初衷只是想能够在开发产品时不用每次都重写这一部分 ...

  6. 处理:“ORA-28002: the password will expire within 7 days”的问题

    一:问题描述: 二:处理步骤 [oracle@localhost 2018_07_14]$ rlwrap sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3 ...

  7. axis 数据流

    产生数据流的代码 模板   重新修改了下 :]axis_data_cnt='d0; :]axis_data_frame_cnt='d0; :]delay_cnt='d0; initial begin ...

  8. D3.js 添加zoom缩放功能后dblclick双击也会放大的问题

    svg.call(zoom).on("dblclick.zoom", null); https://stackoverflow.com/questions/25007466/d3- ...

  9. es2015(es6)基础知识整理(更新中...)

    1.let let可以声明块级作用域变量 'use strict'; if (true) { let app = 'apple'; } console.log(app); //外面是访问不到app的 ...

  10. dbcp连接池出现的问题java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z

    解决方案:mysql-connector 版本为 5.0.4 ,那么对应的 dbcp 和 pool 版本应该为 1.4 和 1.6 .    5.0.4 不应该使用 2.0 及以上版本的 dbcp 和 ...