0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AtCoder Beginner Contest 225の[C - Calendar Validator]

Last updated at Posted at 2021-11-01

SwiftでAtCoder Beginner Contest 225の[C - Calendar Validator]を解きました。

問題内容

問題文
10の100乗行7列の行列Aがあり、任意の整数対 (i,j) (1≤i ≤10の100乗,1≤j≤7) についてその(i,j)成分は(i−1)×7+jです。
N行M列の行列Bが与えられるので、BがAから一部の矩形領域を(向きを変えずに)切り出したものであるかを判定してください。

解答コード

import Foundation

let aa = readLine()!.split(separator:" ").map{Int($0)!}
let n = aa[0]
let m = aa[1]

var array : [[Int]] = []

var flag = true

for _ in 0..<n{
    let aa = readLine()!.split(separator:" ").map{Int($0)!}
    array.append(aa)
}
for i in 0..<m{
    for ii in 1..<n{
        if array[ii][i] - array[ii - 1][i] != 7{
            flag = false
        }
        
    }
}
for i in 0..<n{
    for ii in 1..<m{
        var after = array[i][ii] % 7
        var before = array[i][ii - 1] % 7
        
        if before == 0 {
            before = 7
        }
        if after == 0 {
            after = 7
        }
        if after - before != 1{
            flag = false
        }
    }
}

if flag == true{
    print("Yes")
}
else{
    print("No")
}

二次元配列を作って、

上より下の方が7大きい
左より右の方が1大きい

ことを確認しています。

ただ、右端が7の倍数になることを考えると7,8,9と並ぶことはありえませんので、左より右の方が1大きいことを確認する際は、7で割った時のあまり(7の倍数は0ではなく7にする)を比較しています。

コードが長めになってしまいましたがACが出たので良かったです。

Swiftのお役立ち情報

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?