ABC112 C - Pyramid

備忘録

問題

atcoder.jp

回答

import sys
import os
import math
import bisect
import itertools
import collections
import heapq
import queue
import array

# 時々使う
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

# 再帰の制限設定
sys.setrecursionlimit(10000000)


def ii(): return int(sys.stdin.buffer.readline().rstrip())
def il(): return list(map(int, sys.stdin.buffer.readline().split()))
def fl(): return list(map(float, sys.stdin.buffer.readline().split()))
def iln(n): return [int(sys.stdin.buffer.readline().rstrip())
                    for _ in range(n)]


def iss(): return sys.stdin.buffer.readline().decode().rstrip()
def sl(): return list(map(str, sys.stdin.buffer.readline().decode().split()))
def isn(n): return [sys.stdin.buffer.readline().decode().rstrip()
                    for _ in range(n)]


def lcm(x, y): return (x * y) // math.gcd(x, y)


# MOD = 10 ** 9 + 7
MOD = 998244353
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    xyh = [il() for _ in range(N)]

    for cx in range(0, 101):
        for cy in range(0, 101):
            tmp_x, tmp_y, tmp_h = -1, -1, -1
            for x, y, h in xyh:
                # 高さが1以上の調査点から
                # ピラミッドの中心を求める
                if h == 0:
                    continue
                tmp_x, tmp_y = cx, cy
                tmp_h = h + abs(x - cx) + abs(y - cy)
                break

            if tmp_h != -1:
                # 求めた中心が全ての調査点の条件と
                # 一致するか否かを確かめる
                for x, y, h in xyh:
                    if h != max(tmp_h - abs(x - tmp_x) - abs(y - tmp_y), 0):
                        break
                else:
                    print(tmp_x, tmp_y, tmp_h)
                    exit()


if __name__ == '__main__':
    main()

考え方

中心座標を決めることで、中心座標の高さを求めることができる。
あとは求めた高さが、他の座標の調査結果と一致するかを確かめる。


中心座標を決めることで、高さを求めることが出来る。
また、中心座標の取りうる範囲はx, yともに0~100なので、
全探索してもO(100 * 100)程度。
なので、中心座標は全探索する方針で進める。

次に、中心座標とh >= 1の調査座標から中心座標の高さを求める。
h >= 1の調査座標を用いるのは、h = 0の場合、
同じ高さの他の座標も候補に挙がってしまうため。

後は、決めた中心座標と求めた中心座標の高さから、
全ての調査座標の条件(調査座標の高さ = max(中心座標の高さ - abs(調査座標x - 中心座標x) - abs(調査座標y - 中心座標y, 0))を
満たすことが出来るか否かで、中心座標が正しい位置であるか判定する。