【POJ - 1573】Robot Motion
-->Robot Motion
直接中文
Descriptions:

样例1

样例2
N 向上
S 向下
E 向右
W 向左
例如,假设机器人从网格1的北(顶)侧开始,从南(下)开始。机器人所遵循的路径如图所示。在离开网格之前,机器人在网格中执行10条指令。
比较网格2中发生的情况:机器人仅通过3条指令,然后通过8条指令开始循环,并且永不退出。
您将编写一个程序来确定机器人离开网格所需的时间或机器人如何循环。
Input
Output
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
题目链接
https://vjudge.net/problem/POJ-1573
没啥说的,直接dfs搜就行
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 25
using namespace std;
int r,c,s;//行,列,从第s列开始
char mp[Maxn][Maxn];//地图
int vis[Maxn][Maxn];//标记是否走过
int step[Maxn][Maxn];//(x,y)是第几步 //(x,y) 下一步的字母标志 到(x,y)已是第step步
void dfs(int x,int y,char op,int start)
{
//走出迷宫
if(mp[x][y]=='X'||x<&&y<&&x>r&&y>c)
cout<<start<<" step(s) to exit"<<endl;
//(x,y)没走过
else if(!vis[x][y])
{
vis[x][y]=;
step[x][y]=start+;
//四种走法
if(op=='N')
return dfs(x-,y,mp[x-][y],step[x][y]);
if(op=='S')
return dfs(x+,y,mp[x+][y],step[x][y]);
if(op=='E')
return dfs(x,y+,mp[x][y+],step[x][y]);
if(op=='W')
return dfs(x,y-,mp[x][y-],step[x][y]);
}
//(x,y)走过,输出即可
else if(vis)
cout<<step[x][y]-<<" step(s) before a loop of "<<start+-step[x][y]<<" step(s)"<<endl;
}
int main()
{
while(cin>>r>>c>>s,r+c+s)
{
//初始化
MEM(mp,'X');
MEM(vis,);
MEM(step,);
for(int i=; i<=r; i++)
for(int j=; j<=c; j++)
cin>>mp[i][j];
step[][s]=;
dfs(,s,mp[][s],step[][s]);
}
}
【POJ - 1573】Robot Motion的更多相关文章
- POJ 1573:Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11324 Accepted: 5512 Des ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- PySide——Python图形化界面入门教程(三)
PySide——Python图形化界面入门教程(三) ——使用内建新号和槽 ——Using Built-In Signals and Slots 上一个教程中,我们学习了如何创建和建立交互widget ...
- InstallUtil.exe版本引起安装windows services 服务遇到的问题,System.BadImageFormatException
原文:把程序安装成windows服务的过程及遇到的问题 做好了定时任务的程序,要把它放在服务器上,作为windows服务运行,也就是说,退出登录,用户注销后程序任然在后台运行. 将exe程序发布为服务 ...
- Win10《芒果TV》更新v3.8.30流星版:优化稳定性、升级无边框播放体验
随着暑假到来,大波王牌综艺和青春电视剧热浪来袭,Win10版<芒果TV>全平台同步更新流星版v3.8.30,进一步提升稳定性,巩固播放体验,升级剧场模式和画中画无边框体验,我们在芒果等你. ...
- flume本地调试
本机idea远程调试flume:https://blog.csdn.net/u012373815/article/details/60601118 遇到 [root@hadoop02 bin]# ./ ...
- SqlServer判断数据库、表、字段、存储过程、函数是否存在
原文:SqlServer判断数据库.表.字段.存储过程.函数是否存在 判断数据库是否存在 if exists (select * from sys.databases where name = '数据 ...
- Win10的UWP之进度条
原文:Win10的UWP之进度条 关于UWP的进度条的处理的方案有两种方案 我们新建一个项目,然后处理的界面如下的代码 <Grid.RowDefinitions> <RowDefin ...
- CentOS7 firewall与iptables防火墙的使用与开放端口
如何关闭firewall并开启iptables防火墙 如何使用firewall防火墙 如何关闭firewall并开启iptables防火墙 1.停止firewall systemctl stop fi ...
- Codility--- Triangle
Task description A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is tr ...
- 设计模式之单例模式的几种写法——java
对于设计模式的使用场景和好处,之前有介绍一篇,今天主要是单例模式的编写方式,直接看代码吧 单例模式之饿汉模式,不会懒加载.线程安全 /** * @Author wangtao * @Descripti ...
- 把VSCode配置成C/C++开发IDE
工作一年多了,大学学的数据结构和算法都忘得差不多了.于是想补补数据结构知识,并且刷一下剑指offer的一些面试题.首要问题就是装个C语言的运行环境,大学学C语言和C++的时候都是用的VC++6.0或者 ...