[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上行走的时间 ...
随机推荐
- 每日一库:fsnotify简介
fsnotify是一个用Go编写的文件系统通知库.它提供了一种观察文件系统变化的机制,例如文件的创建.修改.删除.重命名和权限修改.它使用特定平台的事件通知API,例如Linux上的inotify,m ...
- 《最新出炉》系列入门篇-Python+Playwright自动化测试-15-playwright处理浏览器多窗口切换
1.简介 浏览器多窗口的切换问题相比大家不会陌生吧,之前宏哥在java+selenium系列文章中就有介绍过.大致步骤就是:使用selenium进行浏览器的多个窗口切换测试,如果我们打开了多个网页,进 ...
- 前瞻|Java 21 新特性 String Templates(字符串模版)
在日常写Java的时候,对于字符串的操作是非常普遍的,其中最常见的就是对字符串的组织.也因为这个操作非常普遍,所以诞生了很多方案,总下来大概有这么几种: 使用+拼接 使用StringBuffer和Sp ...
- springboot打包与依赖包分离
前言: springboot项目部署时,需要本地打包成一个jar放到服务器进行部署(使用jenkins自动打包部署同理),部署包里包含了其它所有依赖包,整个包会比较大,小则几M,大则几十上百. 正文: ...
- Jmeter获取Websocket多帧消息的实现方法
由于需要对WebSocket进行压力测试,因此又回归到了JMeter的使用.网络上缺少具体的获取多帧消息的操作,且自己也踩了两个坑,总结一下可行的操作供大家参考. 一.情况说明 ...
- 谷粒商城微服务分布式高级篇:linux下使用docker安装ElasticSearch
[root@localhost ~]# docker pull elasticsearch:7.8.0 安装elasticsearch:7.8.0[root@localhost ~]# docker ...
- 5.0 CRC32校验技术概述
CRC校验技术是用于检测数据传输或存储过程中是否出现了错误的一种方法,校验算法可以通过计算应用与数据的循环冗余校验(CRC)检验值来检测任何数据损坏.通过运用本校验技术我们可以实现对特定内存区域以及磁 ...
- Java并发Map的面试指南:线程安全数据结构的奥秘
简介 在计算机软件开发的世界里,多线程编程是一个重要且令人兴奋的领域.然而,与其引人入胜的潜力相伴而来的是复杂性和挑战,其中之一就是处理共享数据.当多个线程同时访问和修改共享数据时,很容易出现各种问题 ...
- ArcGIS地图投影与坐标系转换的方法
本文介绍在ArcMap软件中,对矢量图层或栅格图层进行投影(即将地理坐标系转为投影坐标系)的原理与操作方法. 首先,地理坐标系与投影坐标系最简单的区别就是,地理坐标系用经度.纬度作为空间衡量指 ...
- EQ 均衡器
EQ 的全称是 Equalizer,EQ 是 Equalizer 的前两个字母,中文名字叫做"均衡器".最早是用来提升电话信号在长距离的传输中损失的高频,由此得到一个各频带相对平衡 ...