Submission #667714


Source Code Expand

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import itertools
import numpy
import random
import sys

DS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
H = 30
W = 30


class Solver(object):

    def __init__(self, stage, deltas=DS):
        self.stage = stage
        self.hands = None
        self.deltas = deltas[:]
        self.init_hands()

    def init_hands(self):
        self.hands = []

    def decrement(self, r, c):
        self.hands.append((r + 1, c + 1))
        self.stage[r, c] -= 1

    def naive_solution(self):
        for r, c in itertools.product(range(H), range(W)):
            cell = self.stage[r, c]
            if cell <= 0:
                continue
            for _ in range(cell):
                self.decrement(r, c)

    def search_adj(self, r0, c0):
        random.shuffle(self.deltas)
        for dr, dc in self.deltas:
            (r, c) = (r0 + dr, c0 + dc)
            if r < 0 or r >= H or c < 0 or c >= W:
                continue
            elif 0 < self.stage[r, c] == self.stage[r0, c0]:
                self.decrement(r, c)
                self.search_adj(r, c)
                break

    def use_multiple_adj_solution(self):
        for r0, c0 in itertools.product(range(H), range(W)):
            if self.stage[r0, c0] <= 0:
                continue
            self.decrement(r0, c0)
            self.search_adj(r0, c0)
        self.naive_solution()


def main():
    sys.setrecursionlimit(10000)
    stage = numpy.array([list(map(int, input().split())) for _ in range(H)])
    solver = Solver(stage)
    solver.use_multiple_adj_solution()
    for r, c in solver.hands:
        print("{} {}".format(r, c))
    # print(solver.stage)


if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task A - 高橋君の山崩しゲーム
User hamukichi
Language Python (3.4.3)
Score 541371
Code Size 1778 Byte
Status AC
Exec Time 3676 ms
Memory 30952 KB

Judge Result

Set Name test_01 test_02 test_03 test_04 test_05 test_06 test_07 test_08 test_09 test_10
Score / Max Score 55576 / 100000 53244 / 100000 54832 / 100000 51897 / 100000 54816 / 100000 54594 / 100000 53724 / 100000 55331 / 100000 53820 / 100000 53537 / 100000
Status
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
Set Name Test Cases
test_01 subtask_01_01.txt
test_02 subtask_01_02.txt
test_03 subtask_01_03.txt
test_04 subtask_01_04.txt
test_05 subtask_01_05.txt
test_06 subtask_01_06.txt
test_07 subtask_01_07.txt
test_08 subtask_01_08.txt
test_09 subtask_01_09.txt
test_10 subtask_01_10.txt
Case Name Status Exec Time Memory
subtask_01_01.txt AC 3676 ms 30952 KB
subtask_01_02.txt AC 465 ms 16692 KB
subtask_01_03.txt AC 441 ms 16536 KB
subtask_01_04.txt AC 483 ms 16820 KB
subtask_01_05.txt AC 457 ms 16660 KB
subtask_01_06.txt AC 475 ms 16612 KB
subtask_01_07.txt AC 467 ms 16692 KB
subtask_01_08.txt AC 459 ms 16564 KB
subtask_01_09.txt AC 468 ms 16688 KB
subtask_01_10.txt AC 480 ms 16692 KB