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. Vue3 中 keepAlive 如何搭配 VueRouter 来更自由的控制页面的状态缓存?

    在 vue 中,默认情况下,一个组件实例在被替换掉后会被销毁.这会导致它丢失其中所有已变化的状态--当这个组件再一次被显示时,会创建一个只带有初始状态的新实例.但是 vue 提供了 keep-aliv ...

  2. 通过WinSW部署JAR包为windows服务

    通过WinSW部署JAR包为windows服务 背景 使用 Java 编写了一些有用的工具,因为不方便部署到服务器上,所以需要把 Java 生成的 jar 包在本地 Windows 上部署. 查阅了几 ...

  3. java与es8实战之四:SpringBoot应用中操作es8(无安全检查)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<java与es8实战>系 ...

  4. redis分布式锁,setnx+lua脚本的java实现

    1 前言 在现在工作中,为保障服务的高可用,应对单点故障.负载量过大等单机部署带来的问题,生产环境常用多机部署.为解决多机房部署导致的数据不一致问题,我们常会选择用分布式锁. 目前其他比较常见的实现方 ...

  5. 深入探究API接口

    作为程序员,我们经常会遇到需要获取外部数据或调用外部服务的情况.而API(Application Programming Interface,应用程序编程接口)接口就是这样的一种机制,它允许我们的应用 ...

  6. 使用antd-mobile遇到的坑

    在使用antd-mobile中的组件的时候,遇到了一些比较棘手的问题,经过查找相关资料和网上的帮助,在此整理出了问题以及解决办法: 在引入antd-mobile的时候,根据官网提供的文档: 1,首先安 ...

  7. Java爬虫实战系列2——动手写爬虫初体验

    在上面的章节中,我们介绍了几个目前比较活跃的Java爬虫框架.在今天的章节中,我们会参考开源爬虫框架,开发我们自己的Java爬虫软件. 首先,我们下载本章节要使用到的源代码,本章节主要提供了基于HTT ...

  8. [Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)

    1 故事背景 最近项目上有个业务需求,翻译成技术需求,即:将request.headers中的几个header入参转换成request.body(pageRequest)中的内置参数. 为便于灵活配置 ...

  9. 关于.Net 6.0 在Linux ,Docker容器中,不安装任何依赖就生成图形验证码!!!!!!!!!!!

    在.Net Framework时代,我们生成验证码大多都是用System.Drawing. 在.Net 6中使用也是没有问题的. 但是,System.Drawing却依赖于Windows GDI+. ...

  10. CUDA C编程权威指南:2.2-给核函数计时

      本文主要通过例子介绍了如何给核函数计时的思路和实现.实现例子代码参考文献[7],只需要把相应章节对应的CMakeLists.txt文件拷贝到CMake项目根目录下面即可运行. 1.用CPU计时器计 ...