24h購物| | PChome| 登入
2012-05-30 08:23:50| 人氣6,356| 回應0 | 上一篇 | 下一篇

[UVA][模擬] 118 - Mutant Flatworld Explorers

推薦 0 收藏 0 轉貼0 訂閱站台


 Mutant Flatworld Explorers 

Background

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 as ``beeper-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.

The Problem

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 robot ``scent'' 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.

The 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.

The 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


#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, m, sx, sy;
    char D[2], cmd[1000], pre[100][100] = {};
    scanf("%d %d", &n, &m);
    while(scanf("%d %d %s", &sx, &sy, D) == 3) {
        scanf("%s", cmd);
        int d = D[0], flag = 0, i;
        for(i = 0; cmd[i]; i++) {
            if(cmd[i] == 'F') {
                switch(d) {
                    case 'N':sy++;break;
                    case 'E':sx++;break;
                    case 'W':sx--;break;
                    case 'S':sy--;break;
                }
            } else if(cmd[i] == 'R') {
                switch(d) {
                    case 'N':d = 'E';break;
                    case 'E':d = 'S';break;
                    case 'W':d = 'N';break;
                    case 'S':d = 'W';break;
                }
            } else {
                switch(d) {
                    case 'N':d = 'W';break;
                    case 'E':d = 'N';break;
                    case 'W':d = 'S';break;
                    case 'S':d = 'E';break;
                }
            }
            if(sx < 0 || sy < 0 || sx > n || sy > m) {
                switch(d) {
                    case 'N':sy--;break;
                    case 'E':sx--;break;
                    case 'W':sx++;break;
                    case 'S':sy++;break;
                }
                if(pre[sx][sy] == 1)
                    continue;
                flag = 1;
                pre[sx][sy] = 1;
                break;
            }
        }
        if(!flag)
            printf("%d %d %c\n", sx, sy, d);
        else {
            printf("%d %d %c LOST\n", sx, sy, d);
        }
    }
    return 0;
}

台長: Morris
人氣(6,356) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][幾何] 191 - Intersection
此分類上一篇:[UVA] 12149 - Feynman

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文