zoukankan      html  css  js  c++  java
  • POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573

    题目链接:http://poj.org/problem?id=1573

    Language:
    Robot Motion
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 14195   Accepted: 6827

    Description


    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)
    

    Source

    题目大意:WESN分别代表四个方向,第三个数据代表第一行的第几个位置开始,如果能走出去则输出走了几步,如果不能走出去进入了循环则输出第几步进入了循环,循环有几步。按题目标准格式输出。

    解题思路:水题,作个标记代表第几步,如果下一步被标记过了则退出循环输出,如果走出去了也退出循环。

    AC 代码:

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    char a[15][15];
    int b[15][15];
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        int n,m,k;
        while(cin>>n>>m,n&&m)
        {
            cin>>k;
            mem(a,0);
            mem(b,0);
            for(int i=1;i<=n;i++)
                cin>>a[i]+1;
            int x=1,y=k;
            b[x][y]=1;
            int ans=1;
            int flag=0;
            while(1)
            {
                if(a[x][y]=='W')y--;
                else if(a[x][y]=='S')x++;
                else if(a[x][y]=='E')y++;
                else if(a[x][y]=='N')x--;
                if(x==0||x==n+1||y==0||y==m+1)
                {
                    printf("%d step(s) to exit
    ",ans);
                    break;
                }
                else if(b[x][y])
                {
                    printf("%d step(s) before a loop of %d step(s)
    ",b[x][y]-1,ans-b[x][y]+1);
                    break;
                }
                else
                {
                    b[x][y]=++ans;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    【ClickHouse 技术系列】 ClickHouse 聚合函数和聚合状态
    【ClickHouse 技术系列】 ClickHouse 中的嵌套数据结构
    报表功能升级|新增的这4项图表组件太太太好用了吧!
    【视频特辑】数据分析师必备!快速制作一张强大好用的大宽表
    使用云效Codeup10分钟紧急修复Apache Log4j2漏洞
    技术干货 | 使用 mPaaS 配置 SM2 国密加密指南
    “全”事件触发:阿里云函数计算与事件总线产品完成全面深度集成
    3类代码安全风险如何避免?
    为余势负天工背,云原生内存数据库Tair助力用户体验优化
    LeetCode_Search for a Range
  • 原文地址:https://www.cnblogs.com/teble/p/7243614.html
Copyright © 2011-2022 走看看