repl.it
@mcparadip/

HideousYearlyNonlinearprogramming

Python 2.7

No description

fork
loading
Files
  • main.py
main.py
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
# Oliver Ni, Grade 9
# Project Code Foundation
# Python 2

A              = ((0, 1), (0, 1))
B              = ((1, 0), (0, 1))
C              = ((0, 1), (1, 0), (1, 0))
BLOCKS         = (A, B, C)
rev            = lambda block: [(-x[0], -x[1]) for x in block[::-1]]
cell_to_coords = lambda c, cell: ((cell - 1) / c, (cell - 1) % c)

def place_block(blocked, r, c, i, j, typ, dire):
  block = (BLOCKS[typ], rev(BLOCKS[typ]))[dire]
  block += ((0, 0),)

  for move in block:
    if 0 <= i <= r - 1 and 0 <= j <= c - 1 and not blocked[i][j]:
      i += move[0]
      j += move[1]
    else:
      return None

  return i, j

for _ in range(5):
  inp = map(int, raw_input("> ").split())
  (r, c, s, n), blocks = inp[:4], inp[4:]

  blocked = [[False for j in range(c)] for i in range(r)]

  for b in blocks:
    coords = cell_to_coords(c, b)
    blocked[coords[0]][coords[1]] = True

  i, j = cell_to_coords(c, s)
  dire = 0 if j == 0 else 1
  typ, ans = 0, ""

  for _ in range(100):
    nex = place_block(blocked, r, c, i, j, typ, dire)

    if nex is not None:
      i, j = nex
      j += (1, -1)[dire]
      ans += "ABC"[typ]
      if not 0 <= j <= c - 1: break

    typ += 1
    typ %= 3

  print (ans, ans[::-1])[dire]
Fetching token
?