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

Simon's house looks like a rectangular table consisting of n rows and n columns from above. All rows of the table are numbered from 1to n from top to bottom. All columns of the table are numbered from 1 to n from left to right. Each cell of the table is a room. Pair (x, y)denotes the room, located at the intersection of the x-th row and the y-th column. For each room we know if the light is on or not there.

Initially Simon is in room (x0, y0). He wants to turn off the lights in all the rooms in the house, and then return to room (x0, y0). Suppose that at the current moment Simon is in the room (x, y). To reach the desired result, he can perform the following steps:

  1. The format of the action is "1". The action is to turn on the light in room (x, y). Simon cannot do it if the room already has light on.
  2. The format of the action is "2". The action is to turn off the light in room (x, y). Simon cannot do it if the room already has light off.
  3. The format of the action is "dir" (dir is a character). The action is to move to a side-adjacent room in direction dir. The direction can be left, right, up or down (the corresponding dir is L, R, U or D). Additionally, Simon can move only if he see a light in the direction dir. More formally, if we represent the room, Simon wants to go, as (nx, ny), there shold be an integer k (k > 0), that room(x + (nx - x)k, y + (ny - y)k) has a light. Of course, Simon cannot move out of his house.

Help Simon, find the sequence of actions that lets him achieve the desired result.

Input

The first line contains three positive integers n, x0, y0 (2 ≤ n ≤ 500, 1 ≤ x0, y0 ≤ n).

Next n lines contain the description of rooms in the house. The i-th line contains n space-separated integers ai1, ai2, ..., ain. If numberaij equals zero, then room (i, j) has light off, and if number aij equals one, then room (i, j) has light on. It is guaranteed that at least one room has light on.

Output

If there is no desired sequence of actions, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes) and the description of the required sequence of actions as a string. Note that you do not have to minimize the length of the sequence of actions but you shouldn't use more than 3·106 actions.

题目大意:给一个n*n的矩阵,每个矩阵有一个0或1,一个人一开始在点(x0, y0)。在某个点(x, y)的时候,可以向上下左右4个方向移动,可以移动当且仅当要移动的方向上至少有一个1。然后有3种操作,把0变成1、把1变成0、移动。问是否存在一种方案,使得从(x0, y0)出发,可以把所有的1变成0,然后回来(x0, y0),并输出这个方案。要求方案的操作次数不超过300W。

思路:暴力DFS,在某个点的时候,如果那个点是0,那么把它变成1,然后看一下4个方案能不能移动,若能移动,就走,等回溯到这个点的时候,把这个1变回0,然后回溯。若一次DFS之后,矩阵没有1,说明存在方案,然后输出这个方案(DFS的时候用数组存下来)。

我不会证明为什么是对的,反正是找不到反例。

至于方案长度的问题,每个点最多只会走一次,从代码可以看出每个点最多10次操作,250000个点的10倍,不到300W。

因为不是求最优解,会不会有很多多余的操作也就跟我没什么关系了……

代码(171MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXL = ; int mat[MAXN][MAXN];
bool vis[MAXN][MAXN];
char ans[MAXL];
int cnt, n, x0, y0; void dfs(int x, int y) {
vis[x][y] = true;
if(!mat[x][y]) {
mat[x][y] = ;
ans[cnt++] = '';
}
for(int i = x - ; i > ; --i) {
if(mat[i][y] == ) {
if(!vis[x - ][y]) {
ans[cnt++] = 'U';
dfs(x - , y);
ans[cnt++] = 'D';
}
break;
}
}
for(int i = x + ; i <= n; ++i) {
if(mat[i][y] == ) {
if(!vis[x + ][y]) {
ans[cnt++] = 'D';
dfs(x + , y);
ans[cnt++] = 'U';
}
break;
}
}
for(int i = y - ; i > ; --i) {
if(mat[x][i] == ) {
if(!vis[x][y - ]) {
ans[cnt++] = 'L';
dfs(x, y - );
ans[cnt++] = 'R';
}
break;
}
}
for(int i = y + ; i <= n; ++i) {
if(mat[x][i] == ) {
if(!vis[x][y + ]) {
ans[cnt++] = 'R';
dfs(x, y + );
ans[cnt++] = 'L';
}
break;
}
}
mat[x][y] = ;
ans[cnt++] = '';
} bool check() {
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
if(mat[i][j] == ) return false;
return true;
} int main() {
scanf("%d%d%d", &n, &x0, &y0);
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j) scanf("%d", &mat[i][j]);
dfs(x0, y0);
ans[cnt++] = ;
if(check()) printf("YES\n%s\n", ans);
else puts("NO");
}

