UVA 118 Java

import java.util.*;
import static java.lang.System.*;

public class main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int w = sc.nextInt(), h = sc.nextInt();
    boolean drop_loc[][] = new boolean[51][51];
    while(sc.hasNext())
    {
    	int x = sc.nextInt(), y = sc.nextInt();
    	char ori = sc.next().charAt(0);
    	String cmd = sc.next();
    	boolean drop = false;
    	for(int i = 0; i < cmd.length(); i++)
    	{
    		char c = cmd.charAt(i);
    		int tempx = x;
    		int tempy = y;
    		if(c == 'L')
    		{
    			if(ori == 'N')
    				ori = 'W';
    			else if(ori == 'E')
    				ori = 'N';
    			else if(ori == 'S')
    				ori = 'E';
    			else if(ori == 'W')
    				ori = 'S';
    		}
    		else if(c == 'R')
    		{
    			if(ori == 'N')
    				ori = 'E';
    			else if(ori == 'E')
    				ori = 'S';
    			else if(ori == 'S')
    				ori = 'W';
    			else if(ori == 'W')
    				ori = 'N';
    		}
    		else if(c == 'F')
    		{
    			if(ori == 'N')
    				tempy++;
    			else if(ori == 'E')
    				tempx++;
    			else if(ori == 'S')
    				tempy--;
    			else if(ori == 'W')
    				tempx--;
    			if(tempx < 0 || tempy < 0 || tempx > w || tempy > h)
    			{
    					if(!drop_loc[x][y])
    					{
    						drop_loc[x][y] = true;
    						drop = true;
    						break;
    					}
    			}
    			else
    			{
    				x = tempx;
    				y = tempy;
    			}
    		}
    	}
    	if(drop)
    	{
    		System.out.printf("%d %d %c LOST\r\n", x, y, ori);
    	}
    	else
    		System.out.printf("%d %d %c\r\n", x, y, ori);
    }
  }
};

留言

這個網誌中的熱門文章

C# 井字遊戲