ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • algorithm
    ๐Ÿ’ป ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์บ๋ฆญํ„ฐ์˜ ์ขŒํ‘œ
    2023. 9. 10. 22:03
    728x90

    ๐Ÿ“• ๋ฌธ์ œ ์„ค๋ช…

    ๋จธ์“ฑ์ด๋Š” RPG๊ฒŒ์ž„์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ์ž„์—๋Š” up, down, left, right ๋ฐฉํ–ฅํ‚ค๊ฐ€ ์žˆ์œผ๋ฉฐ ๊ฐ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์œ„, ์•„๋ž˜, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [0,0]์—์„œ up์„ ๋ˆ„๋ฅธ๋‹ค๋ฉด ์บ๋ฆญํ„ฐ์˜ ์ขŒํ‘œ๋Š” [0, 1], down์„ ๋ˆ„๋ฅธ๋‹ค๋ฉด [0, -1], left๋ฅผ ๋ˆ„๋ฅธ๋‹ค๋ฉด [-1, 0], right๋ฅผ ๋ˆ„๋ฅธ๋‹ค๋ฉด [1, 0]์ž…๋‹ˆ๋‹ค. ๋จธ์“ฑ์ด๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐฉํ–ฅํ‚ค์˜ ๋ฐฐ์—ด keyinput์™€ ๋งต์˜ ํฌ๊ธฐ board์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์บ๋ฆญํ„ฐ๋Š” ํ•ญ์ƒ [0,0]์—์„œ ์‹œ์ž‘ํ•  ๋•Œ ํ‚ค ์ž…๋ ฅ์ด ๋ชจ๋‘ ๋๋‚œ ๋’ค์— ์บ๋ฆญํ„ฐ์˜ ์ขŒํ‘œ [x, y]๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

    [0, 0]์€ board์˜ ์ • ์ค‘์•™์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด board์˜ ๊ฐ€๋กœ ํฌ๊ธฐ๊ฐ€ 9๋ผ๋ฉด ์บ๋ฆญํ„ฐ๋Š” ์™ผ์ชฝ์œผ๋กœ ์ตœ๋Œ€ [-4, 0]๊นŒ์ง€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ตœ๋Œ€ [4, 0]๊นŒ์ง€ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๐Ÿ“• ์ œํ•œ ์‚ฌํ•ญ

    • board์€ [๊ฐ€๋กœ ํฌ๊ธฐ, ์„ธ๋กœ ํฌ๊ธฐ] ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    • board์˜ ๊ฐ€๋กœ ํฌ๊ธฐ์™€ ์„ธ๋กœ ํฌ๊ธฐ๋Š” ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.
    • board์˜ ํฌ๊ธฐ๋ฅผ ๋ฒ—์–ด๋‚œ ๋ฐฉํ–ฅํ‚ค ์ž…๋ ฅ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.
    • 0 ≤ keyinput์˜ ๊ธธ์ด ≤ 50
    • 1 ≤ board[0] ≤ 99
    • 1 ≤ board[1] ≤ 99
    • keyinput์€ ํ•ญ์ƒ up, down, left, right๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

     

    ๐Ÿ“ ๋‹ต์•ˆ

    class Solution {
        public int[] solution(String[] keyinput, int[] board) {
            int[] answer = new int[2];
            int w = ((board[0]-1)/2);
            int h = ((board[1]-1)/2);
    
            for(String key:keyinput) {
                if(key.equals("left") || key.equals("right")) {
                    if( ( w > answer[0] && answer[0] > w * -1) ||
                        (w == answer[0] && key.equals("left")) || 
                        (w*-1 == answer[0] && key.equals("right"))
                      )
                    {
                        answer[0] += key.equals("right")? 1:-1;
                    }
                }
                else {
                    if( (h > answer[1] && answer[1] > h*-1 ) ||
                        (h == answer[1] && key.equals("down")) || 
                        (h*-1 == answer[1] && key.equals("up"))
                      ) {
                        answer[1] += key.equals("up")? 1:-1;                
                    }
                }
            }
            return answer;
        }
    }

    8๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ๊ฐ€๋กœ๋ง‰ํ˜€์„œ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๋‹ค๋ณด๋‹ˆ ๊ตฌ๊ตฌ์ ˆ์ ˆ ์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ ์™„์„ฑ!! ๐Ÿ

    ๐Ÿ~~

    ์–ด์จŒ๊ฑฐ๋‚˜ ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ–ˆ๊ธฐ์— ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

     

    ์šฐ์„  board์˜ ๊ฐ€๋กœ, ์„ธ๋กœ ์ œํ•œ ๊ฐ’์„ w์™€ h์— ๊ฐ๊ฐ ๋‹ด์•„์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  keyinput์˜ ์›์†Œ๋งŒํผ for๋ฌธ์„ ๋Œ๋ฉด์„œ left, right์ผ ๋•Œ๋Š”

    answer[0] ๊ฐ’์„ ์„ธํŒ…ํ•ด์•ผ ํ•˜๋‹ˆ answer[0]์ด ๋งต์˜ ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’ ์‚ฌ์ด์— ์œ„์น˜ํ•˜๋Š”์ง€, ์ตœ๋Œ€๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋‹ค๋ฉด left์ธ์ง€, ์ตœ์†Œ๊ฐ’๊ณผ ์ผ์น˜ํ•  ๋•Œ right์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ answer[0] ์›์†Œ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  up,down์ผ ๋• ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ answer[1] ๊ฐ’์„ ์„ธํŒ…ํ•ด์•ผ ํ•˜๋‹ˆ ๋งต์˜ ์ตœ์†Œ๊ฐ’, ์ตœ๋Œ€๊ฐ’ ์‚ฌ์ด์ธ์ง€, ์ตœ๋Œ€๊ฐ’๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด down์ธ์ง€, ์ตœ์†Œ๊ฐ’๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด up์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ ์›์†Œ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

    ๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ์กฐ๊ฑด์„ ๊ตฌ๊ตฌ์ ˆ์ ˆ ์จ๋‚ด๋ ค๊ฐˆ ํ•„์š” ์—†์ด ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด 4์ค„๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

     

    ๐Ÿ“ ๊ฐœ์„ ๋œ ๋‹ต์•ˆ

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

    ํฐ ํ‹€์€ ์œ„์— ์ŠคํŒŒ๊ฒŒํ‹ฐ ๋กœ์ง๊ณผ ๋‹ค๋ฅด์ง€ ์•Š์ง€๋งŒ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์— ๋”ฐ๋ผ ์ด๋ ‡๊ฒŒ ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    ํ˜„์—…์—์„œ๋Š” ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ ธ์„œ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ ๋“ค ํ•˜์ง€๋งŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด์—์„œ๋Š” ๋งŽ์ด ๋“ฑ์žฅํ•˜๋‹ˆ ์•Œ์•„๋‘๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

    ๐Ÿ’ก ์ฐธ๊ณ 

    • ๋ฌธ์ž์—ดA.equals(๋ฌธ์ž์—ดB) : ๋ฌธ์ž์—ดA์™€ ๋ฌธ์ž์—ดB๊ฐ€ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, ์ผ์น˜ํ•˜๋ฉด true ๋ถˆ์ผ์น˜ํ•˜๋ฉด false๋ฅผ ๋ฆฌํ„ด
    728x90
Designed by Tistory.