codeforces 359E Neatness(DFS+构造)的更多相关文章

  1. Codeforces 359E Neatness

    Neatnes dfs一下用set维护能不能走, 进入的时候点亮灯, 回溯的时候灭灯. #include<bits/stdc++.h> #define LL long long #defi ...

  2. codeforces 681D Gifts by the List dfs+构造

    题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...

  3. Codeforces 946 课程表背包DP 数位DFS构造

    A B 给你A,B 两个数      1.a=0 OR b=0 break      2.a>=2b a=a-2b        3.b>=2a b=b-2a 如果只是单纯模拟肯定会超时 ...

  4. codeforces 1282 E. The Cake Is a Lie (dfs+构造)

    链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...

  5. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  6. codeforces 731C(DFS)

    题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...

  7. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  8. Uva 12009 平方数尾数与自身同样 dfs 构造

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/25166611 题目链接:点击打开链接 题意 ...

  9. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

随机推荐

  1. 几行代码实现iOS摇一摇功能

    实现这个功能很简单,我们直接看代码 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@&quo ...

  2. js检测是否可以访问公网服务器

    wifi认证开发过程所用到的,源码如下: 注:检测AC是否放行成功,是否可以访问公网阿里云服务器 功能调用: checkNet().then(function(res) { if(res) { //连 ...

  3. RN如何固定底部的按钮

    如上图的底部新增按钮,要是放在web里那是相当简单,直接是用固定定位就行,但是在RN里是没有固定定位可言的. 方案一: 采用绝对定位,相对于最外层的定位在底部位置.(在部分安卓机上有问题,动态计算的高 ...

  4. 20181029NOIP模拟赛T3

    3 .空间活动 [题目描述] 贝茜和佩奇正在玩一款游戏,在游戏开始会生成一个有n个点m条单向边的地图,经过每条边需要花费价格为Hi的费用(Hi<=1000).但是如果两个点可以互相到达,那么这两 ...

  5. Tornado异步与延迟任务

    最近一直在研究Tornado异步操作,然而一番研究后发现要使一个函数异步化的最好方法就是采用相关异步库,但目前很多功能强大的库都不在此列.经过一番查找文档和搜索示范,终于发现了ThreadPoolEx ...

  6. 关于“CheckBox”通过表单提交的问题

    大多数时候CheckBox取值传到java后台都是通过js取值,ajax传值,今天改一离职同事的老代码,那家伙通过表单提交一些列的CheckBox设置,没想到的是后台死活接收不正常,name.valu ...

  7. visual studio进程或线程自上一个步骤以来已更改

    1.自己的解决方案:visual studio在多进程执行,在配置页面(webconfig)里把UseCounterThread参数设置为0 2.公司其他人解决方案,自己试了,多进程执行的时候没起作用 ...

  8. Python学习 :常用模块(一)

    常用模块(一) 一.时间(time)模块 时间戳 (Timestamp):时间戳表示的是从1970年1月1日00:00:00为计时起点,到当前的时间长度 import time print(help( ...

  9. java instanceof 的理解

    简单来说,java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法 ...

  10. Noip 2011 Day 1 & Day 2

    Day 1   >>> T1   >> 水题一道 . 我们只需要 for 一遍 , 由于地毯是从下往上铺的 , 我们只需要记录该位置最上面的地毯的编号 , 每一次在当前地 ...