13 Sun
TIL
์คํ ๋งํ์ ์ปดํ์ผ๋ฌ
River Crossing : ์ต์ข
์ฝ๋

#
# data : 2020.12.02
# author : sangmandu at Dankook Univ.
# program : river crossing
# language : python
#
#
# H : Human, W : Wolf, S : Sheep, C : Cabbage
# 1 ... W eat S when there are only W and S
# 2 ... S eat C when there are only S and C
# 3 ... boat can be accommodate in up to 2 elements and Human essential
# 4 ... objective is that all element moves left to right
#
#
# two lists that left and right
# prior : keep from generating duplicate
# dir : direction left to right or vice versa
# record : the procedure to success(or failure)
#
# failure 1) left check issue : collision with rule 1 or 2
# 2) left check issue : collision with rule 1 or 2
# 3) duplicate case : prevent that infinite loop
#
# initialization
_left = ['H', 'W', 'S', 'C']
_prior = ''
_right = []
_dir = '>'
_record = []
# check rule 1 and 2
# when breaking rule then True else False
def checkIssue(rand):
if len(rand) == 2 and 'S' in rand:
if 'W' in rand or 'C' in rand:
return True
return False
stack = [(_left, _prior, _right, _dir, _record)]
while stack:
left, prior, right, dir, record = stack.pop(0)
print()
print("selected element : ", left, prior, right, dir)
print("and remained stack : ", stack)
if not left:
_record.append(record)
continue
if dir == ">":
for ele in left:
if ele == 'H':
continue
print(f"ele is {ele}")
l, p, r, rec = left[:], prior[:], right[:], record[:]
if ele == prior:
print("ele == prior")
continue
l.remove(ele)
l.remove('H')
if checkIssue(l):
print("left check issue")
continue
r.append(ele)
r.append('H')
if checkIssue(r):
print("right check issue")
continue
memo = f"{ele} and human move from left : {left} to right : {right}"
if memo in rec:
print("duplicate issue")
continue
rec.append(memo)
stack.append((l, ele, r, '<', rec))
print(f"stack append {(l, ele, r, '<', rec)}")
else: # dir == "<"
for ele in right:
if ele == 'H':
continue
print(f"ele is {ele}")
l, p, r, rec = left[:], prior[:], right[:], record[:]
if ele == prior:
print("ele == prior")
continue
r.remove(ele)
r.remove('H')
if checkIssue(r):
print("right check issue")
continue
l.append(ele)
l.append('H')
if checkIssue(l):
print("left check issue")
continue
memo = f"{ele} and human move to left : {left} from right : {right} "
if memo in rec:
print("duplicate issue")
continue
rec.append(memo)
stack.append((l, ele, r, '>', rec))
print(f"stack append {(l, ele, r, '>', rec)}")
record.append(f"only human moves to left : {left} from right : {right} ")
right.remove('H')
left.append('H')
if checkIssue(right):
print("right check issue when human only on board")
continue
stack.append((left, '', right, '>', record))
print(f"stack append that only human {(l, '', r, '>', rec)}")
print()
for idx, record in enumerate(_record, 1):
print(f"#{idx} success case")
for rec in record:
print(rec)
print()
์ค๊ฐ ์ฝ๋์๋ ๋น์ทํ๋ฉด์ ์ข ๋ค๋ฅด๋ค. (ํ๋ฒ ๊ฐ์ ์์ด๊ฐ์ง๊ณ ...) ์ฌ๊ท ํจ์๋ณด๋ค๋ while / stack ๋ฐฉ์์ด ๋ณ์ ๊ด๋ฆฌ๊ฐ ํธํ ๊ฒ ๊ฐ๋ค. ํจ์ ๋ด์์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋ณ์๋ l = left[:] ์ ๊ฐ์ด ๊น์ ๋ณต์ฌ๋ฅผ ํ๋ ค๊ณ ํด๋ ์ ์๋๋ค. ๊ฒฐ๊ตญ copy.deepcopy๋ฅผ ์จ์ผ ๋๋๋ฐ, ๊ตณ์ด ์ด๋ ๊ฒ? ๋ผ๋ ์๊ฐ์ด ๋๋ฌด ๋ง์ด ๋ ๋ค. ํจ์ ๋ด์์๋ ์ฌ๊ทํจ์ ๋ํ ๋์ผํ ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๊ทธ๋ฐ๊ฒ ์๋๊น ๋ผ๋ ์ถ์ธก์ ์์ง๋ง ํ์คํ ์ด์ ๋ ๋ชจ๋ฅด๊ฒ ๋ค.
100์ค ์กฐ๊ธ ๋๊ฒ ์์ฑ๋ ์ฝ๋. ์ข ๋ ํจ์ํํ๊ณ ํจ์ถํ์ฌ ์ฝ๋๋ฅผ ์ค์ผ ์ ์๊ฒ ์ง๋ง ์ ์ด์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์์์ ๊ทธ๋ด ํ์๊ฐ ์๋ค. ์ข ๋ ์ง๊ด์ ์ด์ด์ ์ค๋ช ํ๊ฑฐ๋ ๋ณผ ๋๋ ์ข์ ์ ์๋ค. ์กฐ๊ธ ๋ ์์ ํ์๋ฉด prior ๋ณ์๋ฅผ ์์ ๊ณ duplicate case์ ์ถ๊ฐํ ์ ์๊ฒ ๋ค.
๊ฐ๋จํ ์๊ณ ๋ฆฌ์ฆ์ธ๋ฐ ์๊ฐ๋ณด๋ค ๊ณ ๋ฏผ์ด ๊ธธ์๋ค. ์ฃผ๋ ํฌ์ธํธ๋ left to right์ธ์ง ๋๋ ๋ฐ๋์ธ์ง๋ฅผ ํ์ธํ๊ณ ์ด์ ๋ํด ๊ฐ left(or right)์์ ๋ค์ ๋ฐ๋ํธ์ผ๋ก ๊ฑด๋๊ฐ ์์๋ฅผ ๊ณ ๋ฅธ๋ค. ์ด ๋ prior๋ ์ด์ ์ ๊ฐ์ ธ์จ ์์๋ฅผ ๊ธฐ์ตํ์ฌ ๋ค์ ๊ณ ๋ฅด์ง ์๋๋ก ํ๋ค. checkIssue๋ ์ ๋ฌ๋ ๋ฆฌ์คํธ์์ rule์ด ์ง์ผ์ง๋์ง ํ์ธํ์ฌ ์ง์ผ์ง์ง ์์์ ๊ฒฝ์ฐ True๋ฅผ ๋ฐํํ๋ค. (๋ฃฐ ํ์ธ์ ์ํด if๋ฌธ ์ธ ๋ not์ ์ฐ๊ธฐ๊ฐ ์ง์ ๋ถ ํ๊ฒ ๊ฐ์ True๋ฅผ ๋ฐํํ๋๋ก ํ๋ค...)
๊ฐ case๋ฅผ BFS ๋ฐฉ์์ผ๋ก ์ ๊ทผํ๋ฉฐ ๊ฐ ์ผ์ด์ค๋ง๋ค ์คํจํ ๊ฒฝ์ฐ ์คํจ ์ด์ ๊ฐ ์ถ๋ ฅ๋๋ค. ์คํจํ์ ๊ฒฝ์ฐ์๋ ๊ทธ ๊ณผ์ ์ ์ผ๋ถ๋ก ์ถ๋ ฅํ์ง ์๋๋ก ํ๋ค. (์๊ถ๊ธํด์?) ๋ํ ์ฑ๊ณตํ ๊ฒฝ์ฐ๋ฅผ ๋ง์ง๋ง์ ์ ๋ฆฌํด์ ๋ค์ ์ถ๋ ฅํ๋๋ก ํ๋ค. ์ค์ ๋ก๋ 2๊ฐ์ง์ ๊ฒฝ์ฐ์ ์๊ฐ ์กด์ฌํ๋๋ฐ, ํ๋ก๊ทธ๋จ ๋ํ ๋ ๊ฐ์ง์ ์ฑ๊ณต ์ผ์ด์ค๋ฅผ ์ถ๋ ฅํ๋ค. ๊ฒฐ๊ณผ 150์ค ์ ๋ ์ถ๋ ฅ๋๋ค.

BFS๋ฐฉ์์ผ๋ก ์ถ๋ ฅํ๋ ๋ชจ์ต. ๊ฐ ์คํจ ์ผ์ด์ค์ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.

๋ค์์ ์ฑ๊ณต์ ์ผ๋ก ์ถ๋ ฅ๋๋ ๋ชจ์ต.

Last updated
Was this helpful?