LoginSignup
2
0

More than 5 years have passed since last update.

ABC088 C - Takahashi's Information (300点)

Posted at

問題概要

問題のリンク

$3×3$ のグリッドがある。各行を上から$i$、各列を左から$j$でナンバリングしていく時、マス$(i,j)$に数$c_{i,j}$が書かれている。
また $c_{i,j} = a_i + b_j$である。

以下のように$c_{1,1}$~$c_{3,3}$までが与えられる時、成立するか判定しろ。

1 0 1
2 1 2
1 0 1

制約条件

$c_{i,j} (1≤i≤3,1≤j≤3)$ は $0$ 以上 $100$ 以下の整数

考えたこと

$c_{i,j} = a_i + b_j$ ということは $c_{i,j} - a_i = b_j$ である。
よって、各行の数から $a_i$を引き、各列の値が同じになれば与えられたグリッドは正しいと言える。

ではどのように$a_i$を求めるかだが、$a_i$と$b_j$の制約はないため、同じ基準で$a_i$を仮定すればよい。私は最小値を使った。

入力例で実験すると以下のようになる。
20190402_abc088-c.001.jpeg
20190402_abc088-c.002.jpeg
20190402_abc088-c.003.jpeg

計算量は入力によらず一定で $O(1)$である。

解答

c.cs
using System;
using System.Linq;

class Program
{
    static void Main(string[] args) {
        int[][] c = new int[3][];
        c[0] = Console.ReadLine().Split().Select(int.Parse).ToArray();
        c[1] = Console.ReadLine().Split().Select(int.Parse).ToArray();
        c[2] = Console.ReadLine().Split().Select(int.Parse).ToArray();
        Console.WriteLine(solve(c) ? "Yes" : "No");
    }
    static bool solve(int[][] c) {
        for (int i = 0; i < c.Length; i ++) {
            int min = c[i].Min();
            for (int j = 0; j < c[i].Length; j ++) {
                c[i][j] -= min;
            }
        }
        for (int i = 0; i < c.Length; i ++) {
            if(c[0][i] != c[1][i] || c[1][i] != c[2][i]) return false;
        }
        return true;
    }
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0