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个儿子,如果一个 ...
随机推荐
- 【模板】缩点(tarjan,DAG上DP)
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...
- C++笔记011:C++对C的扩展——变量检测增强
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 在C语言中重复定义多个同名的变量是合法的,多个同名的全局变量最终会被链接到全局数据区的同一个地址空间上. 在C++中,不允许定义多个同名的 ...
- uva_11806_Cheerleaders
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...
- 03JavaScript 输出
JavaScript 输出 JavaScript 没有任何打印或者输出的函数. 先来一点DOM的小知识点: DOM 解释: 您会经常看到 document.getElementById("i ...
- 「PHP」抽象工厂模式
引言 所属:创建型模式,常用设计模式之一 参考资料: <大话设计模式>程杰 模式概述 官方定义:抽象工厂模式(Abstract Factory),提供一个创建一系列相关或互相 ...
- PHP中计算字符串相似度的函数代码
similar_text — 计算两个字符串的相似度 int similar_text ( string $first , string $second [, float &$percent ...
- 第7章 YARN HA配置
目录 7.1 yarn-site.xm文件配置 7.2 测试YARN自动故障转移 ResourceManager (RM)负责跟踪集群中的资源,以及调度应用程序(例如,MapReduce作业).在Ha ...
- 论文翻译第二弹--用python(或Markdown)对论文复制文本进行处理
图中这种论文你想进行文本复制放入翻译软件进行翻译时,会发现是这种形式: 句子之间是断开的,这时普遍的方法,也是我之前一直用的方法就是打开一个文档编辑器,复制上去后一行行地继续调整.昨天不想这样了,就打 ...
- VS中添加lib与dll
参考与拓展阅读:https://blog.csdn.net/u012043391/article/details/54972127 lib: 1.附加包含目录---添加工程的头文件目录: ...
- vim 粘贴文本,格式混乱 tab
粘贴的代码如上.修改方法: 方法一: set paste 贴完后,设置 set nopaste 恢复代码缩进. 方法二:修改配置文件 vim /etc/vim/vimrc set pastetoggl ...