题目在这里

题意

问你按照图中所给的提示走,多少步能走出来???

其实只要根据这个提示走下去就行了。模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs。如果出现loop状态,只要记忆每个所到的点的第一次的步数,最后总步数减掉它即可

 /*************************************************************************
> File Name: poj1573.cpp
> Author: YeGuoSheng
> Description:
给一个开始位置和一个标记了每个走向的迷宫,问能不能按照每一步的
提示走出迷宫
> Created Time: 2019年07月23日 星期二 17时27分34秒
************************************************************************/ #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
const int maxn = ;
char g[maxn][maxn];
int vis[maxn][maxn];
int gCount[maxn][maxn];//标记所访问到的点是第几步访问到的
int row,col,start;
int Count = ;
int t = ;
bool flag = false;//标记是否走出来或死虚幻
int p;//记录上一次到达某位置的步数,防止被第二次到达的步数所覆盖 void dfs2(int x,int y,int Count)
{
if(x== && y == start)//回到起始位置结束dfs
{
return ;
}
} void dfs(int x,int y,int Count)
{
t = Count;//记录到总步数
p = gCount[x][y];//提前保存第一次某点到达的步数
gCount[x][y] = t;//更新到当前访问到点的总步数
if(x== ||y== || x == row+ || y == col+)//走出去的情况
{
flag = true;
return;
}
if(vis[x][y] == )//如果当前结点未访问
{
vis[x][y] = ;//标记为1,,然后进行下面的dfs
}
else // 1当前位置已经访问过,即接下来将构成死循环
{
flag = false;
return ;
}
if(g[x][y] == 'W')//dfs下一个位置
{
dfs(x,y-,Count+);
return ;
}
else if(g[x][y]=='E')
{
dfs(x,y+,Count+);
return ;
}
else if (g[x][y] =='S')
{
dfs(x+,y,Count+);
return;
}
else//N
{
dfs(x-,y,Count+);
return ;
}
} int main()
{
while(scanf("%d%d%d",&row,&col,&start) && row != && col != && start != )
{
Count=;
p = ;
t = ;
memset(g,'.',sizeof(g));//边界用‘.’填充
memset(vis,,sizeof(vis));
for(int i = ;i <= row;i++)//整个Grid加了边界
{
for(int j = ;j <= col;j++)
{
cin>>g[i][j];
}
}
dfs(,start,);
if(flag)
{
// for(int i = 1;i <= row;i++)
// {
// for(int j= 1;j <= col;j++)
// {
// cout<<gCount[i][j]<<" ";
// }
// cout<<endl;
// }
cout<<t<<" step(s) to exit"<<endl;
}
else
{
cout<<p<<" step(s) before a loop of "<<t-p<<" step(s)"<<endl;
}
}
return ;
}

POJ1573(Robot Motion)--简单模拟+简单dfs的更多相关文章

  1. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

  2. POJ1573——Robot Motion

    Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...

  3. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

  4. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  5. POJ1573 Robot Motion(模拟)

    题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...

  6. poj1573&amp;&amp;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  7. POJ-1573 Robot Motion模拟

    题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...

  8. poj1573 Robot Motion(DFS)

    题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...

  9. hdu 1035 Robot Motion(模拟)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

随机推荐

  1. Gradle基础:11:使用Kotlin的Gradle(转)

    Gradle基础及进阶(转) https://blog.csdn.net/liumiaocn/article/category/8369185 Gradle基础:1: 简介与安装 Gradle基础:2 ...

  2. python接入微博第三方API之2接入用户登录和微博发布

    python接入微博第三方API之2接入用户登录和微博发布 # coding=utf-8 import requests import json import MySQLdb from datetim ...

  3. Spark累加器(Accumulator)

    一.累加器简介 在Spark中如果想在Task计算的时候统计某些事件的数量,使用filter/reduce也可以,但是使用累加器是一种更方便的方式,累加器一个比较经典的应用场景是用来在Spark St ...

  4. 宣化上人:大佛顶首楞严经四种清净明诲浅释(1)(转自学佛网:http://www.xuefo.net/nr/article23/230609.html)

    唐天竺·沙门般剌密帝译 宣化上人主讲 一九八三年四月十七日晚讲于万佛圣城 四种清净明诲,真实不虚 楞严经里的四种清净明诲:断淫.断杀.断偷.断妄,是息息相关的.若犯淫戒,就容易犯杀戒,也容易犯盗戒.妄 ...

  5. Delphi XE6 使用定时器或者线程解决程序界面无响应问题

    ---恢复内容开始--- 介绍 在手机应用上,我们不应该使用速度慢的代码,当然我们在桌面程序上也应该避免这个,当手机应用长时间没有相应的时候,程序会提示“程序没响应,是否关闭”的提示,这个非常不好,所 ...

  6. Uncaught TypeError: TableInit is not a constructor

    我最近在做东西的时候,用到了Bootstrap的表格,我复制了一份代码使用,结果运行报错 Uncaught TypeError: TableInit is not a constructor 我点进去 ...

  7. 基于jsplumb插件制作可拖拽、保存流程图、重绘保存后的流程图总结

    1.重点参考博文 https://blog.csdn.net/j_bean/article/details/78092647 2.关键点总结 1)实现可视区域图形画满后,拖动整个画布的效果 a.最好不 ...

  8. Freemarker讲解

    FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现 ...

  9. nload 源码安装

    nload 1.下载 wget http://www.roland-riegel.de/nload/nload-0.7.4.tar.gz 2.解压 tar zvxf nload-0.7.4.tar.g ...

  10. 【转帖】docker 如何删除none镜像

    https://blog.csdn.net/hicoldcat/article/details/80802447 shell 命令博大精深 需要仔细学习 删除none的镜像,要先删除镜像中的容器.要删 ...