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. Python中字符串截取

    # 截取字符串时,如果位数不够,Python不会报错,而是返回空字符串 # 这是因为Python中的字符串是不可变的,所以当我们尝试访问一个不存在的索引时,Python会返回空字符串而不是报错 # 示 ...

  2. API接口的重要性

    API接口的重要性在现代软件开发中无可替代.以下是API接口的几个重要方面: 1. 实现系统集成:API接口允许不同应用程序之间实现数据共享和交流.通过API接口,不同的软件系统可以相互连接和协作,实 ...

  3. 接口未配置在app.json文件中

    微信小程序发布 提示 接口未配置在app.json文件中 狗血 昨天更新 就在app.json中添加  解决问题 "requiredPrivateInfos":[ "ge ...

  4. 提高 MySQL查询效率的方法

    当涉及到提高MySQL查询效率时,以下是一些重要的策略和技巧,可以帮助你优化数据库性能.无论你是一个Web开发者.数据工程师还是系统管理员,这些方法都可以帮助你确保你的MySQL数据库运行得更快.更有 ...

  5. xv6 进程切换中的锁:MIT6.s081/6.828 lectrue12:Coordination 以及 Lab6 Thread 心得

    引言 这节课和上一节xv6进程切换是一个完整的的进程切换专题,上一节主要讨论进程切换过程中的细节,而这一节主要讨论进程切换过程中锁的使用,所以本节的两大关键词就是"Coordination& ...

  6. ISO/OSI七层模型的分层与作用

    ISO/OSI的七层模型 第七层:应用层 为用户提供服务,给用户一个操作界面,如window的图形界面,Linux的命令行: 第六层:表示层 数据提供表示:把01二进制转换为图像数字等用户可以看懂的内 ...

  7. 文心一言 VS 讯飞星火 VS chatgpt (98)-- 算法导论9.3 4题

    四.用go语言,对一个包含n个元素的集合,假设一个算法只使用比较来确定第i小的元素,证明:无需额外的比较操作,它也能找到第 i-1 小的元素和第 n-i大的元素. 文心一言: 在这个问题中,我们要使用 ...

  8. 想转行DevOps工程师?快来看看DevOps工程师的学习路径,少走弯路

    DevOps方法论 :::tips DevOps方法论的主要来源是Agile, Lean 和TOC, 独创的方法论是持续交付. ::: DevOps 是一种软件开发方法,涉及持续开发,持续测试,持续集 ...

  9. 前端三件套系例之JS——JavaScript基础、JavaScript基本数据类型、JavaScript函数

    文章目录 1 JavaScript基础 1.JavaScript是什么 2.JavaScript介绍 2-1 ECMAScript和JavaScript的关系 2-2 ECMAScript的历史 3. ...

  10. code2uml使用教程

    通常的开发顺序是 先设计uml然后进行code编程. 但是很多时候我们是先看到code源码,却不知道uml关系如何. 特别是写论文的时候也是很需要的. 这个code2uml,就是辛苦搜寻到的结果,可以 ...