728x90
SMALL

using System;

public class Solution {
    public int[] solution(string[] keyinput, int[] board) {
        int[] answer = new int[2];
        int updown = 0;
        int rightleft = 0;
        
        for(int i = 0;i<keyinput.Length;i++)
        {
            if(keyinput[i] == "left" && rightleft > -1*board[0]/2)
            {
                answer[0] -= 1;
                rightleft--;
            }
            else if(keyinput[i] == "right" && rightleft < board[0]/2)
            {
                answer[0] += 1;
                rightleft++;
            }
            else if(keyinput[i] == "up" && updown < board[1]/2)
            {
                answer[1] += 1;
                updown++;
            }
            else if(keyinput[i] == "down" && updown > -1 * board[1]/2)
            {
                answer[1] -= 1;
                updown--;
            }
        }
        
        return answer;
    }
}

 

 

자바

 

class Solution {
    public int[] solution(String[] keyinput, int[] board) {
        int numRow = board[1];
        int numCol = board[0];
        int r = 0;
        int c = 0;
        for (String key : keyinput) 
        {
            switch (key.charAt(0)) 
            {
                case 'l':
                    c = Math.max(-numCol / 2, c - 1);
                    break;
                case 'r':
                    c = Math.min(numCol / 2, c + 1);
                    break;
                case 'd':
                    r = Math.max(-numRow / 2, r - 1);
                    break;
                case 'u':
                    r = Math.min(numRow / 2, r + 1);
                    break;
            }
            
            System.out.println(r + " " + c);
        }
        
        return new int[]{c, r};
    }
}

 

 

class Solution {
    public int[] solution(String[] keyinput, int[] board) {
        int[] now = {0, 0};
        
        for (int i = 0; i < keyinput.length; i++)
        {
            if(keyinput[i].equals("left")) 
              now[0] -= now[0]>-(board[0]/2)?1:0;
            else if(keyinput[i].equals("right"))
              now[0] += now[0]<(board[0]/2)?1:0;
            else if(keyinput[i].equals("down"))
              now[1] -= now[1]>-(board[1]/2)?1:0;
            else if(keyinput[i].equals("up")) 
              now[1] += now[1]<(board[1]/2)?1:0;
        }
        
        return now;
    }
}
728x90

설정

트랙백

댓글