ACM Robot Motion(dfs深度优先搜索)

NYOJ 第371题 机器人II 阅读详情

 

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are 

N north (up the page) 
S south (down the page) 
E east (to the right on the page) 
W west (to the left on the page) 

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid. 

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits. 

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around. 

 Input

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0. 

 Output

For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1. 

 Sample Input

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

     题意是一个机器人从给定的(1,y)点出发,按照地上写的EWSN分别按东西南北的方向走,若能走出地图,输出所需步数;若形成了环,则输出在进入环前走了几步和环有几步。因是多次重复该步骤,使用dfs。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;

int r,c,start,ans,maxn,pou;
char g[12][12];
int book[12][12];//这个数组用来记录每个点是在第几步走到的 

void dfs(int x,int y,char p,int step){
	int tx=x,ty=y;
	if(x==0||x==r+1||y==0||y==c+1){//走出了地图,搜索结束 
		ans=step;
		pou=1;
		return ;
	}
	switch(p){//四个方向 
		case 'N':tx=x-1;break;
		case 'S':tx=x+1;break;
		case 'W':ty=y-1;break;
		case 'E':ty=y+1;break;
	}
	if(book[tx][ty]==-1){//这个点还没走过 
		book[tx][ty]=step+1;
		maxn=max(maxn,step+1);//使用max函数记录下最后的步数 
		dfs(tx,ty,g[tx][ty],step+1);
		book[tx][ty]=-1;
	}
	else{//回到了某个走过的点(形成了环,搜索结束) 
		ans=book[tx][ty];
	}
	return ;
}

int main(int argc, char** argv) {
	while(scanf("%d%d%d",&r,&c,&start)&&(r||c)){
		memset(g,'\0',sizeof(g));
		memset(book,-1,sizeof(book));
		pou=0;
		ans=0;
		maxn=0;
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++)
				cin>>g[i][j];
		}
		book[1][start]=0;
		dfs(1,start,g[1][start],0);
		if(pou)
			cout<<ans<<" step(s) to exit"<<endl;
		else
			cout<<ans<<" step(s) before a loop of "<<maxn-ans+1<<" step(s)"<<endl;
	}
	return 0;
}

 

Android16 RK3576 Bluetooth source和sink模式切换 摘要:本文介绍了在RK3576平台上实现蓝牙A2DP模式动态切换的修改方案。通过在device配置文件中添加A2DP Sink相关属性,修改Bluetooth模块代码实现:1)在Config.java中增加persist.bluetooth.rka2dpsink属性控制模式切换;2)修改A2dpSinkStreamHandler.java取消音频焦点限制;3)调整btif_a2dp_sink.cc默认授予音频焦点。最终通过设置persist.bluetooth.rka2dpsink属性为true/false 阅读详情

相关推荐

细说VLAN之untag和tag

一、拓扑图 先给出上面拓扑图的结论,后面再详细讲解: 结论:只有PC1与PC4之间能相互通信,除此之外,所有PC之间均不能通信,比如PC1与PC3不能建立通信。 针对Ping命令的一个知识: 要想ping通某台主机,必须满足两个条件: 1,源主机的ICMP请求包必须能达到目的主机。 2,目的主机的ICMP应答包必须能回到源主机。 二、理论基础 端口的出...

NoobMaster的博客 3万+

POJ 1573 && hdu 1035 Robot Motion【简单模拟】

题目链接:http://poj.org/problem?id=1573                    http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot

free斩 1613

【SWAT水文模型】SWAT-CUP参数率定与模型验证实战指南

本文提供了SWAT水文模型参数率定与模型验证的完整实战指南。详细介绍了如何使用SWAT-CUP工具,特别是SUFI-2算法,进行自动化参数校准,并阐述了模型验证的关键步骤以避免过度拟合。内容涵盖从工程搭建、核心参数选择、迭代优化到结果分析与常见问题排查的全流程,旨在帮助用户提升SWAT模型模拟的准确性与可靠性。

weixin_29072299的博客 509

ACM-搜索之Robot Motion——hdu1035

Robot Motion 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1035 Problem Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robo

继续激情,继续奋斗。 2394

矢量网络分析仪(矢网)组成和原理简介

一、概述 矢量网络分析仪是一种常见的射频测量仪器,主要用来测量高频器件、电路及系统的性能参数,如线性参数、非线性参数、变频参数等。 1.1 分类 矢量网络分析仪一般以频率来划分,截止频率越高,价格也越贵; 根据测试端口的数量可分为:双端口、 3 端口、 4 端口、 6 端口; 常见的厂家有:美国的安捷伦( Agilent)、德国的罗德与施瓦茨( R&S) , 国内的有南京普纳和中电 41 所。 1.2 常用功能 常用的测试器件有:功分器、合路器、滤波器、衰减器、天线、电缆、放大器、混频器、双工器、耦

大音希声 5万+

YOLOv8改进 | Conv篇:基于FasterBlock与C2f结构的二次创新,参数量下降70万【附保姆级代码】

通过引入FasterBlock二次创新C2f结构,我们成功地对YOLOv8模型进行了优化,显著降低了其参数量,并提升了推理速度,而精度保持不变甚至有所提升。该模型在多个实际应用场景中展现出了优异的性能,尤其是在嵌入式设备、移动端和自动驾驶等领域,具有广泛的应用潜力。未来的研究将进一步优化模型结构,探索更多的应用场景,为目标检测技术的发展做出贡献。

一键难忘的博客 3238

csuoj 1112: 机器人的指令

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1112 1112: 机器人的指令 Time Limit:1 SecMemory Limit:128 MBSubmit:1153Solved:386[Submit][Status][Web Board] Description 数轴原点有一个机器人。该机器人将...

