-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdodgeball.py
More file actions
63 lines (57 loc) · 3.08 KB
/
Copy pathdodgeball.py
File metadata and controls
63 lines (57 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def is_correct_direction(dx, dy, d_name):
if d_name == "N": return dx == 0 and dy > 0
if d_name == "S": return dx == 0 and dy < 0
if d_name == "E": return dy == 0 and dx > 0
if d_name == "W": return dy == 0 and dx < 0
if d_name == "NE": return dx > 0 and dy > 0 and dx == dy
if d_name == "SW": return dx < 0 and dy < 0 and dx == dy
if d_name == "SE": return dx > 0 and dy < 0 and dx == -dy
if d_name == "NW": return dx < 0 and dy > 0 and dx == -dy
return False
def compute_throws(players_dict, start_dir, start_id):
directions = ["N","NE", "E", "SE", "S", "SW", "W", "NW"] # DIRS v pořadí hodinových ručiček
field = {i+1: {"x": p[0], "y": p[1], "active": True} for i, p in players_dict.items()} # 1-based
current_id, from_dir, throw_count = start_id, start_dir, 0
while True:
field[current_id]["active"] = False # hráč, který míč má, ho teď hodí a odejde
start_search_idx = (directions.index(from_dir) + 1) % 8
target_id, found_at_dir = None, None
for i in range(8): # prohlédneme všech 8 směrů
search_dir = directions[(start_search_idx + i) % 8]
best_dist, temp_target = float("inf"), None
for p_id, p_data in field.items():
if not p_data["active"]: continue
dx, dy = p_data["x"] - field[current_id]["x"], p_data["y"] - field[current_id]["y"]
if is_correct_direction(dx, dy, search_dir): # ověření směru
dist = max(abs(dx), abs(dy))
if dist < best_dist: best_dist, temp_target = dist, p_id
if temp_target:
target_id, found_at_dir = temp_target, search_dir
break # přestane se otáčet
if target_id:
throw_count += 1
from_dir = directions[(directions.index(found_at_dir) + 4) % 8]
current_id = target_id
else: return throw_count, current_id
def read_throws(input_file: str, output_file: str):
try: #reading from input nubler of rounds,players and coords of players
with open(input_file, "r", encoding="utf-8") as f, open(output_file, "w") as f_out:
lines = [l.strip() for l in f if l.strip()]
if not lines: return
it = iter(lines)
num_test_cases = int(next(it))
for _ in range(num_test_cases):
N = int(next(it))
player_spots = {}
for i in range(N):
coords = next(it).split()
player_spots[i] = (int(coords[0]), int(coords[1]))
start_dir = next(it)
start_player_id = int(next(it))
throws, last_p = compute_throws(player_spots, start_dir, start_player_id)
f_out.write(f"{throws} {last_p}\n")
#print(f"{throws} {last_p}")
except FileNotFoundError: print("File not found")
if __name__ == "__main__":
read_throws("./dodgeball/dodgeball_throws.txt", "./dodgeball/output_throws.txt")
read_throws("./dodgeball/test.in.txt","./dodgeball/test.out.txt")