UVA118 HDU1620 Mutant Flatworld Explorers【模拟】

Mutant Flatworld Explorers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 207 Accepted Submission(s): 52

Problem Description
Robotics, robot motion planning, and machine learning are areas that cross the boundaries of many of the subdisciplines that comprise Computer Science: artificial intelligence, algorithms and complexity, electrical and mechanical engineering to name a few. In addition, robots as turtles'' (inspired by work by Papert, Abelson, and diSessa) and asbeeper-pickers’’ (inspired by work by Pattis) have been studied and used by students as an introduction to programming for many years.

This problem involves determining the position of a robot exploring a pre-Columbian flat world

Given the dimensions of a rectangular grid and a sequence of robot positions and instructions, you are to write a program that determines for each sequence of robot positions and instructions the final position of the robot.

A robot position consists of a grid coordinate (a pair of integers: x-coordinate followed by y-coordinate) and an orientation (N,S,E,W for north, south, east, and west). A robot instruction is a string of the letters ‘L’, ‘R’, and ‘F’ which represent, respectively, the instructions:

Left: the robot turns left 90 degrees and remains on the current grid point.
Right: the robot turns right 90 degrees and remains on the current grid point.
Forward: the robot moves forward one grid point in the direction of the current orientation and mantains the same orientation.
The direction North corresponds to the direction from grid point (x,y) to grid point (x,y+1).

Since the grid is rectangular and bounded, a robot that moves off'' an edge of the grid is lost forever. However, lost robots leave a robotscent’’ that prohibits future robots from dropping off the world at the same grid point. The scent is left at the last grid position the robot occupied before disappearing over the edge. An instruction to move ``off’’ the world from a grid point from which a robot has been previously lost is simply ignored by the current robot.

Input
The first line of input is the upper-right coordinates of the rectangular world, the lower-left coordinates are assumed to be 0,0.

The remaining input consists of a sequence of robot positions and instructions (two lines per robot). A position consists of two integers specifying the initial coordinates of the robot and an orientation (N,S,E,W), all separated by white space on one line. A robot instruction is a string of the letters ‘L’, ‘R’, and ‘F’ on one line.

Each robot is processed sequentially, i.e., finishes executing the robot instructions before the next robot begins execution.

Input is terminated by end-of-file.

You may assume that all initial robot positions are within the bounds of the specified grid. The maximum value for any coordinate is 50. All instruction strings will be less than 100 characters in length

Output
For each robot position/instruction in the input, the output should indicate the final grid position and orientation of the robot. If a robot falls off the edge of the grid the word ``LOST’’ should be printed after the position and orientation.

Sample Input
5 3
1 1 E
RFRFRFRF
3 2 N
FRRFLLFFRRFLL
0 3 W
LLFFFLFLFL

Sample Output
1 1 E
3 3 N LOST
2 3 S

Source
UVA

问题链接UVA118 HDU1620 Mutant Flatworld Explorers
问题简述:(略)
问题分析:模拟机器人运动过程的题,给程序代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA118 HDU1620 Mutant Flatworld Explorers */

#include <bits/stdc++.h>

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const char d[] = "NESW";
const int N = 50 + 1;
const int L = 100 + 1;
char start, block[N][N], step[L];

int main()
{
    map<char, int> mp;
    mp['N'] = 0;
    mp['E'] = 1;
    mp['S'] = 2;
    mp['W'] = 3;
    memset(block, 0, sizeof(block));

    int n, m, x, y;
    scanf("%d%d", &n, &m);
    while(~scanf("%d %d %c%s", &x, &y, &start, step)) {
        int dir = mp[start], flag = 0;

        for(int i = 0; step[i]; i++)
            if(step[i] == 'F') {
                int nx = x + dx[dir];
                int ny = y + dy[dir];
                if(nx < 0 || nx > n || ny < 0 || ny > m) {
                    if(block[x][y]) continue;
                    block[x][y] = 1;
                    flag = 1;
                    break;
                }
                x = nx;
                y = ny;
            } else if(step[i] == 'R')
                dir = (dir + 1) % 4;
            else if(step[i] == 'L')
                dir = (dir - 1 + 4) % 4;

        printf("%d %d %c", x, y, d[dir]);
        if(flag) printf(" LOST");
        printf("\n");
    }

    return 0;
}
原创文章 2323 获赞 2382 访问量 269万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105781087
118