습/코테

[구현] 이코 실전문제

이준늬 2023. 3. 3. 23:48

왕실의 나이트

 

입력 

현재 나이트가 위치한 곳의 좌표 _ 8*8 좌표 평면: 행(1~8), 열(a~h) ex) a1

 

출력

나이트가 이동할 수 있는 경우의 수

1. 수평으로 두 칸 이동 후 수직으로 한 칸 이동

2. 수직으로 두 칸 이동 후 수평으로 한 칸 이동

3. 1, 2 두 가지 경우로 이동 가능하며, 좌표 밖으로는 나갈 수 없음.

 

이동할 수 있는 방향의 좌표들을 묶어 모든 경우의 수 반복

> python

더보기
더보기
import sys

input_data = input()

column = int(ord(input_data[0])) - int(ord('a')) + 1
row = int(input_data[1])

steps = [(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2)]

result = 0
for step in steps:
    
    next_column = column + step[0]
    next_row = row + step[1]
    
    if next_row >= 1 and next_row <= 8 and next_column >= 1 and next_column <= 8:
        result += 1

print(result)

> java

더보기
더보기
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String location = sc.nextLine();

        int column = location.charAt(0) - 'a' + 1;
        int row = location.charAt(1) - '0';

        List<List<Integer>> steps = List.of(List.of(-2, -1), List.of(-2, 1),
                List.of(-1, -2), List.of(-1, 2),
                List.of(1, -2), List.of(1, 2),
                List.of(2, -1), List.of(2, 1));

        int result = 0;
        for (List<Integer> step : steps) {
            int nextColumn = column + step.get(0);
            int nextRow = row + step.get(1);

            if (nextColumn >= 1 && nextRow >= 1 && nextColumn <= 8 && nextRow <= 8) {
                result++;
            }
        }
        System.out.println(result);
    }
}

 


게임 개발

 

입력

N,  M(3 ≤ N_세로, M_가로 ≤ 50) 공백으로 구분

A, B, d (게임 캐릭터가 있는 칸의 좌표 (A_세로, B_가로)와 바라보는 방향 d( 0: 북, 1: 동, 2: 남, 3: 서 ) 공백으로 구분

N * M 크기의 맵의 정보( 0: 육지, 1: 바다 )

 

출력

이동을 마친 후 캐릭터가 방문한 수의 칸 출력 ( 모든 방면으로 이동할 수 없을 때까지 왼쪽 회전 후 전진 or 왼쪽 회전 반복)

 

바라보는 방향이 상하좌우 움직임과 같음. 인덱스를 활용해 구현

> python

더보기
더보기
import sys

n, m = map(int, input().split())
x, y, direction = map(int, input().split())

visited_map = [[0] * m for _ in range(n)]
visited_map[x][y] = 1

game_map = []
for i in range(n):
    game_map.append(list(map(int, input().split())))

move_x = [-1, 0, 1, 0]
move_y = [0, 1, 0, -1]

def turn_left():
    global direction
    direction -= 1
    
    if direction == -1:
        direction = 3
        
        
count = 1
turn_time = 0

while True:
    
    turn_left()
    next_x = x + move_x[direction]
    next_y = y + move_y[direction]
    
    if game_map[next_x][next_y] == 0 and visited_map[next_x][next_y] == 0:
        visited_map[next_x][next_y] = 1
        x = next_x
        y = next_y
        
        count += 1
        turn_time = 0
        continue
    
    else:
        turn_time += 1
        
    if turn_time == 4:
        next_x = x - move_x[direction]
        next_y = y - move_y[direction]
        
        if game_map[next_x][next_y] == 0:
            x = next_x
            y = next_y
        
        else:
            break
        
        turn_time = 0
        
print(count)

>java

 


이것이 코딩 테스트다 _ 나동빈

정리허긔

' > 코테' 카테고리의 다른 글

[2023.03.13] 6 주차  (0) 2023.03.13
[Greedy] 백준 풀긔  (0) 2023.03.03
[Greedy] 이코 기출문제  (0) 2023.03.03
[Greedy] 이코 실전문제  (0) 2023.03.03