icpc probs
This commit is contained in:
parent
4c531b965f
commit
69d48861b1
17 changed files with 448 additions and 2 deletions
23
kattis/23-9-24/b/input.txt
Normal file
23
kattis/23-9-24/b/input.txt
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
2
|
||||
|
||||
.#.#.#.#.#
|
||||
#.#.#.#.#.
|
||||
.#.#.B.#.#
|
||||
#.#.#.#.#.
|
||||
.#.#.B.#.#
|
||||
#.#.W.#.#.
|
||||
.#.#.#.#.#
|
||||
#.#.#.B.#.
|
||||
.#.#.#.#.#
|
||||
#.#.#.#.#.
|
||||
|
||||
.#.#.#.#.#
|
||||
#.#.#.#.#.
|
||||
.#.#.B.#.#
|
||||
#.B.#.B.#.
|
||||
.#.#.B.#.#
|
||||
#.B.W.#.#.
|
||||
.#.B.B.#.#
|
||||
#.#.#.#.#.
|
||||
.#.B.B.#.#
|
||||
#.#.#.#.#.
|
||||
117
kattis/23-9-24/b/solution.py
Normal file
117
kattis/23-9-24/b/solution.py
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
"""
|
||||
Backtracking Framework
|
||||
- Set of choices
|
||||
- Limited by constraints
|
||||
- To reach a goal
|
||||
|
||||
1. Understand
|
||||
- Board of W/B, can capture; looking for max consecutive sequence of captures using same piece
|
||||
- Capture in any direction "skipping over"; land on open square
|
||||
|
||||
Similar Problems: N Queens
|
||||
|
||||
Set of candidates (W pieces) limited in moves (capturing B pieces) to reach max # captures (i.e. capturing moves)
|
||||
2. Develop
|
||||
|
||||
- Given board state, w/ W candidates
|
||||
- Consider every candidate as the piece yielding correct answer
|
||||
- For every candidate, consider every capturing move they can make
|
||||
- For each move:
|
||||
- Make the move, and recursively find max # captures using this piece after this capture
|
||||
- update/restore state on failure
|
||||
- Update max if new max found
|
||||
|
||||
consider after: caching/optimization
|
||||
|
||||
backtracking function: def max_captures(board, w) -> int:
|
||||
|
||||
|
||||
3. Carry Out
|
||||
4. Revise
|
||||
|
||||
At any particular position,
|
||||
"""
|
||||
|
||||
BOARD_SIZE = 10
|
||||
|
||||
|
||||
def parse_board() -> tuple[list[list[str]], list[tuple[int, int]]]:
|
||||
board = []
|
||||
candidates: list[tuple[int, int]] = []
|
||||
|
||||
input()
|
||||
|
||||
for r in range(BOARD_SIZE):
|
||||
board.append(list(input()))
|
||||
|
||||
candidates.extend((r, c) for c, cell in enumerate(board[-1]) if cell == "W")
|
||||
|
||||
return board, candidates
|
||||
|
||||
|
||||
def valid(board, r, c):
|
||||
return 0 <= r < len(board) and 0 <= c < len(board[0])
|
||||
|
||||
|
||||
# all capturing moves white piece can make
|
||||
def capturing_moves(board, r, c) -> list[tuple[int, int]]:
|
||||
if not valid(board, r, c):
|
||||
return []
|
||||
|
||||
moves = []
|
||||
|
||||
for dr, dc in [(-1, -1), (1, 1), (-1, 1), (1, -1)]:
|
||||
if (
|
||||
valid(board, r + 2 * dr, c + 2 * dc)
|
||||
and board[r + dr][c + dc] == "B"
|
||||
and board[r + 2 * dr][c + 2 * dc] not in "BW"
|
||||
):
|
||||
moves.append((dr, dc))
|
||||
|
||||
return moves
|
||||
|
||||
|
||||
def max_candidate_captures(board, r, c) -> int:
|
||||
max_captures = 0
|
||||
|
||||
for dr, dc in capturing_moves(board, r, c):
|
||||
# place
|
||||
board[r][c] = "."
|
||||
board[r + dr][c + dc] = "."
|
||||
board[r + dr * 2][c + dc * 2] = "W"
|
||||
|
||||
ans = max_candidate_captures(board, r + dr * 2, c + dc * 2)
|
||||
max_captures = max(max_captures, 1 + ans)
|
||||
|
||||
# unplace
|
||||
board[r + dr * 2][c + dc * 2] = "."
|
||||
board[r][c] = "W"
|
||||
board[r + dr][c + dc] = "B"
|
||||
|
||||
return max_captures
|
||||
|
||||
|
||||
def max_captures(board, candidates: list[tuple[int, int]]) -> int:
|
||||
max_captures = 0
|
||||
|
||||
for r, c in candidates:
|
||||
max_captures = max(max_captures, max_candidate_captures(board, r, c))
|
||||
|
||||
return max_captures
|
||||
|
||||
|
||||
def solve() -> None:
|
||||
T = int(input())
|
||||
|
||||
while T:
|
||||
board, candidates = parse_board()
|
||||
print(max_captures(board, candidates))
|
||||
T -= 1
|
||||
|
||||
|
||||
def main() -> None:
|
||||
solve()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
3
kattis/23-9-24/contest.md
Normal file
3
kattis/23-9-24/contest.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# contest 2
|
||||
|
||||
https://open.kattis.com/contests/wrrf23
|
||||
1
kattis/23-9-24/d/input.txt
Normal file
1
kattis/23-9-24/d/input.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
(2+(2*2)+2)
|
||||
46
kattis/23-9-24/d/solution.py
Normal file
46
kattis/23-9-24/d/solution.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
def solve(equation: str) -> None:
|
||||
stack = []
|
||||
paren_pairs = []
|
||||
|
||||
for i, token in enumerate(equation):
|
||||
if token == "(":
|
||||
stack.append([i, None])
|
||||
elif token == ")":
|
||||
l, r = stack.pop()
|
||||
r = i
|
||||
paren_pairs.append((l, r))
|
||||
|
||||
P = [[]]
|
||||
|
||||
for paren_pair in paren_pairs:
|
||||
P.extend([[paren_pair] + p for p in P])
|
||||
|
||||
def format(permutation):
|
||||
output = list(equation)
|
||||
|
||||
for l, r in permutation:
|
||||
output[l] = None
|
||||
output[r] = None
|
||||
|
||||
return "".join(filter(lambda token: token, output))
|
||||
|
||||
seen = set()
|
||||
ans = []
|
||||
for permutation in P[1:]:
|
||||
output = format(permutation)
|
||||
if output not in seen:
|
||||
seen.add(output)
|
||||
ans.append(output)
|
||||
|
||||
for x in sorted(ans):
|
||||
print(x)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
equation = input()
|
||||
|
||||
solve(equation)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
22
kattis/23-9-24/e/solution.py
Normal file
22
kattis/23-9-24/e/solution.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_."
|
||||
|
||||
character_to_index = {c: i for i, c in enumerate(ALPHABET)}
|
||||
index_to_character = {i: c for i, c in enumerate(ALPHABET)}
|
||||
|
||||
while line := input():
|
||||
split = line.split()
|
||||
|
||||
if len(split) == 1:
|
||||
break
|
||||
|
||||
k, string = int(split[0]), split[1]
|
||||
|
||||
backwards = string[::-1]
|
||||
|
||||
ans: list[str] = []
|
||||
|
||||
for letter in backwards:
|
||||
index = character_to_index[letter]
|
||||
ans.append(index_to_character[(index + k) % len(ALPHABET)])
|
||||
|
||||
print("".join(ans))
|
||||
6
kattis/23-9-24/f/input.txt
Normal file
6
kattis/23-9-24/f/input.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Will Smith
|
||||
Agent Smith
|
||||
Peter Pan
|
||||
Micky Mouse
|
||||
Minnie Mouse
|
||||
Peter Gunn
|
||||
32
kattis/23-9-24/f/solution.py
Normal file
32
kattis/23-9-24/f/solution.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import sys
|
||||
from collections import Counter
|
||||
|
||||
|
||||
def solve() -> None:
|
||||
counter: Counter[str] = Counter()
|
||||
|
||||
ans = []
|
||||
|
||||
lines = sys.stdin.readlines()
|
||||
for line in lines:
|
||||
first, last = line.split()
|
||||
|
||||
counter[first] += 1
|
||||
|
||||
ans.append((last, first))
|
||||
|
||||
ans.sort()
|
||||
|
||||
for last, first in ans:
|
||||
if counter[first] > 1:
|
||||
print(f"{first} {last}")
|
||||
else:
|
||||
print(first)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
solve()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue