0
0

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.

paizaラーニング レベルアップ問題集 線形探索メニュー応用編 JavaScript 二次元データの区間探索

Last updated at Posted at 2022-11-02

二次元データの区間探索 (paizaランク B 相当)

解答例

レジャーシート左上基準で、区画を順に調べていきます。
レジャーシートを敷く区画がxを超過していたら敷けません。
敷けるならカウントします。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const [n, m, k, x] = lines[0].split(" ").map(Number);
const field = lines.slice(1).map(line => line.split(" ").map(Number));

let count = 0;//何通りあるか
//レジャーシート左上基準で、区画を順に調べていく
for (let i = 0; i <= n - k; i++) {
  for (let j = 0; j <= m - k; j++) {
    //敷けるか調べる
    let flag = true;//敷けるか判定
    for (let p = i; p < i + k; p++) {
      for (let q = j; q < j + k; q++) {
        //区画の番号がx超過なら
        if (field[p][q] > x) {
          flag = false;//敷けない
          break;
        }
      }
    }
    //敷けるなら
    if (flag) {
      count += 1;
    }
    
  }
}
console.log(count);
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?