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}$ is U and $i \neq 1$, move to $(i-1,j)$.

If $G_{i,j}$ is D and $i \neq H$, move to $(i+1,j)$.

If $G_{i,j}$ is L and $j \neq 1$, move to $(i,j-1)$.

If $G_{i,j}$ is R and $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, or R.
  • $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的更多相关文章

  1. 搜索的应用--计算最优解: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, ...

  2. 三分套三分 --- HDU 3400 Line belt

    Line belt Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3400 Mean: 给出两条平行的线段AB, CD,然后一 ...

  3. 搜索(三分):HDU 3400 Line belt

    Line belt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. 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. ...

  5. Line belt

    Problem Description In a two-dimensional plane there are two line belts, there are two segments AB a ...

  6. UVA1265 Tour Belt Kruskal重构树、倍增、树上差分

    题目传送门 题意:定义$Tour \, Belt$为某张图上的一个满足以下条件的点集:①点集中至少有$2$个点②任意两点互相连通③图上两个端点都在这个点集中的边的权值的最小值严格大于图上只有一个端点在 ...

  7. Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp

    D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...

  8. HDU 3400 Line belt (三分嵌套)

    题目链接 Line belt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 3400 Line belt (三分再三分)

    HDU 3400 Line belt (三分再三分) ACM 题目地址:  pid=3400" target="_blank" style="color:rgb ...

  10. HDU 3400 Line belt【三分套三分】

    从A出发到D,必定有从AB某个点E出发,从某个点F进入CD 故有E,F两个不确定的值. 在AB上行走的时间   f = AE / p 在其他区域行走的时间 g = EF / r 在CD上行走的时间   ...

随机推荐

  1. Java NIO 图解 Netty 服务端启动的过程

    一.启动概述 了解整体Netty常用的核心组件后,并且对比了传统IO模式.在对比过程中,找到了传统IO对应Netty中是如何实现的.最后我们了解到在netty中常用的那些组件. 本文在了解下这些核心组 ...

  2. js调起android安卓与ios苹果方法 vue3.0 + ts

    let shareSelect = (ev :any) => { const u :any= navigator.userAgent; const win :any = window const ...

  3. 正则表达式快速入门三: python re module + regex 匹配示例

    使用 Python 实现不同的正则匹配(从literal character到 其他常见用例) reference python regular expression tutorial 目录 impo ...

  4. redisson分布式锁的应用——秒杀、超卖 简单例子(分布式锁相关)

    1.常见的分布式事务锁 1.数据库级别的锁 乐观锁,给予加入版本号实现 悲观锁,基于数据库的for update实现 2.Redis,基于SETNX.EXPIRE实现 3.Zookeeper,基于In ...

  5. Mysql中文字符串提取datetime

    DATE_FORMAT无法用于提取含中文字符的时间字符串中的时间, 可以通过STR_TO_DATE来提取其中的信息, 如下: SELECT STR_TO_DATE("2018年11月05日 ...

  6. 「coci 2021-2022 #1」Logičari

    link. 断环后把断的边所连的两个点特殊标记,作为两个特殊点.这样就是一个树,树的做法很简单吧,把两个特殊点特殊处理带进状态即可. 具体一点就是,设 \(f(x,c_x,c_f,c_{rt_1},c ...

  7. 小札 Combinatorics 2

    对于 Newton Expansion,式子本身的证明其实无甚可翻新的花样,但是题还是很有意思的.比如 codeforces - 1332E Height All the Same 这个. 首先给出几 ...

  8. Ds100p -「数据结构百题」31~40

    31.P2163 [SHOI2007]园丁的烦恼] 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草. 有一天国王漫步在花园里 ...

  9. k8s 自动扩缩容HPA原理及adapter配置详解👑

    大家好,我是蓝胖子,都知道,k8s拥有自动扩缩容机制HPA,我们能够通过配置针对不同的扩缩容场景进行自动扩缩容,往往初学者在面对其中繁多配置的时候会学了又忘记,今天我将会以一种不同的视角,结合api ...

  10. oracle多账套(用户)引用同一个账套的表或视图数据

    1.赋权限访问nbjf账套权限给到其他账套用户. grant select on nbjf.receivables to gzjf,hfjf,hfjy; 2.分别登陆 gzjf,hfjf,hfjy账套 ...