[ABC265C] Belt Conveyor
Problem Statement
We have a grid with $H$ horizontal rows and $W$ vertical columns. $(i, j)$ denotes the square at the $i$-th row from the top and $j$-th column from the left.
$(i,j)$ has a character $G_{i,j}$ written on it. $G_{i,j}$ is U, D, L, or R.
You are initially at $(1,1)$. You repeat the following operation until you cannot make a move.
Let $(i,j)$ be the square you are currently at.
If $G_{i,j}$ isUand $i \neq 1$, move to $(i-1,j)$.
If $G_{i,j}$ isDand $i \neq H$, move to $(i+1,j)$.
If $G_{i,j}$ isLand $j \neq 1$, move to $(i,j-1)$.
If $G_{i,j}$ isRand $j \neq W$, move to $(i,j+1)$.
Otherwise, you cannot make a move.
Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1 instead.
Constraints
- $1 \leq H, W \leq 500$
- $G_{i,j}$ is
U,D,L, orR. - $H$ and $W$ are integers.
Input
Input is given from Standard Input in the following format:
$H$ $W$
$G_{1,1}G_{1,2}\dots G_{1,W}$
$G_{2,1}G_{2,2}\dots G_{2,W}$
$\vdots$
$G_{H,1}G_{H,2}\dots G_{H,W}$
Output
If you end up at $(i, j)$, print it in the following format:
$i$ $j$
If you indefinitely repeat moving, print -1.
Sample Input 1
2 3
RDU
LRU
Sample Output 1
1 3
You will move as $(1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3)$, ending up here, so the answer is $(1, 3)$.
Sample Input 2
2 3
RRD
ULL
Sample Output 2
-1
You will indefinitely repeat moving as $(1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots$, so -1 should be printed in this case.
Sample Input 3
9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
从点 $(1,1)$ 开始走,走到不能走为止。如果走到一个到过的点,说明有环,可以不停走,判断即可。
#include<cstdio>
const int tx[]={-1,1,0,0},ty[]={0,0,-1,1},N=505;
int n,m,v[N][N],t[N][N],x,y,dx,dy;
char ch;
int can(int x,int y)
{
return x>0&&x<=n&&y>0&&y<=m;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf(" %c",&ch);
if(ch=='D')
t[i][j]=1;
else if(ch=='L')
t[i][j]=2;
else if(ch=='R')
t[i][j]=3;
}
}
x=y=v[1][1]=1;
while(1)
{
dx=x+tx[t[x][y]],dy=y+ty[t[x][y]];
if(!can(dx,dy))
{
printf("%d %d\n",x,y);
return 0;
}
x=dx,y=dy;
if(v[x][y])
{
printf("-1");
return 0;
}
v[x][y]=1;
}
}
[ABC265C] Belt Conveyor的更多相关文章
- 搜索的应用--计算最优解:Aizu - ALDS1_4_D Allocation
搜索的应用-计算最优解 题目: You are given nn packages of wiwi kg from a belt conveyor in order (i=0,1,...n−1i=0, ...
- 三分套三分 --- HDU 3400 Line belt
Line belt Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3400 Mean: 给出两条平行的线段AB, CD,然后一 ...
- 搜索(三分):HDU 3400 Line belt
Line belt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Your data vis “Spidey-sense” & the need for a robust “utility belt”
@theboysmithy did a great piece on coming up with an alternate view for a timeline for an FT piece. ...
- Line belt
Problem Description In a two-dimensional plane there are two line belts, there are two segments AB a ...
- UVA1265 Tour Belt Kruskal重构树、倍增、树上差分
题目传送门 题意:定义$Tour \, Belt$为某张图上的一个满足以下条件的点集:①点集中至少有$2$个点②任意两点互相连通③图上两个端点都在这个点集中的边的权值的最小值严格大于图上只有一个端点在 ...
- Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp
D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...
- HDU 3400 Line belt (三分嵌套)
题目链接 Line belt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 3400 Line belt (三分再三分)
HDU 3400 Line belt (三分再三分) ACM 题目地址: pid=3400" target="_blank" style="color:rgb ...
- HDU 3400 Line belt【三分套三分】
从A出发到D,必定有从AB某个点E出发,从某个点F进入CD 故有E,F两个不确定的值. 在AB上行走的时间 f = AE / p 在其他区域行走的时间 g = EF / r 在CD上行走的时间 ...
随机推荐
- NOIP 2022 VP游记
总结:挂大分. HA NOIP没初中生的份,VP. CSP-S 图论专场 NOIP 数数专场. CCF 我服你. T1 看完之后,感觉不难,瞎搞了 40min+,过了大样例. 对拍不会写. T2 猜不 ...
- CTFshow misc1-10
小提示:需要从图片上提取flag文字,可以通过截图翻译或者微信发送图片,这两个的ai图像识别挺好用的. misc1: 解压打开就能看见flag,提取出来就行 misc2: 记事本打开,看见 ng字符, ...
- Go 上下文的理解与使用
为什么需要 context 在 Go 程序中,特别是并发情况下,由于超时.取消等而引发的异常操作,往往需要及时的释放相应资源,正确的关闭 goroutine.防止协程不退出而导致内存泄露.如果没有 c ...
- 千万级数据的表,我把慢sql优化后性能提升30倍!
分享技术,用心生活 背景:系统中有一个统计页面加载特别慢,前端设置的40s超时时间都加载不出来数据,因为是个统计页面,基本上一猜就知道是mysql的语句有问题,遗留了很久没有解决,正好趁不忙的时候,下 ...
- 为何 Linus 一个人就能写出这么强的系统,中国却做不出来?
前言 知乎上有一个提问:为何 Linus 一个人就能写出这么强的系统,中国却做不出来? ↓↓↓ 今天,我们就这个话题,一起来做个讨论. 不知道大家是怎么看这个问题的?是美国人更聪明吗,所以才能写出这么 ...
- ipmitool配置机器的BMC
一.设置IP地址 1.确定操作对象 #ipmitool mc info 输出中"Device Revision"是命令的操作对象 2.设置BMC IP # ipmitool -I ...
- python判断ip所属地区 python 判断ip 网段
IP地址是互联网中唯一标识一个设备的地址,有时候需要判断一个IP地址所属的地区,这就需要用到IP地址归属查询.本文将介绍Python如何通过IP地址查询所属地区并展示代码. 一. IP地址归属查询 I ...
- MySQL运维1-日志
一.错误日志 错误日志是MySQL中最重要的日志之一,它记录了当MySQL启动和停止时,以及服务器在运行过程中发生的任何严重错误时的相关信息,当数据库出现任何故障导致无法正常使用时,建议首先查看此日志 ...
- ORACLE DBLink创建
在写测试脚本时,经常需要跨库取数据,SQL本身不支持跨库查找.Oracle提供DBLink链接,支持跨库操作. 1.创建DBLink Create public database link Next_ ...
- How to start with Gradle?
How to start with Gradle? Download the latest Gradle release from http://www.gradle.org/downloads Se ...