题目在这里

题意

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

其实只要根据这个提示走下去就行了。模拟每一步就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. ubuntu18 更换屏幕分辨率

    ubuntu18.04怎么修改屏幕分辨率 最近在自己的电脑中安装了ubuntu18.04系统,默认分辨率不对所以只好自己修改分辨率,但是在桌面右键并没找到设置分辨率的选项,那么我们应该在哪里设置分辨率 ...

  2. openresty开发系列28--openresty中操作mysql

    openresty开发系列28--openresty中操作mysql Mysql客户端   应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢?   ...

  3. Java 有双引号的字符串处理

    public class Test{ public static void main(String[] args){ String str1 = "\"name\"&qu ...

  4. 泡泡一分钟:LandmarkBoost: Efficient Visual Context Classifiers for Robust Localization

    Marcin Dymczyk, Igor Gilitschenski, Juan Nieto, Simon Lynen, Bernhard Zeis, and Roland Siegwart Land ...

  5. 【笔记】第六章、Linux 的文件权限与目录配置

    Linux的每个文件中,依据权限分为使用者.群组与其他人三种身份: 群组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个群组的支持: 利用ls -l显示的文件属性中,第一个字段是 ...

  6. Python - Django - 添加首页尾页上一页下一页

    添加首页和尾页: views.py: from django.shortcuts import render from app01 import models def book_list(reques ...

  7. 02点睛Spring MVC 4.1-@RequestMapping

    转发地址:https://www.iteye.com/blog/wiselyman-2213907 2.1 @RequestMapping @RequestMapping是SpringMVC的核心注解 ...

  8. 为做个程序员英语字典,我处理了StackOverflow和HackerNews10年5千万条数据

    有点标题党,不过都说都真实的.英语技能对开发员人员至关重要.所有人都不喜欢背单词,但更惨的是背住的单词发现没怎么用,又慢慢地忘记了.本来计划给自己做个开发人员常用单词表,感觉可能对其它人也有用,所以就 ...

  9. 使用第三方Java类库jaudiotagger完成Flac音频文件metadata(元数据)的读和修改

    最近需要使用Java Swing做个读写Flac格式音频文件的小GUI工具,虽然Mp3tag完全可以完成Flac的读写(编辑)任务,但是为了简化工作流程(编辑Flac信息后调用其它系统的接口完成部分信 ...

  10. htm5手机端实现拖动图片

    htm5手机端实现拖动图片 <pre> <!doctype html><html><head> <title>Mobile Cookbook ...