Notice
Recent Posts
Recent Comments
Link
관리 메뉴

윤일무이

[JavaScript] 구름톤 챌린지 2주차 코딩테스트 : GameJam (못 품) 본문

⚙️ 코딩테스트

[JavaScript] 구름톤 챌린지 2주차 코딩테스트 : GameJam (못 품)

썸머몽 2023. 8. 31. 14:29
728x90

📌  문제

📌  입력

📌  출력 및 예제


📌  풀이

알아야 하는 것

  •  

구할 것

  • 정해 코드를 참고해서 다시 풀어보자
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const directions = {
	"U": [-1, 0],
	"D": [1, 0],
	"L": [0, -1],
	"R": [0, 1],
};

let N;
let goormPos, playerPos;
let goormVisited, playerVisited;
let board = [];

function set_Pos(a) {
	if (a === -1) return N-1;
	if (a === N) return 0;
	return a;
}

function move(pos, visited, score, board) {
	let [x, y] = pos;
	visited[x][y] = true;
	let flag = true;

	while (flag) {
		let command = board[x][y];
		let distance = parseInt(command.slice(0, -1));
		let direction = command.slice(-1);
		
		for (let i = 0 ; i < distance; i++){
			x += directions[direction][0];
			y += directions[direction][1];
			x = set_Pos(x);
			y = set_Pos(y);
			if (!visited[x][y]){
				visited[x][y] = true;
				score += 1;
			}
			else{
				flag = false;
				break;
			}
		}
	}
	return score;
}

let input = []
rl.on('line', (line) => {
	input.push(line);
	N = Number(input[0]);
	if (input.length === N + 3){
		rl.close();
	}
})

rl.on('close', () => {
	
	goormPos = input[1].split(' ').map(num => Number(num) - 1);
	goormVisited = Array.from(Array(N), () => new Array(N).fill(false));
	playerPos = input[2].split(' ').map(num => Number(num) - 1);
	playerVisited = Array.from(Array(N), () => new Array(N).fill(false));
	for (let i = 3 ; i < N + 3 ; i++){
		board.push(input[i].split(' '));
	}
	
	let goormScore = move(goormPos, goormVisited, 1, board);
	let playerScore = move(playerPos, playerVisited, 1, board);

	if (goormScore > playerScore) {
		console.log("goorm " + goormScore );
	} else if (goormScore < playerScore) {
		console.log("player " + playerScore);
	}
	process.exit();
});

 

 

728x90