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

Updated:

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

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

๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๋ฅผ 4๊ฐ€์ง€ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์›€์ง์ด๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • U: ์œ„์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ
  • D: ์•„๋ž˜์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ
  • R: ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ
  • L: ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ ๊ฐ€๊ธฐ

์บ๋ฆญํ„ฐ๋Š” ์ขŒํ‘œํ‰๋ฉด์˜ (0, 0) ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ขŒํ‘œํ‰๋ฉด์˜ ๊ฒฝ๊ณ„๋Š” ์™ผ์ชฝ ์œ„(-5, 5), ์™ผ์ชฝ ์•„๋ž˜(-5, -5), ์˜ค๋ฅธ์ชฝ ์œ„(5, 5), ์˜ค๋ฅธ์ชฝ ์•„๋ž˜(5, -5)๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ULURRDLLU๋กœ ๋ช…๋ นํ–ˆ๋‹ค๋ฉด

  • 1๋ฒˆ ๋ช…๋ น์–ด๋ถ€ํ„ฐ 7๋ฒˆ ๋ช…๋ น์–ด๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์›€์ง์ž…๋‹ˆ๋‹ค.

  • 8๋ฒˆ ๋ช…๋ น์–ด๋ถ€ํ„ฐ 9๋ฒˆ ๋ช…๋ น์–ด๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์›€์ง์ž…๋‹ˆ๋‹ค.

์ด๋•Œ, ์šฐ๋ฆฌ๋Š” ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๊ฐ€ ์ง€๋‚˜๊ฐ„ ๊ธธ ์ค‘ ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๊ฐ€ ์›€์ง์ธ ๊ธธ์ด๋Š” 9์ด์ง€๋งŒ, ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋Š” 7์ด ๋ฉ๋‹ˆ๋‹ค.

(8, 9๋ฒˆ ๋ช…๋ น์–ด์—์„œ ์›€์ง์ธ ๊ธธ์€ 2, 3๋ฒˆ ๋ช…๋ น์–ด์—์„œ ์ด๋ฏธ ๊ฑฐ์ณ ๊ฐ„ ๊ธธ์ž…๋‹ˆ๋‹ค)

๋‹จ, ์ขŒํ‘œํ‰๋ฉด์˜ ๊ฒฝ๊ณ„๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๋ช…๋ น์–ด๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, LULLLLLLU๋กœ ๋ช…๋ นํ–ˆ๋‹ค๋ฉด

  • 1๋ฒˆ ๋ช…๋ น์–ด๋ถ€ํ„ฐ 6๋ฒˆ ๋ช…๋ น์–ด๋Œ€๋กœ ์›€์ง์ธ ํ›„, 7, 8๋ฒˆ ๋ช…๋ น์–ด๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ 9๋ฒˆ ๋ช…๋ น์–ด๋Œ€๋กœ ์›€์ง์ž…๋‹ˆ๋‹ค.

์ด๋•Œ ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋Š” 7์ด ๋ฉ๋‹ˆ๋‹ค.

๋ช…๋ น์–ด๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜ dirs๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ๊ฐ€ ์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜์—ฌ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

์ฒ˜์Œ ์ ‘๊ทผํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€ ๋„์ฐฉ ์ง€์ ์˜ ์ขŒํ‘œ๊ฐ€ ์˜ˆ์ „์— ๋ฐฉ๋ฌธ ํ–ˆ๋˜ ์ขŒํ‘œ ์ค‘์— ์žˆ๋‹ค๋ฉด ๊ทธ๋Ÿฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ ๋‘ ๋ฒˆ์งธ ๋ถ€ํ„ฐ

์ฒ˜์Œ ๊ฑธ์–ด๋ณธ ๊ธธ์ด ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.. ๋ฌผ๋ก  ์•„์ฃผ์•„์ฃผ ๋‹จํŽธ์ ์ด๊ณ  ๋ฉ์ฒญํ•œ ์ƒ๊ฐ์ด์—ˆ๋‹ค.

์ €๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ์ฝ”๋”ฉ์„ ํ•˜๋‹ˆ ์ฝ”๋“œ๋„ ๋”๋Ÿฌ์›Œ์ง€๊ณ  ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋„ ๊ณตํ†ต์ ์œผ๋กœ ํ•˜๋Š”๊ฒŒ ์•„๋‹Œ ๊ทธ๋•Œ๊ทธ๋•Œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋Š๋‚Œ์ด๋ผ์„œ

์‹น ์ง€์šฐ๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๋‹ค..

์—ฌ๋Ÿฌ ๊ฒฝ์šฐ๋ฅผ ๊ทธ๋ ค ์†์œผ๋กœ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๋‹ˆ ๊ทœ์น™์ด ๋ณด์˜€๋‹ค.

๋ฐ”๋กœ ์ƒ๊ฐ ๋‚œ ์‚ฌ๋žŒ๋„ ์žˆ๊ฒ ์ง€๋งŒ ๋‚˜๋Š” ์•„๋‹ˆ์˜€๋‹ค.. ์–ด์จŒ๋“  ๊ทœ์น™์€ ์ง€๊ธˆ ์ด๋™ ํ•œ ์‹œ์ž‘์ ์˜ ์ขŒํ‘œ, ๋„์ฐฉ์ ์˜ ์ขŒํ‘œ๊ฐ€

