UVA118




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


解法: 模擬題 打表紀錄


import java.io.IOException;
import java.util.Scanner;

public class UVA118 {

 public static void main(String[] args) throws IOException {

  Scanner sc = new Scanner(System.in);

  char direction[] = new char[] { 'E', 'N', 'W', 'S' };

  int move[][] = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };

  int x = sc.nextInt();
  int y = sc.nextInt();

  boolean rectangular[][] = new boolean[51][51];
  // 紀錄哪個位置會fall out

  while (sc.hasNext()) {

   boolean check = true;

   int it_x = sc.nextInt();
   int it_y = sc.nextInt();

   char dir = sc.next().charAt(0);

   sc.nextLine();// 吃掉next的換行

   String instruction = sc.nextLine();

   int temp = 0;

   for (int i = 0; i < 4; i++) {
    if (dir == direction[i])
     temp = i;
   }

   for (int i = 0; i < instruction.length() && check; i++) {
    char each_instruction = instruction.charAt(i);

    if (each_instruction == 'L') {

     temp += 5;
     temp = temp % 4;
    } else if (each_instruction == 'R') {

     temp += 3;
     temp = temp % 4;
    } else if (each_instruction == 'F') {

     it_x += move[temp][0];
     it_y += move[temp][1];

     if (it_x > x || it_y > y || it_x < 0 || it_y < 0) {

      it_x -= move[temp][0];
      it_y -= move[temp][1];

      if (rectangular[it_x][it_y] == false) {
       check = false;
       rectangular[it_x][it_y] = true;

       System.out.println(it_x + " " + it_y + " "
         + direction[temp] + " LOST");
      }

     }

    }

   }

   if (check)
    System.out.println(it_x + " " + it_y + " " + direction[temp]);

  }

  sc.close();

 }

}