POJ1573(Robot Motion)--简单模拟+简单dfs
题意
:
问你按照图中所给的提示走,多少步能走出来???
其实只要根据这个提示走下去就行了。模拟每一步就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的更多相关文章
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
- POJ1573——Robot Motion
Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
...
- POJ 1573 Robot Motion(模拟)
题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...
- POJ1573 Robot Motion(模拟)
题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...
- poj1573&&hdu1035 Robot Motion(模拟)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...
- POJ-1573 Robot Motion模拟
题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...
- poj1573 Robot Motion(DFS)
题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...
- hdu 1035 Robot Motion(模拟)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
随机推荐
- Python3基础 函数 多值参数 元组与字典形式(键值对分别指出)
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Qt获取时间戳作为图片名
Qt获取时间戳作为图片名 //保存图片 void SaveRealsenseImg() { QString picIndexName = dataSavePath; picIndexName.appe ...
- wpf日期控件
/// <summary> /// Value converter to convert a datetime object to the specified string format. ...
- Spring cloud微服务安全实战-6-8sentinel限流实战
阿里2018年开源的. 简单来说就是干三件事,最终的结果就是保证你的服务可用,不会崩掉.保证服务高可用. 流控 先从最简单的场景来入手. 1.引用一个依赖, 2,声明一个资源. 3.声明一个规则 注意 ...
- 算法习题---5.8Unix ls命令(Uva400)
一:题目 输入正整数n以及n个文件名,将这n个文件名按照ASCII优先升序排列,按列优先方式左对齐输出. 注意:文件名最长的为M,那么最右列字符串长度为M,其他列的长度为M+2 注意:一列最多允许出现 ...
- 基于Java API for WebSocket (JSR-356)的标准websocket客户端
maven依赖 springboot <dependency> <groupId>org.springframework.boot</groupId> <ar ...
- Amazon MWS Scratchpad
https://mws.amazonservices.com/scratchpad/index.html Use this page to test Amazon MWS API request an ...
- Selenium WebDriver原理
WebDriver原理 WebDriver是按照Server-Client的经典设计模式设计的. Server端就是RemoteServer,可以是任意的浏览器,当我们的脚本启动浏览器后,该浏览器就是 ...
- 数据挖掘经典算法PrefixSpan的一个简单Python实现
前言 用python实现了一个没有库依赖的"纯" py-based PrefixSpan算法. Github 仓库 https://github.com/Holy-Shine/Pr ...
- Linux下的Curses库的源码下载与安装
curses库是可以在linux终端中写出字符用户界面的一个库,现在较新的版本应该是ncurses库,现在已经很少有人再去使用他了,所以想拥有这个库并且在linux下写出自己用户界面的可以参考一下本博 ...