์ด์ „์— ์ด๋™ํ•œ ์‹œ์ž‘์ ์˜ ์ขŒํ‘œ, ๋„์ฐฉ์ ์˜ ์ขŒํ‘œ์— ๊ฒน์น˜๋Š”๊ฒŒ ์žˆ๋‹ค๋ฉด ์ด๋ฏธ ๊ฑธ์–ด๋ดค๋˜ ๊ธธ์ด๋‹ค!

ย  ์‹œ์ž‘์  ๋„์ฐฉ์ 
R (0, 0) (1, 0)
U (1, 0) (1, 1)
D (1, 1) (1, 0)
D (1, 0) (1, -1)
L (1, -1) (0, -1)
U (0, -1) (0, 0)
U (0, 0) (0, 1)
R (0, 1) (1, 1)
D (1, 1) (1, 0)

์œ„์˜ ํ‘œ๋ฅผ ๋ณด๋ฉด ์„ธ ๋ฒˆ์งธ ์ด๋™์ธ โ€˜Dโ€™ ๋Š” ๋‘ ๋ฒˆ์งธ ์ด๋™์ธ โ€˜Uโ€™ ์™€ ์‹œ์ž‘์ , ๋„์ฐฉ์ ์ด ๋ฐ˜๋Œ€๋กœ ๊ฒน์นœ๋‹ค.

๊ทธ ๋ง์€ ์˜ฌ๋ผ๊ฐ”๋˜ ๊ธธ์„ ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค์™”๋‹ค๊ณ  ํŒ๋‹จ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ ์ด๋™์ธ โ€˜Dโ€™ ๋ฅผ ๋ณด๋ฉด ๋‘ ๋ฒˆ์งธ ์ด๋™์ธ โ€˜Uโ€™ ์™€ ์‹œ์ž‘์ , ๋„์ฐฉ์ ์ด ๋ฐ˜๋Œ€๋กœ ๊ฒน์นœ๋‹ค.

์ด ๋ง๋„ ๋Œ๊ณ ๋Œ์•„ ์˜ฌ๋ผ๊ฐ”๋˜ ๊ธธ์„ ๋‹ค์‹œ ๋‚ด๋ ค์™”๋‹ค๊ณ  ํŒ๋‹จ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  x, y ์ขŒํ‘œ๊ฐ€ -5 ์ด์ƒ 5 ์ดํ•˜ ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ด๋™์ด ์œ ํšจํ–ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋จผ์ € ๋„์ฐฉ์  ์ขŒํ‘œ๋ฅผ ๊ณ„์‚ฐ ํ•˜๊ณ  ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒฝ์šฐ์—๋งŒ

๊ฐฑ์‹ ์„ ํ•ด์ฃผ์—ˆ๋‹ค.

def check(i, start, end):

	for j in range(i):
		if start[i] == start[j] and end[i] == end[j]:
			return False

		if start[i] == end[j] and end[i] == start[j]:
			return False
	else:
		return True

def solution(dirs):
	x = 0
	y = 1
	answer = 1

	start = [[0 for i in range(2)] for j in range(len(dirs))]
	end = [[0 for i in range(2)] for j in range(len(dirs))]

	if dirs[0] == 'U':
		end[0][y] += 1
	elif dirs[0] == 'D':
		end[0][y] -= 1
	elif dirs[0] == 'R':
		end[0][x] += 1
	elif dirs[0] == 'L':
		end[0][x] -= 1

	for i in range(1, len(dirs)):
		start[i] = end[i-1]

		if dirs[i] == 'U':
			end[i][x] = start[i][x]
			if -5 <= start[i][y] + 1 <= 5:
				end[i][y] = start[i][y] + 1
			else:
				end[i][y] = start[i][y]
				continue

		elif dirs[i] == 'D':
			end[i][x] = start[i][x]
			if -5 <= start[i][y] - 1 <= 5:
				end[i][y] = start[i][y] - 1
			else:
				end[i][y] = start[i][y]
				continue

		elif dirs[i] == 'R':
			end[i][y] = start[i][y]
			if -5 <= start[i][x] + 1 <= 5:
				end[i][x] = start[i][x] + 1
			else:
				end[i][x] = start[i][x]
				continue
			
		elif dirs[i] == 'L':
			end[i][y] = start[i][y]
			if -5 <= start[i][x] - 1 <= 5:
				end[i][x] = start[i][x] - 1
			else:
				end[i][x] = start[i][x]
				continue

		if check(i, start, end):
			answer += 1
	return answer

dirs = "RUDDLUURD"
print(solution(dirs))

๊ฐœ์ธ์ ์œผ๋กœ ์ข€ ๋‚œํ•ดํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋Š”๋ฐ ํ’€๊ณ ๋‚˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฒŒ ๊ฐ€์žฅ ์ข‹์€ ๋ถ€๋ถ„ ๊ฐ™๋‹ค.

๋‚˜๋Š” ์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ ๋‹ค๋ฅธ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋‚˜ ๋ณด๋ฉด์„œ ๋ฐฐ์›Œ๊ฐ€๋Š”๊ฒŒ ์ •๋ง ๋งŽ๋‹ค.


Categories:

Updated:

Leave a comment