• POJ1573(Robot Motion)--简单模拟+简单dfs


    题目在这里

    题意

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

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

      1 /*************************************************************************
      2     > File Name: poj1573.cpp
      3     > Author: YeGuoSheng
      4     > Description:  
      5     给一个开始位置和一个标记了每个走向的迷宫,问能不能按照每一步的
      6     提示走出迷宫
      7     > Created Time: 2019年07月23日 星期二 17时27分34秒
      8  ************************************************************************/
      9 
     10 #include<iostream>
     11 #include<stdio.h>
     12 #include<cstring>
     13 #include<cmath>
     14 #include<vector>
     15 #include<stack>
     16 #include<map>
     17 #include<set>
     18 #include<list>
     19 #include<queue>
     20 #include<string>
     21 #include<algorithm>
     22 #include<iomanip>
     23 using namespace std;
     24 const int maxn = 20;
     25 char g[maxn][maxn];
     26 int vis[maxn][maxn];
     27 int gCount[maxn][maxn];//标记所访问到的点是第几步访问到的
     28 int row,col,start;
     29 int Count = 0;
     30 int t = 0;
     31 bool flag = false;//标记是否走出来或死虚幻
     32 int p;//记录上一次到达某位置的步数,防止被第二次到达的步数所覆盖
     33 
     34 void dfs2(int x,int y,int Count)
     35 {
     36     if(x==1 && y == start)//回到起始位置结束dfs
     37     {
     38         return ;
     39     }
     40 }
     41 
     42 void dfs(int x,int y,int Count)
     43 {
     44     t = Count;//记录到总步数
     45     p = gCount[x][y];//提前保存第一次某点到达的步数
     46     gCount[x][y] = t;//更新到当前访问到点的总步数
     47     if(x==0 ||y==0 || x == row+1 || y == col+1)//走出去的情况
     48     {
     49         flag = true;
     50         return;
     51     }
     52     if(vis[x][y] == 0)//如果当前结点未访问
     53     {
     54         vis[x][y] = 1;//标记为1,,然后进行下面的dfs
     55     }
     56     else // 1当前位置已经访问过,即接下来将构成死循环
     57     {
     58         flag = false;
     59         return ;
     60     }
     61     if(g[x][y] == 'W')//dfs下一个位置
     62     {
     63         dfs(x,y-1,Count+1);
     64         return ;
     65     }
     66     else if(g[x][y]=='E')
     67     {
     68         dfs(x,y+1,Count+1);
     69         return ;
     70     }
     71     else if (g[x][y] =='S')
     72     {
     73         dfs(x+1,y,Count+1);
     74         return;
     75     }
     76     else//N
     77     {
     78         dfs(x-1,y,Count+1);
     79         return ;
     80     }
     81 }
     82 
     83 int main()
     84 {
     85     while(scanf("%d%d%d",&row,&col,&start) && row != 0 && col != 0 && start != 0)
     86     {
     87         Count=0;
     88         p = 0;
     89         t = 0;
     90         memset(g,'.',sizeof(g));//边界用‘.’填充
     91         memset(vis,0,sizeof(vis));
     92         for(int i =1 ;i <= row;i++)//整个Grid加了边界
     93         {
     94             for(int j = 1;j <= col;j++)
     95             {
     96                 cin>>g[i][j];
     97             }
     98         } 
     99         dfs(1,start,0);
    100         if(flag)
    101         {
    102             // for(int i = 1;i <= row;i++)
    103             // {
    104             //     for(int j= 1;j <= col;j++)
    105             //     {
    106             //         cout<<gCount[i][j]<<" ";
    107             //     }
    108             //     cout<<endl;
    109             // }
    110             cout<<t<<" step(s) to exit"<<endl;
    111         }
    112         else
    113         {
    114             cout<<p<<" step(s) before a loop of "<<t-p<<" step(s)"<<endl;
    115         }
    116     }
    117     return 0;
    118 }
    View Code
  • 相关阅读:
    SpatiePermissionPermissionServiceProvider' not found
    ThinkPad L14G 连接外接显示屏无响应问题解决
    HBase 初学习之数据模型
    python 实现 kakfa 的 生产消费模式 和 发布订阅模式
    数据结构中树的基本概念
    MySQL行级锁、表级锁、页级锁详细介绍
    设置div只显示2行
    linux 关闭防护墙,永久关闭
    yum install nginx 时报错:No package nginx available.
    win10 查询端口,杀掉端口进程
  • 原文地址:https://www.cnblogs.com/ygsworld/p/11233705.html
Copyright © 2020-2023  润新知