weixin_30808575的博客 565

概率质量函数(PMF)、概率密度函数(PDF)、累积分布函数(CDF)

1.概率分布函数(Probability Distribution Functions) 笔记来源:Probability Distribution Functions (PMF, PDF, CDF) 1.1 离散型:PMF和CDF 1.2 连续型:PDF和CDF 累积分布函数的导数 === 概率密度函数的函数值 dF(x)dx=f(x) \frac{dF(x)}{dx}=f(x) dxdF(x)​=f(x) 概率密度函数与 xxx 轴围成的面积 === 累积分布函数的函数值 ∫−∞xf(x)d

一个搬运知识的笨小孩 3万+

Robot Motion HDU - 1035(奇异DFS)

题目的大意就是给你一个地图,地图上不是传统的.和# 等等东西,而是方向N S W E ,你走到那个点,那个点是什么方向就是接下来你走的方向,一开始会给你入口(一开始的方向点),判断能不能走出迷宫(四个边界,走出任意一个都可以),如果走不出去(在地图里面循环的情况)。走出就求出步数,走不出去,就写出一开始走几步就开始了几个一周期的循环(步数表示)。 一个机器人已经被编程成按照其路径上的指令进行...

博客 519

【零基础入门unity游戏开发——unity3D篇】unity CharacterController 3D角色控制器最详细的使用介绍,并实现俯视角、第三人称角色控制(附项目源码)

【unity小技巧】unity最完美的CharacterController 3d角色控制器,实现移动、跳跃、下蹲、奔跑、上下坡、物理碰撞效果,复制粘贴即用但是我发现很多人还是不理解,都跑来私信问我,所以决定重新做一个更加详细且简单控制器,主要是实现俯视角和第三人称角色控制。添加一个胶囊体,添加Character Controller组件,记得删除Capsule Collider组件,因为Character Controller自带了碰撞,所以不需要Character Controller参数介绍。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 5031

【从零开始学习 UVM】1.1、UVM 概述 —— Preface

【从零开始学习 UVM】1.1、UVM 概述 —— Preface

努力,加油每一天鸭~ 840

hdu 4756 Robot(简单递推)

Robot Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 313    Accepted Submission(s): 123 Problem Description Michael has a telecontro

记录成长的点点滴滴. . . . . 301

AgentScope 2.0 API完全指南:构建企业级AI智能体应用的技术实践

本文系统梳理AgentScope 2.0框架的完整API体系,涵盖智能体核心引擎、统一模型适配层、工具系统、消息协议、权限控制与工作空间管理等七大核心模块。通过深入的源码级分析与架构图示,详细解读Agent类的推理-行动循环、ChatModelBase的多模型抽象、Toolkit的工具管理机制、ContentBlock的多模态内容表达、PermissionEngine的安全决策流程,以及Local/Docker/E2B三种工作空间实现。文章还提供了智能客服系统的完整实战案例,展示从自定义工具开发到流式服务部

program哲学 507

杭电 hdu 1035 Robot Motion

嘿嘿,一次AC……哦耶~~~ 总得来说不算是难题,不过在处理坐标的时候出了问题,呃,这个代码中会有略微提到,因为比较简单,就不多说了,相信通过自己测试数据,也能理解的。   /* THE PROGRAM IS MADE BY PYY */ /*--------------

panyanyany(飞雨幻沙) 1203

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

HDU 1035 Robot Motion(dfs + 模拟)

嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035 这道题比较简单,但自己一直被卡,原因就是在读入mp这张字符图的时候用了scanf被卡... 注意初始化和dfs边界:如果超出图或者曾经被标记过则输出 AC代码: 1 #include<cstdio> 2 #inclu...

weixin_30408739的博客 243

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

HDU 1035 Robot Motion

链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1035 题意 : 一个机器人在图上走,图上的E,W,N,S分别代表四个方向——右,左,上,下 最终机器人有两种下场:1、进入一个循环;2、走出图 走出图的话输出总共的步数,进入循环的话输出进入循环前的步数和循环的步数 简单模拟(DFS),used数组来记录步数 #include

azhuge233的博客 329

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

POJ 1573 - Robot Motion

很简单的图遍历,模拟即可,但我还是想了好长时间,好在1遍AC了 #include #include using namespace std; char mat[15][15]; int vis[15][15]; int n,m,k,ct,q; void dfs(int i,int j) { if(in||j>m)return; if(vis[i][j])

DC的ACM奋斗史 714

ACM题目————Robot Motion

Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions a...

weixin_30485799的博客 127

POJ-1573 Robot Motion 模拟水题

  题目链接:http://poj.org/problem?id=1573   随便模拟,水水~ 1 //STATUS:G++_AC_0MS_696KB 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 #include<math.h> 6 #...

weixin_30241919的博客 111

Robot Motion (DFS)

题目链接:Robot Motion 题目 Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15129 Accepted Submission(s): 7041 Problem Description A r...

wait 251

机器人测试 ACM

DescriptionA robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (...

Hello,小高同学 6506

Robot Motion

链接:http://poj.org/problem?id=1573 Problem  Description:   机器人已经被编程以遵循其路径中的指示。机器人要移动的下一个方向的说明放在网格中。可能的指令是  N北(页面上)  S南(页面下)  E东(页面右)  W西(页面左)  例如,假设机器人在北面开始(顶部)网格1的一侧并开始向南(向下)。显示了机器人遵循的路径。离开电网之前,机器人...

innocence的博客 275
上一篇: csuacm H - 爬楼梯
下一篇: tomcat部署多个springboot项目
LangoRow
博客等级 码龄8年 2粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值