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;
}

 

保姆级教程:RK3588部署yolo目标检测模型 RK3588部署yolo目标检测模型非常详细的保姆级教程 阅读详情

相关推荐

C#实战:如何用winmm.dll打造毫秒级高精度定时器(附完整代码)

本文详细介绍了在C#中利用winmm.dll实现毫秒级高精度定时器的完整方案。针对.NET内置定时器精度不足的问题,通过调用timeSetEvent等原生API,提供了超越系统时钟中断周期的精准定时能力。文章包含核心原理剖析、完整的封装类代码以及音视频处理、游戏开发等场景下的实战应用与性能调优指南。

ujm5678901的博客 690

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斩 1611

汽车底盘技术解析:从悬架调校到智能化,如何评价国产车底盘实力

汽车底盘作为决定车辆驾驶质感、安全性和耐久性的核心系统,其技术内涵远不止于悬架结构。从行驶系、转向系到制动系的协同工作,底盘调校是一门融合机械原理与工程实践的艺术。在电动化与智能化浪潮下,底盘技术正经历从被动适应到主动响应的变革,CDC可变阻尼减震器和空气悬架等技术的应用,让底盘具备了通过OTA升级实现性能优化的可能。国产汽车品牌如奇瑞、吉利等,通过平台化架构研发与调校经验积累,正推动底盘技术从“能用”向“好用”乃至“塑造独特驾驶风格”迈进。理解底盘技术,需超越简单的结构对比,关注其在实际路况中的滤震性、支

weixin_34413103的博客 387

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

TransUnet官方代码训练自己数据集(彩色RGB3通道图像的分割)

官方代码:https://github.com/Beckschen/TransUNet 目的:训练5个类别的汽车部件分割任务(测试在另一篇博客中) 实现效果: 1. github下载代码,并解压。 项目里的文件可能跟你下载的不一样,不急后面会讲到! 2. 配置数据集(尽最大努力还原官方数据集的格式)。 通常自己手上的数据集分images和labels文件夹,分别存放着原始图像和对应的mask图像,如下图所示; mask图像中的像素有0,1,2,3,4 分别代表背景,车身,轮子,车灯,窗户,一共五个

qq_37652891的博客 5万+

宇树G1嵌入式软件架构及技术实现

该架构通过硬件加速(FPGA)+确定性调度(RTX5)的组合,在1ms控制周期内完成从传感器采集到43轴输出的全链路处理,相比波士顿动力系统缩短40%的着地稳定时间。,结合ARM Cortex-R5与FPGA的异构计算能力,实现43自由度1kHz实时控制。

m0_60674045的博客 4311

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的博客 564

OceanBase迁移平台OMS企业版部署

OceanBase迁移平台OMS企业版部署摘要: 本文档详细介绍了OceanBase迁移平台OMS企业版的部署流程,包含环境准备、OAT部署管理、MetaDB部署等关键步骤。

Sebastien23的博客 521

Robot Motion HDU - 1035(奇异DFS)

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

博客 517

Opus Clip AI技术浅析(一)

Opus Clip AI 是一款由Opus公司推出的AI视频剪辑工具,旨在通过人工智能技术简化视频编辑过程,帮助用户快速从长视频中提取精彩片段,并生成适合社交媒体分享的短视频。

m0_75253143的博客 2152

【100%通过率】华为OD机试真题 Python 实现【字符串重新排列】【2022.11 Q4新题】

给定一个字符串s,s包括以空格分隔的若干个单词,请对s进行如下处理后输出: 1、单词内部调整:对每个单词字母重新按字典序排序 2、单词间顺序调整: 1)统计每个单词出现的次数,并按次数降序排列 2)次数相同,按单词长度升序排列 3)次数和单词长度均相同,按字典升序排列 请输出处理后的字符串,每个单词以一个空格分隔。

MISAYAONE的博客 7982

Robot Motion

Problem 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 (up the page)S south (down the page)E east (to th

Mike的专栏 1158

ESP32学习笔记06-UART 的485 通讯模式

通信方式选择UART 控制器支持多种通信模式,使用函数 uart_set_mode() 可以选择模式。选择特定模式后,UART 驱动程序将处理已连接 UART 设备的相应行为。例如,主机开始向 Tx FIFO 缓冲区写入数据时,UART 驱动程序会自动置位 RTS 管脚(逻辑 1);最后一位数据传输完成后,驱动程序就会取消置位 RTS 管脚(逻辑 0)。要使用此模式,软件。

Car12 2476

杭电 hdu 1035 Robot Motion

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

panyanyany(飞雨幻沙) 1203

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

HDU 1035 Robot Motion

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

azhuge233的博客 328

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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奋斗史 713

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

机器人测试 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,小高同学 6499

POJ1573《Robot Motion》方法:模拟

纯粹的模拟,无难度 #include #include #include using namespace std; int main() { int row, col, entry; char grid[12][12]; //freopen("temp.txt", "r", stdin); while (true) { memset(grid, 'O', sizeof(gr

相门码农的专栏 525
上一篇: 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、付费专栏及课程。

余额充值