[Programmers] ๋ฐฉ๋ฌธ ๊ธธ์ด 2

Updated:

๋ฐฉ๋ฌธ ๊ธธ์ด

๋ฐฉ๋ฌธย ๊ธธ์ด ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์˜ˆ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์€ ๊ธฐ๋ถ„์ด ๋“ค์–ด ๊ฒ€์ƒ‰์„ ํ•ด๋ณด์•˜๋”๋‹ˆ

[Programmers] ๋ฐฉ๋ฌธ ๊ธธ์ด ์ž‘๋…„ 6์›” 19์ผ์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ํ’€๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ์˜€๋Š”๋ฐ ๊ทธ๋•Œ ์ฝ”๋“œ์™€ ๋น„๊ต๋ฅผ ํ•˜๋‹ˆ ๋งŽ์ด ๋‚˜์•„์ง„ ๊ฒƒ ๊ฐ™๋‹ค..

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ 3์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค.

๊ฐ ์œ„์น˜๋งˆ๋‹ค ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ๊ฐ€๋Š” ๊ธธ์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

๋งŒ์•ฝ [5, 5] ์—์„œ [4, 5] ๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค๋ฉด [5, 5] ์˜ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ธธ๊ณผ [4, 5] ์˜ ๋‚ด๋ ค๊ฐ€๋Š” ๊ธธ์„ ์‚ฌ์šฉ ํ–ˆ๋‹ค๊ณ  ์ฒดํฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ฃผ์–ด์ง„ ๋ช…๋ น๋Œ€๋กœ ์ขŒํ‘œ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ธธ์„ ์ฒ˜์Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด์— ๋”ํ•˜๋ฉด ๋œ๋‹ค.


def solution(dirs):
	answer = 0
	road = [[[True, True, True, True] for _ in range(11)] for _ in range(11)]
	move = {
		'U': [-1, 0],
		'R': [0, 1],
		'D': [1, 0],
		'L': [0, -1],
	}
	head = {
		'U': [0, 2],
		'R': [1, 3],
		'D': [2, 0],
		'L': [3, 1],
	}

	cur_i, cur_j = [5, 5]
	for val in dirs:
		visit_i, visit_j = cur_i + move[val][0], cur_j + move[val][1]
		if 0 <= visit_i <= 10 and 0 <= visit_j <= 10:
			if road[cur_i][cur_j][head[val][0]] and road[visit_i][visit_j][head[val][1]]:
				answer += 1
				road[cur_i][cur_j][head[val][0]] = False
				road[visit_i][visit_j][head[val][1]] = False

			cur_i = visit_i
			cur_j = visit_j

	return answer



Categories:

Updated:

Leave a comment