2021. 11. 28. 16:06γProgramming/Algorithms
1. λ¬Έμ
ν±νν λ 3x3 ν¬κΈ°μ κ²μνμμ νλ 3λͺ© κ²μμ λλ€. λ λͺ μ΄ λ²κ°μκ°λ©° oμ xλ₯Ό κ²μνμ λΉ μΉΈμ μ°λ, λ¨Όμ κ°μ κΈμλ₯Ό κ°λ‘, μΈλ‘ νΉμ λκ°μ μΌλ‘ 3κ° μ°μ΄λλ‘ νλ μͺ½μ΄ μ΄κΉλλ€. μλ₯Ό λ€μ΄, λ€μ κ²μνμ xκ° μ΄κΈ΄ κ²μνμ λλ€.
xoo
.x.
..x
κ²μμ νμ xλΆν° μμν©λλ€.
ν±νν κ²μνμ νμ¬ μνκ° μ£Όμ΄μ§λλ€. λ μ¬λ λͺ¨λ μ΅μ μ λ€νλ€κ³ κ°μ ν λ, μ΄λμͺ½μ΄ μ΄κΈΈμ§ νλ¨νλ νλ‘κ·Έλ¨μ μμ±νμΈμ.
2. νμ΄
/*
- canWin(board) :
κ²μνμ΄ νμ¬ boardμΌ λ, μ΄λ² μ°¨λ‘μΈ μ¬λμ΄
μ΄κΈΈ μ μλ€λ©΄ 1,
λΉκΈΈ μ μλ€λ©΄ 0,
μ§ μ λ°μ μλ€λ©΄ -1μ λ°ννλ€.
+ boardλ₯Ό μν μ리μ 3μ§μλ‘ λ³Έλ€.
- ν¨μμ λ°ν κ° μ€ -1μ΄ μμΌλ―λ‘,
cache[]λ₯Ό -2λ‘ μ΄κΈ°νν΄μΌνλ€.
- canWin()μ νμ¬ κ²μν μν©κ³Ό λλΆμ΄ λꡬ μ°¨λ‘μΈμ§λ₯Ό turnμΌλ‘ μ λ¬νλ€.
- canWin()μ λͺ¨λ μλ₯Ό μλνλ©°, λ°ν κ° μ€ κ°μ₯ μμ κ²μ μ°Ύλλ€.
: μ΄λ€ μλ₯Ό λμμ λ, -1μ΄ λ°νλλ€λ©΄ λ΄κ° μ΄κΈΈ μ μλ€λ λ»μ΄κ³
-1 μμ΄ 0μ΄ μλ€λ©΄ 0μ΄ μλ€λ©΄ λΉκΈΈ μ μλ€λ λ»μ΄λ€.
*/
bool isFinished(const vector<string>& board, char turn);
// turnμ΄ ν μ€μ λ§λ€μλμ§ νμΈνλ€.
/*
bijection (μ λ¨μ¬ ν¨μ)
: λ μ§ν© μ¬μ΄λ₯Ό μ€λ³΅ μμ΄ λͺ¨λ μΌλμΌλ‘ λμμν€λ ν¨μ, μΌλμΌ λμ.
: κ²μν(board)κ° μ£Όμ΄μ‘μ λ, [0 ~ 19682 ~=3^9 ] λ²μμ μ μλ‘ λ³ννλ€.
*/
int bijection(const vector <string>&board) {
int ret = 0;
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
ret = ret * 3;
if (board[y][x] == 'o') {
++ret;
}
else if (board[y][x] == 'x') {
ret += 2;
}
}
}
return ret;
}
int cache[19683];
int canWin(vector<string>& board, char turn) {
// board μμ λ³νμ΄ μκΈ° λλ¬Έμ, const μ μΈX
if (isFinished(board, 'o' + 'x' - turn) == true ) {
return -1;
// κΈ°μ μ¬λ‘: λ§μ§λ§μ μλκ° λμ΄ ν μ€μ΄ λ§λ€μ΄μ§ κ²½μ° -> -1 λ°ν
// λ΄κ° xμ΄λ©΄ input parameterμ΄ o, λ΄κ° oμ΄λ©΄ input parameterμ΄ x!
}
int& ret = cache[bijection(board)];
if (ret != -2) {
return ret;
}
int minValue = 2;
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
if (board[y][x] == '.') {
board[y][x] = turn;
// λΉ μΉΈμ λμ μλ₯Ό λμμ λ, κ²°κ³Όλ₯Ό μμλ‘ λμΆν΄λ³Έλ€.
// λ°λ³΅λ¬Έμ ν΅νμ¬ 9κ°μ§ κ²½μ°μ μ λͺ¨λ μννλ©° νλ³νλ€.
minValue = min(minValue, canWin(board, 'o' + 'x' - turn));
board[y][x] = '.';
// λ€μ λλλ € λλ κ³Όμ !
}
}
}
if (minValue == 2 || minValue == 0) {
return ret = -minValue;
// νλ μ΄ ν μ μκ±°λ, λΉκΈ°λ κ²μ΄ μ΅μ μΈ κ²½μ°
// μλκ° μ΄κΈ°λ κ² -> λ΄κ° μ§λ κ²
}
}
3. impartial game
In combiantional game theory, an impartial game is a game in which the allowable moves depend
only on the position and not on which of the two players is currently moving, and where the payoffs are symmetric.
the only difference between player 1 and player 2 is that player 1 goes first.
The game is played until a terminal position is reached.
A terminal position is one from which no moves are possible.
Then one of the players is declared the winner and the other the loser.
Furthermore, impartial games are played with perfect information and no chance moves, meaning all information about the game and operations for both players are visible to both players.
4. git-hub
5. μΆμ²
- https://www.algospot.com/judge/problem/read/TICTACTOE
'Programming > Algorithms' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μκ³ λ¦¬μ¦ λ¬Έμ νμ΄] STRJOIN λ¬Έμμ΄ ν©μΉκΈ° / ννλ§ μ½λ (0) | 2021.11.30 |
---|---|
[μκ³ λ¦¬μ¦ λ¬Έμ νμ΄] NUMBERGAME : μ«μ κ²μ (0) | 2021.11.28 |
[μκ³ λ¦¬μ¦ λ¬Έμ νμ΄] ZIMBABWE (0) | 2021.11.27 |
[μκ³ λ¦¬μ¦] λμ κ³νλ², λ©λͺ¨μ΄μ μ΄μ μ΄λ? (0) | 2021.11.27 |