codeforces 359E Neatness(DFS+构造)
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:
- 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.
- 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.
- 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.
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.
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+构造)的更多相关文章
- Codeforces 359E Neatness
Neatnes dfs一下用set维护能不能走, 进入的时候点亮灯, 回溯的时候灭灯. #include<bits/stdc++.h> #define LL long long #defi ...
- codeforces 681D Gifts by the List dfs+构造
题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...
- 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 如果只是单纯模拟肯定会超时 ...
- codeforces 1282 E. The Cake Is a Lie (dfs+构造)
链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...
- codeforces 734E(DFS,树的直径(最长路))
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- codeforces 723D(DFS)
题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...
- Uva 12009 平方数尾数与自身同样 dfs 构造
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/25166611 题目链接:点击打开链接 题意 ...
- Military Problem CodeForces 1006E (dfs序)
J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...
随机推荐
- Paths with -a does not make sense.
最近开始使用为windows的系统,进行git操作的时候出现了一个小问题. 使用命令: E:\IdeaProjects\mmall>git commit -am 'first commit in ...
- [MYSQL][2]索引
创建表时创建索引: 在已经存在的表上创建索引: 方法一 方法二 删除索引:
- 使用泛型与不使用泛型的Map的遍历
https://www.cnblogs.com/fqfanqi/p/6187085.html
- MySQL---正确使用索引、limit分页、执行计划、慢日志查询
正确使用索引 数据库表中添加索引后确实会让查询速度起飞,但前提必须是正确的使用索引来查询,如果以错误的方式使用,则即使建立索引也会不奏效.即使建立索引,索引也不会生效: - like '%xx' se ...
- php.ini修改后,重启无效
今天做项目,上传图,需要修改php.ini.发现修改后,多次长期服务器也没用,在网上找了好多方案.介绍一下我的流程 1.使用phpinfo()找到php.ini的位置,如果位置不准确,修改肯定没有任何 ...
- ruby做接口测试
一. 工具选择 IDE:rubymine:http接口请求:Unirest,ruby单元测试框架:rspec 二.工程创建 新建工程,在工程目录下,执行:rspec --init:初始化rspec工程 ...
- 最新Altium_Designer_Beta_18.7.is AD18安装教程及破解说明
下解Altium_Designer带破解的压缩包. 下载链接:https://pan.baidu.com/s/1TlPHtSthJKxLcXWcCR-q-g 密码:bt0g 解压缩Altium_Des ...
- Lambda表达式的语法与如何使用Lambda表达式
Lambda表达式是对象,是一个函数式接口的实例 如何来写Lambda表达式? 看参数 看返回值 代码实例1: package day2; import jdk.nashorn.internal.co ...
- SAP Odata実行命令(1)
$count $Orderby:desc/asc ※$Orderby=ソートする項目 desc降順/asc昇順 を指定すること $Filter: $Skip,Top and Inline count: ...
- 单节锂电池充电(电路)芯片TP4056