zoukankan      html  css  js  c++  java
  • F


    A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are 

    N north (up the page) 
    S south (down the page) 
    E east (to the right on the page) 
    W west (to the left on the page) 

    For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid. 

    Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits. 

    You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around. 

    Input

    There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

    Output

    For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

    Sample Input

    3 6 5
    NEESWE
    WWWESS
    SNWWWW
    4 5 1
    SESWE
    EESNW
    NWEEN
    EWSEN
    0 0 0

    Sample Output

    10 step(s) to exit
    3 step(s) before a loop of 8 step(s)
    题目大意 : 看机器人是否可以按照给的指令走出迷宫,,如果可以的话,输出步数,如果发生循环的话输出走了多少步开始循环还要输出循环步数的大小
    思路 : BFS+栈 利用栈先进去先出的性质,不用立刻删除栈中元素,,,简而言之就是保留路线,,用一个二维数组标记每一个走过方格,方便判断循环

    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<cstdio>
    #include<stack> 
    using namespace std;
    int n,m,l;
    struct stu{
        int a,b;
    };
    char arr[25][25];
    int mark[25][25]={0};
    int arr1[25][25]={0};
    int d[4][2]={{-1,0},{0,-1},{1,0},{0,1}};//N,W,S,E
    void bfs(int x,int y){
        stack<stu>st;
        st.push({x,y});
        mark[x][y]=1;
        arr1[x][y]=0;
        while(st.size()){
            int x=st.top().a;
            int y=st.top().b;
            int dx,dy,i;
            if(arr[x][y]=='N') i=0;
            if(arr[x][y]=='W') i=1;
            if(arr[x][y]=='S') i=2;
            if(arr[x][y]=='E') i=3;
            dx=x+d[i][0];
            dy=y+d[i][1];
    //        cout<<dx<<"ADS"<<dy<<endl;
    //        cout<<arr1[x][y]<<"--"<<endl;
            if(dx<0||dy<0||dx>=n||dy>=m){//越界,说明走出了迷宫
                printf("%d step(s) to exit
    ",arr1[x][y]+1);
                break;
            }
            if(mark[dx][dy]==1){//说明从dx dy处开始循环
    //            cout<<dx<<"--"<<dy<<endl;
                int sum=0;
                st.pop();//第一个肯定位dx,dy 
                while(st.size()){
    //                cout<<que.top().a<<"__"<<que.top().b<<endl;
                    
                    if(st.top().a==dx&&st.top().b==dy){
                        break;
                    }
                    else {
                        sum++;
                        st.pop();
                    }
                }
                printf("%d step(s) before a loop of %d step(s)
    ",st.size()-1,sum+2);
                //剩余的部分包括重复的第一个元素 所以要减去1
                //开头 删除了一个 然后又删除了一个, 还有一个没删除的即第一个重复元素 所以应加2 
                break;
            }
            arr1[dx][dy]=arr1[x][y]+1;
            mark[dx][dy]=1;
            que.push({dx,dy});
        }
    }
    int main()
    {
        while(cin>>n>>m>>l){
            if(n==0&&m==0&&l==0) break;
            memset(mark,0,sizeof(mark));
            for(int i=0;i<n;i++){
                scanf("%s",arr[i]);
            }
            bfs(0,l-1);
        }
        return 0;
    }





  • 相关阅读:
    flask 模板block super()的讲解
    flask-include标签使用标签
    spring boot 使用属性加载顺序
    mongodb中的_id的ObjectId的生成规则
    由整数对(父节点和子节点的关系)组成的二叉树的高度问题
    Akka in action (第一章 介绍Akka)
    数据结构学习(一)数组
    spray-json
    JMS API(二)
    JSM 学习(一)
  • 原文地址:https://www.cnblogs.com/Accepting/p/11239754.html
Copyright © 2011-2022 走看看