POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot MotionDFS+BFS

Posted tigerisland45

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot MotionDFS+BFS相关的知识,希望对你有一定的参考价值。

Robot Motion
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16166 Accepted: 7643
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

Mid-Central USA 1999

Regionals 1999 >> North America - Mid-Central USA

问题链接POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot Motion
问题简述:(略)
问题分析
????有r*c区域,机器人从第一行的某列进入,该区域全部由‘N‘ , ‘S‘ , ‘W‘ , ‘E‘ ,走到某个区域的时候只能按照该区域指定的方向进行下一步,计算机器人能否走出该片区域,若不能则输出开始绕圈的步数和圈的大小。
????这个问题有2种解法,DFS和BFS都可以。用BFS来解时,需要用STL的队列来辅助。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序(HDU以外,DFS)如下:

/* POJ1573  ZOJ1708 UVA10116 UVALive5334 Robot Motion */

#include <stdio.h>
#include <string.h>

#define N 10
char maze[N][N + 1];
int vis[N][N], step[N][N], r, c, cnt;

void dfs(int row, int col)
{
    cnt++;
    if(row < 0 || row >= r || col < 0 || col >= c)
                printf("%d step(s) to exit
", cnt);
    else if(vis[row][col])
        printf("%d step(s) before a loop of %d step(s)
", step[row][col], cnt - step[row][col]);
    else {
        vis[row][col] = 1;
        step[row][col] = cnt;
        if(maze[row][col] == 'S') dfs(row + 1, col);
        if(maze[row][col] == 'N') dfs(row - 1, col);
        if(maze[row][col] == 'E') dfs(row, col + 1);
        if(maze[row][col] == 'W') dfs(row, col - 1);
    }
}

int main(void)
{
    int enter, i;
    while(scanf("%d%d%d", &r, &c, &enter) && (r || c || enter)) {
        for(i = 0; i < r; i++)
            scanf("%s", maze[i]);

        memset(vis, 0, sizeof(vis));
        cnt = -1;
        dfs(0, enter - 1);
    }

    return 0;
}

AC的C语言程序(HDU,DFS)如下:

/* HDU1035 Robot Motion */

#include <stdio.h>
#include <string.h>

#define N 10
char maze[N][N + 1];
int vis[N][N], step[N][N], r, c, cnt;

void dfs(int row, int col)
{
    cnt++;
    if(row < 0 || row >= r || col < 0 || col >= c)
                printf("%d step(s) to exit
", cnt);
    else if(vis[row][col])
        printf("%d step(s) before a loop of %d step(s)
", step[row][col], cnt - step[row][col]);
    else {
        vis[row][col] = 1;
        step[row][col] = cnt;
        if(maze[row][col] == 'S') dfs(row + 1, col);
        if(maze[row][col] == 'N') dfs(row - 1, col);
        if(maze[row][col] == 'E') dfs(row, col + 1);
        if(maze[row][col] == 'W') dfs(row, col - 1);
    }
}

int main(void)
{
    int enter, i;
    while(scanf("%d%d", &r, &c) && (r || c)) {
        scanf("%d", &enter);
        for(i = 0; i < r; i++)
            scanf("%s", maze[i]);

        memset(vis, 0, sizeof(vis));
        cnt = -1;
        dfs(0, enter - 1);
    }

    return 0;
}

AC的C++语言程序(HDU以外,BFS)如下:

/* POJ1573  ZOJ1708 UVA10116 UVALive5334 Robot Motion */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int N = 10;
char maze[N][N + 1];
bool vis[N][N];
int step[N][N], r, c, enter, cnt;

struct Node {
    int row, col;
    Node(int r = 0, int c = 0) : row(r), col(c) {}
};

void bfs()
{
    memset(vis, false, sizeof(vis));
    step[0][enter - 1] = 0;
    queue<Node> q;
    q.push(Node(0, enter - 1));
    int cnt = -1;
    while(!q.empty()) {
        Node t = q.front();
        q.pop();
        cnt++;
        if(t.row < 0 || t.row >= r || t.col < 0 || t.col >= c)
                    printf("%d step(s) to exit
", cnt);
        else if(vis[t.row][t.col])
            printf("%d step(s) before a loop of %d step(s)
", step[t.row][t.col], cnt - step[t.row][t.col]);
        else {
            vis[t.row][t.col] = true;
            step[t.row][t.col] = cnt;
            if(maze[t.row][t.col] == 'S') q.push(Node(t.row + 1, t.col));
            if(maze[t.row][t.col] == 'N') q.push(Node(t.row - 1, t.col));
            if(maze[t.row][t.col] == 'E') q.push(Node(t.row, t.col + 1));
            if(maze[t.row][t.col] == 'W') q.push(Node(t.row, t.col - 1));
        }
    }
}

int main()
{
    while(scanf("%d%d%d", &r, &c, &enter) && (r || c || enter)) {
        for(int i = 0; i < r; i++)
            scanf("%s", maze[i]);

        bfs();
    }

    return 0;
}

AC的C++语言程序(HDU,BFS)如下:

/* HDU1035 Robot Motion */

#include <bits/stdc++.h>

using namespace std;

const int N = 10;
char maze[N][N + 1];
bool vis[N][N];
int step[N][N], r, c, enter, cnt;

struct Node {
    int row, col;
    Node(int r = 0, int c = 0) : row(r), col(c) {}
};

void bfs()
{
    memset(vis, false, sizeof(vis));
    step[0][enter - 1] = 0;
    queue<Node> q;
    q.push(Node(0, enter - 1));
    int cnt = -1;
    while(!q.empty()) {
        Node t = q.front();
        q.pop();
        cnt++;
        if(t.row < 0 || t.row >= r || t.col < 0 || t.col >= c)
                    printf("%d step(s) to exit
", cnt);
        else if(vis[t.row][t.col])
            printf("%d step(s) before a loop of %d step(s)
", step[t.row][t.col], cnt - step[t.row][t.col]);
        else {
            vis[t.row][t.col] = true;
            step[t.row][t.col] = cnt;
            if(maze[t.row][t.col] == 'S') q.push(Node(t.row + 1, t.col));
            if(maze[t.row][t.col] == 'N') q.push(Node(t.row - 1, t.col));
            if(maze[t.row][t.col] == 'E') q.push(Node(t.row, t.col + 1));
            if(maze[t.row][t.col] == 'W') q.push(Node(t.row, t.col - 1));
        }
    }
}

int main()
{
    while(scanf("%d%d", &r, &c) && (r || c)) {
        scanf("%d", &enter);
        for(int i = 0; i < r; i++)
            scanf("%s", maze[i]);

        bfs();
    }

    return 0;
}

以上是关于POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot MotionDFS+BFS的主要内容,如果未能解决你的问题,请参考以下文章

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA10670 POJ1907 ZOJ2372 Work Reduction贪心

UVA10081 POJ2537 ZOJ1883 Tight WordsDP

UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学

POJ2689 ZOJ1842 UVA10140 Prime Distance筛选法

POJ2351 ZOJ1916 UVA10371 Time Zones时区计算