지구정복

[구현] 이코테 - 상하좌우 본문

데이터 엔지니어링 정복/Algorithm

[구현] 이코테 - 상하좌우

eeaarrtthh 2023. 4. 2. 22:42
728x90
반응형
SMALL

 

switch문과 if문을 을 써서 풀어봤는데 그냥 정답대로 푸는게 좋은 것 같다.

 

상하좌우로 움직일 지도의 크기 N이 주어지고

어느 방향으로 움직일지 입력받는다.

 

R, L, U, D 각각 오른쪽, 왼쪽, 위, 아래로 한 칸씩 움직이게 되는데 이때 지도의 크기(1~N)보다 작거나 크면

지도를 벗어나는 것이므로 무효처리해야 한다.

 

 

-자바

package implementation;

public class ThisCote_4_1 {

    static int N, x, y;

    public static void main(String[] args) {
        N = 5;
        char[] roots = {'R', 'R', 'R', 'U', 'D', 'D'};
        x = 1; y = 1;

        for( char s : roots ) {
            move(s);
        }

        System.out.println( x + " " + y );
    }

    public static void move(char c) {
        int nx = 0;
        int ny = 0;

        switch(c) {
            case 'R': {
                ny = 1;
                if( checkMove(x+nx, y+ny) ) {
                    break;
                }
                y = y+ny;
                break;
            }
            case 'L': {
                ny = -1;
                if( checkMove(x+nx, y+ny)) {
                    break;
                }
                y = y+ny;
                break;
            }
            case 'U': {
                nx = -1;
                if( checkMove(x+nx, y+ny)) {
                    break;
                }
                x = x+nx;
                break;
            }
            case 'D': {
                nx = 1;
                if( checkMove(x+nx, y+ny)) {
                    break;
                }
                x = x+nx;
                break;
            }
        }
    }

    public static boolean checkMove(int xx, int yy) {
        if( xx < 1 || yy > N ) {
            return true;
        }
        return false;
    }

}

 

-자바

package implementation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class ThisCote_4_1_answer {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt( br.readLine() );
        StringTokenizer st = new StringTokenizer( br.readLine() );

        int x = 1;
        int y = 1;

        //L, R, U, D에 따른 이동 방향
        int[] dx = {0, 0, -1, 1};
        int[] dy = {-1, 1, 0, 0};
        String[] moveTypes = {"L", "R", "U", "D"};

        while( st.hasMoreTokens() ) {
            int nx = 0;
            int ny = 0;
            String s = st.nextToken();


            for( int i=0; i<moveTypes.length; i++ ) {
                if( s.equals(moveTypes[i]) ) {
                    nx = x + dx[i];
                    ny = y + dy[i];
                }
            }

            if( nx < 1 || nx > N || ny < 1 || ny > N ) {
                continue;
            }
            x = nx;
            y = ny;
        }

        System.out.println(x+" "+y);
    }
}

 

-파이썬

N = int( input() )
x, y = 1, 1
plans = input().split()

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
move_types = ['L', 'R', 'U', 'D']

for plan in plans:

    for i in range( len(move_types) ):
        if plan == move_types[i]:
            nx = x+dx[i]
            ny = y+dy[i]

    if nx < 1 or nx > N or ny < 1 or ny > N:
        continue

    x, y = nx, ny

print(x, y)
728x90
반응형
LIST
Comments