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

裏返せる可能性(縦横)

Last updated at Posted at 2025-12-16

今回は paiza の「裏返せる可能性(縦横)」の問題に挑戦!

新シリーズのテーマは「リバーシ」!


問題概要

  • H 行、横 W 列の盤面を作成する問題
  • 盤面上の 1 マス (Y, X) に石を置く
  • 盤面の各マスは、次のルールで文字を決める
    • 石を置いたマス (Y, X)'!'
    • 石を置いたマスと 同じ行または同じ列 のマス → '*'
    • 上記以外のマス → '.'
  • 座標は
    • 左上が (0, 0)
    • 下方向が y の正方向
    • 右方向が x の正方向
  • 条件に従って作成した盤面を 上から順に H 行出力 する



入力例:

10 10 5 5 // H W Y X

出力例:

.....*....
.....*....
.....*....
.....*....
.....*....
*****!****
.....*....
.....*....
.....*....
.....*....






✅OK例:

const rl = require('readline').createInterface({ input: process.stdin });

const lines = [];

rl.on('line', (input) => lines.push(input));

rl.on('close', () => {
    const [H, W, Y, X] = lines[0].split(' ').map(Number);
    
    const grid = Array.from({ length: H }, () => Array(W).fill('.'));
    
    for (let y = 0; y < H; y++) {
        for (let x = 0; x < W; x++) {
            if (y === Y && x === X) {
                grid[y][x] = '!';
            } else if (y === Y || x === X) {
                grid[y][x] = '*';
            }
        }
    }
    
    grid.forEach(g => console.log(g.join('')));
});

🔍コードの流れ

  • 標準入力を受け取る準備をする
  • 入力の1行目から
    • 盤面の高さ H
    • 盤面の幅 W
    • 石を置く行 Y
    • 石を置く列 X
      を取得する
  • H × W の盤面を、すべて '.' で初期化する
  • 盤面の全マスを二重ループで順に確認する
  • 各マスについて
    • (Y, X) と一致するマスは '!' を置く
    • それ以外で、「行または列」が (Y, X) と同じマスは '*' を置く
  • 盤面の各行を文字列として出力する






📝まとめ

  • 二次元配列で盤面を表現すると処理しやすい
  • 盤面はまず すべて . で初期化 する
  • 全マスを 二重ループ(yx) で走査する
  • 条件判定の優先順位が重要
    • (Y, X) を最優先で '!' にする
    • 次に「同じ行 or 同じ列」を '*' にする
  • 行・列の一致は、y === Y || x === X で簡単に判定できる
  • 小さい制約(最大 20×20)なので 全探索で十分
1
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
1
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?