-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sys | ||
from collections import deque | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N, M = map(int, input().split()) | ||
ladder = [list(map(int, input().split())) for _ in range(N)] | ||
snake = [list(map(int, input().split())) for _ in range(M)] | ||
game = [i for i in range(1,101)] | ||
visited = [0 for _ in range(100)] | ||
q = deque() | ||
|
||
q.append((1,0)) | ||
visited[0] = 1 | ||
|
||
while q: | ||
cur, dice = q.popleft() | ||
if cur == 100: | ||
print(dice) | ||
break | ||
for i in range(1, 7): | ||
check = 0 | ||
|
||
if cur + i > 100: continue | ||
|
||
# μ¬λ€λ¦¬ κ° μ μλμ§ κ²μ¬ | ||
for l, n in ladder: | ||
if cur + i == l: | ||
if visited[n-1] == 0: | ||
q.append((n, dice+1)) | ||
visited[n-1] = 1 | ||
check = 1 | ||
break | ||
|
||
if check: continue | ||
# λ± μλμ§ κ²μ¬ | ||
for s, n in snake: | ||
if cur + i == s: | ||
if visited[n-1] == 0: | ||
q.append((n, dice+1)) | ||
visited[n-1] = 1 | ||
check = 1 | ||
break | ||
|
||
if check: continue | ||
# κ·Έ μΈ | ||
if visited[cur + i-1] == 0: | ||
q.append((cur + i, dice + 1)) | ||
visited[cur + i-1] = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
var answer = 0 | ||
var n: Int = 0 | ||
lateinit var board: IntArray | ||
fun main() { | ||
val br = BufferedReader(InputStreamReader(System.`in`)) | ||
n = br.readLine().toInt() | ||
board = IntArray(n) | ||
backTracking(0) | ||
println(answer) | ||
} | ||
|
||
private fun backTracking(row: Int) { | ||
// row κ° μ¦κ°νλ€κ° n μ΄λ κ°μμ§λ©΄ μ λ΅ +1 | ||
if (row == n) { | ||
answer++ | ||
return | ||
} | ||
// row μΌ λ κ° μ΄μ λμ κ²½μ°μ μ | ||
for (col in 0 until n) { | ||
board[row] = col | ||
// λμ μ μλ€λ©΄ λ€μ ν | ||
if (check(row)) backTracking(row + 1) | ||
} | ||
} | ||
|
||
private fun check(row: Int): Boolean { | ||
for (i in 0 until row) { | ||
// νμ΄ κ°κ±°λ | ||
if (board[row] == board[i]) { | ||
return false | ||
} | ||
// λκ°μ | ||
if (row - i == kotlin.math.abs(board[row] - board[i])) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |