Задача A-Місце зустрічі class Explorer: def __init__(self, x, y): self.ways = [(x, y)] with file("cave.map") as f: cavemap = []; explorers = []; Y = 0 for line in f: mapline = [None] * len(line); X = 0 for c in line[:-1]: if c == '.': mapline[X] = set() elif c == '*': e = Explorer(X, Y); explorers.append(e) mapline[X] = set([e]) X += 1 cavemap.append(mapline); Y += 1 N = len(explorers)
def GoTo(explorer, x, y): if 0 <= x < X and 0 <= y < Y: place = cavemap[y][x] if place != None and explorer not in place: place.add(explorer); explorer.ways.append((x, y)) return len(place) return 0 time = 0; maxmeet = 1 while maxmeet < N and any(explorer.ways for explorer in explorers): time += 1 for explorer in explorers: ways = explorer.ways; explorer.ways = [] for x, y in ways: maxmeet = max([GoTo(explorer, x - 1, y), GoTo(explorer, x, y - 1), GoTo(explorer, x + 1, y), GoTo(explorer, x, y + 1), maxmeet]) with file("cave.res", "w") as f: print >> f, time
Задача B-Хакер with file("password.dat") as f: H = int(f.readline()) mincode = 33; maxcode = 126; maxchars = 24 password = [] h = 0; nh = mincode while nh <= H and len(password) < maxchars: h = nh; password.append(mincode) nh = h * 2 + mincode for i in reversed(range(len(password))): password[i] += (H - h) / (2 ** i) if password[i] > maxcode: password[i] = maxcode h += (password[i] - mincode) * (2 ** i) password = "".join(chr(c) for c in reversed(password)) with file("password.res", "w") as f: print >> f, password
Задача С-Немає здачі # encoding: UTF8 with file("market.dat") as f: N = int(f.readline()) A = [int(x) for x in f.readline().split()] B = [int(x) for x in f.readline().split()] S = (sum(A) - N) % 1000; C = A + B; A = [True] + [False] * 999 for v in C: for i in xrange(999, v - 1, -1): A[i] = A[i] or A[i - v] with file("market.res", "w") as f: print >> f, "Будь ласка." if A[S] else "Немає здачі."