题目在这里

题意

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

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

    最近在学习使用iptables做网关服务器,即SNAT设置 我们的宿主机有两块网卡,地址分别是: 192.168.6.108/24 192.168.66.119/24 我们的目标是在虚拟机VMware ...

  2. [原][OE][官方例子]osgearth_features OE地球添加shp文件(特征标识)

    OE所有官方样例 官方示例代码 /* -*-c++-*- */ /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph * Co ...

  3. Jmeter 时间函数工具汇总

    在使用Jmeter过程中,常使用的函数汇总 __time : 获取时间戳.格式化时间 ${__time(yyyy-MM-dd HH:mm:ss:SSS,time)}  :格式化生成时间格式 2018- ...

  4. 有些lambda表达式就可以体现出编程中「Context(上下文)」环境

    编程中什么是「Context(上下文)」?   每一段程序都有很多外部变量.只有像Add这种简单的函数才是没有外部变量的.一旦你的一段程序有了外部变量,这段程序就不完整,不能独立运行.你为了使他们运行 ...

  5. netty5自定义私有协议实例

    一般业务需求都会自行定义私有协议来满足自己的业务场景,私有协议也可以解决粘包和拆包问题,比如客户端发送数据时携带数据包长度,服务端接收数据后解析消息体,获取数据包长度值,据此继续获取数据包内容.我们来 ...

  6. 基于C#在WPF中使用斑马打印机进行打印【转】——不支持XPS的打印机

    https://www.cnblogs.com/zhaobl/p/4666002.html

  7. 【linux学习笔记二】常见目录的作用

  8. 【ARTS】01_41_左耳听风-201900819~201900825

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  9. c# stmp邮件发送

    最近用到了邮件发送功能,因为stmp设置.参数传递错误等一些问题走了弯路,虽然代码很简单,这里还是记录一下,方便下次查阅. 用个人163邮箱测试的邮件发送 class Program { static ...

  10. LeetCode 168. Excel表列名称(Excel Sheet Column Title)

    168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...