2. POSTECH Computer Algorithm Team
1. NIM Game
2. Solution of NIM Game
3. Grundy Number
4. Addition of NIM Game
Index
Index
3. POSTECH Computer Algorithm Team
- 베스킨 라빈스 31 게임이 있다고 하자.
- Player A,B가 번갈아가면서 원래 보다 1 or 2 or 3 큰수를 부르고, 31을 부르는 사람이 지는 게임
- 베스킨 라빈스 31 게임의 필승전략은 아주 잘 알려져 있다.
- 베스킨 라빈스 n 게임은 어떨까?
- n이 4k+1이면 후수필승, 아니면 선수필승.
- 왜?
NIM Game
Nim Game
4. POSTECH Computer Algorithm Team
- 베스킨 라빈스 31 같은 게임을 NIM Game이라고 부른다.
- NIM Game: A two-player sequential game of perfect information satisfying the ending condition and
the normal play condition
- A two-player sequential game: 두 사람이 번갈아 가면서 진행함
- Perfect information: 모든 Player는 게임 규칙을 완벽하게 숙지함
- Ending condition: 이 게임은 반드시 끝난다. 무승부는 없고, 게임이 무한히 지속되지 않는다.
- Normal play condition: 모든 Player는 자신의 승리를 위해 최선을 다한다.
- 우리가 흔히 아는 체스, 바둑등은 NIM Game이라 부르기 힘들다.
NIM Game
Nim Game
5. POSTECH Computer Algorithm Team
- 어떤 NIM Game이 주어져 있다고 가정해보자.
- 이 NIM Game은 선수필승인가? 후수필승인가? 딱히 정해지지 않는가?
- 필승전략이 존재하는가?
NIM Game
NIM Game
6. POSTECH Computer Algorithm Team
- NIM Game의 분석에는 Automata,혹은 State-based Model을 이용한다.
Solution of NIM Game
Solution of NIM Game
1
2
3
4
5
6
7
7. POSTECH Computer Algorithm Team
- A two-player sequential game: 어떤 State를 A가 Play하고 있다면, 그 다음 State들은 B가 Play한다.
- Perfect information: NIM Game을 푸는데 재귀적 접근이 가능하다.
- Ending condition: 이 Automata는 DAG 구조를 이룬다.
- Normal play condition: 이 Automata를 이용하여 구해야 할것이 명확해진다.
Solution of NIM Game
Solution of NIM Game
8. POSTECH Computer Algorithm Team
- Automata as NIM Game version
Solution of NIM Game
Solution of NIM Game
1
2
3
4
5
6
7
9. POSTECH Computer Algorithm Team
- func(state): 어떤 state로 부터 시작할때 내가 승리하는가?
- 만약 func(nextState)가 Lose라고 가정하자. Two-player sequential game이므로 이 nextState는 상대방이 Play
한다.
- 상대방이 지는 경기는 내가 이기는 경기다. 그러므로, 이경우 func(state)는 Win이다.
- 만약 모든 func(nextState)가 Win이라 가정하자. 항상 상대방이 이기므로 Lose다.
- 만약 nextState가 없다고 가정하자. 그러면 그냥 Lose.
Solution of NIM Game
Solution of NIM Game
11. POSTECH Computer Algorithm Team
- NIM Game의 또다른 풀이 방법
- 만약 state가 endstate라면(nextstate가 없다면), G(state) = 0
- nextstate가 있다면, Grundy Number를 다음과 같이 정의한다.
- min( {0,1,2,...} – {G(nextstate)} )
- 즉, G(nextstate)중 제일 처음 안나타는 수가 Grundy Number가 된다.
- 예: G(nextstate)가 각각 0,1,3이면 G(state)는 2다.
Grundy Number
Grundy Number
12. POSTECH Computer Algorithm Team
- Theorem: G(state)가 1이상이면 이 state는 Win, 0이면 Lose다.
- G(state)가 1 이상이면 G(nextstate)=0인게 반드시 1개이상 존재한다는 의미.
- G(nextstate)=0이 되게하는 nextstate를 고르면 반드시 지지 않는다.
- G(state)가 0이면, 다음 G(nextstate)들은 1 이상이다.
- 상대에게 G(state)=0이 되게하는 nextstate를 주면 나는 다시 G가 1 이상인 state를 받게 된다.
- G(endstate)=0이므로, 반드시 상대가 진다.
Grundy Number
Grundy Number
14. POSTECH Computer Algorithm Team
- 어떤 state에서 Win/Lose는 단순 재귀함수로도 구할수 있지만, 이것 만으로는 정보가 너무 적다.
- Grundy Number는 더 많은 정보를 가지고 있고, 어려운 NIM Game에서는 주로 Grundy Number가 쓰인다.
- 어려운 NIM Game이란 뭘까?
Grundy Number
Grundy Number
15. POSTECH Computer Algorithm Team
- Nim Game N1, N2가 주어져 있다고 하자.
- N1과 N2의 Addition N은 다음과 같이 정의된다.
- 각 Player는 자신의 턴 N1을 Play할지, N2를 Play할지 고른다.
- 고른 Nim Game을 Play한다.
- 둘 중 하나의 게임이라도 Lose하면 전체 게임에서 Lose한다.
- Q. N1+N2 = N2+N1?
- Q. (N1+N2)+N3 = N1+(N2+N3)?
- Q. N+N = ?
Addition of NIM Game
Addition of NIM Game
16. POSTECH Computer Algorithm Team
- Theorem. N을 NIM Game N1과 N2의 addition이라고 정의하자. 그러면, G(N)은 G(N1) XOR G(N2)다.
- (Sprague-Grundy Theorem, 1939)
Addition of NIM Game
Addition of NIM Game
17. POSTECH Computer Algorithm Team
- http://koistudy.net/?mid=prob_page&NO=1014&SEARCH=0
Addition of NIM Game
Addition of NIM Game