题目在这里

题意

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

其实只要根据这个提示走下去就行了。模拟每一步就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. VMWare-Linux NAT模式联网配置

    VMWare-Linux NAT模式联网配置   摘自:https://blog.csdn.net/a56112777/article/details/83053566 (注意使用root用户) 1. ...

  2. snmp oid 和厂商对应关系

    <node oid="default" name="Unknown" type="workstation"/><node ...

  3. Windows2008R2+iis7.5环境下的dz论坛X3版伪静态设置教程

    Windows2008R2+iis7.5环境下的dz论坛X3版伪静态设置教程 因为2008R2不是那么的普及,加上X3版新出不久,所以伪静态的设置教程比较少,今天搞出来了,其实很简单,那么下面给大家简 ...

  4. 【Leetcode_easy】872. Leaf-Similar Trees

    problem 872. Leaf-Similar Trees 参考 1. Leetcode_easy_872. Leaf-Similar Trees; 完

  5. Git出现There is no tracking information for the current branch提示的解决办法

    参考:https://blog.csdn.net/sinat_36246371/article/details/79738782 在执行git pull的时候,提示当前branch没有跟踪信息: Th ...

  6. pickle.load EOFError: Ran out of input

    错误原因:pickle.loads()的次数超过了pickle.dumps()的次数 https://www.cnblogs.com/cmnz/p/6986667.html

  7. Vue简单基础 + 实例 及 组件通信

    vue的双向绑定原理:Object.defineProperty() vue实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty() 来劫 ...

  8. Git常用命令及方法

    https://blog.csdn.net/web_csdn_share/article/details/79243308 Git常用命令及方法大全 下面是我整理的常用 Git 命令清单.几个专用名词 ...

  9. 说说Spring XML的头

    部分内容截取自(http://blog.csdn.net/zhch152/article/details/8191377,http://iswift.iteye.com/blog/1657537) 在 ...

  10. Linux 下 安装 Jmeter

    Linux下安装jmeter 1.1 下载JMeter 官方网站下载最新版本: http://jmeter.apache.org/download_jmeter.cgi    两个只是解压方式不一样而 ...