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个儿子,如果一个 ...
随机推荐
- iOS 自定义任意形状加载进度条(水波纹进度条)
1. 项目中要做类似下面的加载动画: 先给出安卓的实现方式 2.iOS的实现方式参考了下面两位的,感谢. 以任意底部图片为背景的加载动画 和 水波纹动画 最后附上自己的demo
- Ubuntu16 安装Anaconda3+tensorflow cpu版
打开火狐浏览器,下载anaconda安装包,网址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=D 下载完成,到Do ...
- 解决微信小程序用 SpringMVC 处理http post时请求报415错误
解决微信小程序用 SpringMVC 处理http post时请求返回415错误 写微信小程序时遇到的问题,这个坑硬是让我整了半天 wx.request请求跟ajax类似处理方法一致 小程序端请求代码 ...
- 为什么后台返回的日期我输出处理了在苹果手机里显示NAN?
现象: //结束时间var ent_time ="2018-04-28 09:36:00"alert((Date.parse(new Date(ent_time))));/ ...
- 最完整的数据倾斜解决方案(spark)
一.了解数据倾斜 数据倾斜的原理: 在执行shuffle操作的时候,按照key,来进行values的数据的输出,拉取和聚合.同一个key的values,一定是分配到一个Reduce task进行处理. ...
- 利用谷歌浏览器断点调试js反向解析,解密
目标网站:https://www.aqistudy.cn/html/city_detail.html 点击按钮才会去后台请求数据, 第一步:将click打开, 第二步:找个后台请求数据的url h ...
- linux shell 字符串常用操作
1.shell内置的字符串操作 表达式 含义 ${#string} $string的长度 ${string:position} string中,从位置$position开始提取字符串 ${string ...
- 如何在golang中打印grpc详细日志
最近捣鼓fabric,在一个tls证书问题上纠结挺久,连接orderer服务时候,grpc日志总是冷冰冰的显示这个信息 Orderer Client Status Code: (2) CONNECTI ...
- uCOS-II中的任务切换-图解多种任务调度时机与问题
[@.1 任务调度时机] 之前的一篇文章分析了具体的uCOS-II中的任务切换机制,是从函数调用的角度上分析的.这次我具体从整个程序运行的时间上来看,分析多种任务调度发生的时机.以下所有图片均可点击放 ...
- 武汉Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...