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?

移動が可能かの判定・方角

Posted at

今回は paiza の「移動が可能かの判定・方角」の問題に挑戦!

似たような問題だけど、新たなシリーズに突入!


🧩 問題概要

  • H×W のマップが与えられる
  • '.' = 移動可能, '#' = 障害物
  • 現在の位置 (sy, sx) と移動方向 m(N/S/E/W) が与えられる
  • 1 マスだけ、m の方向に移動できるか判定する

※ 移動可能とは、移動先が「マップの範囲内」かつ「'.'」であること



入力例:

3 3 1 1 E // H W sy sx m
..#       // S_i
..#
...

出力例:

No






✅ OK例:

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

const lines = [];

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

rl.on('close', () => {
    const [H, W, sy, sx, m] = lines[0].split(' ');
    const gridS = lines.slice(1).map(line => line.split(''));
    
    const y = Number(sy);
    const x = Number(sx);
    
    if (m === 'N') {
        if (y-1 >= 0 && gridS[y-1][x] === '.') {
            console.log('Yes');
        } else { // === '#'
            console.log('No');
        }
    } else if (m === 'S') {
        if (y+1 < Number(H) && gridS[y+1][x] === '.') {
            console.log('Yes');
        } else { 
            console.log('No');
        }
    } else if (m === 'E') {
        if (x+1 < Number(W) && gridS[y][x+1] === '.') {
            console.log('Yes');
        } else { 
            console.log('No');
        }
    } else { // === 'W'
       if (x-1 >= 0 && gridS[y][x-1] === '.') {
            console.log('Yes');
        } else { 
            console.log('No');
        } 
    }
});

🔍 コードの流れ

  1. 入力を lines に読み込む
  2. 1 行目から
  3. H, W, sy, sx, m を取得(マップサイズ・現在座標・移動方向)
  4. 2 行目以降をマップ gridS として配列化
  5. 現在位置 y, x を数値に変換して設定
  6. 移動方向 m に応じて以下を判定
    • 北なら y-1 が範囲内かつ移動先が '.'
    • 南なら y+1 が範囲内かつ移動先が '.'
    • 東なら x+1 が範囲内かつ移動先が '.'
    • 西なら x-1 が範囲内かつ移動先が '.'
  7. 条件を満たせば "Yes"、満たさなければ "No" を出力




✨ 短縮例:

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

const lines = [];

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

rl.on('close', () => {
    const [H, W, sy, sx, m] = lines[0].split(' ');
    const gridS = lines.slice(1).map(line => line.split(''));
    
    let y = Number(sy);
    let x = Number(sx);
    
    if (m === 'N') {
        y--;
    } else if (m === 'S') {
        y++;
    } else if (m === 'E') {
        x++;
    } else { // === 'W'
        x--;
    }
    
    if (y >= 0 && y < Number(H) && x >= 0 && x < Number(W) && gridS[y][x] === '.') {
        console.log('Yes');
    } else {
        console.log('No');
    }
});

🔍 コードの流れ

  1. 標準入力を1行ずつ受け取り、lines 配列に格納する
  2. 入力が終わったら close イベントが発火して処理開始
  3. 1行目をスペース区切りで分解し、以下の値を取得
    • H: 高さ
    • W: 幅
    • sy: 初期 y 座標
    • sx: 初期 x 座標
    • m: 移動方向(N/S/E/W)
  4. 残りの行を 1文字ずつに分解して gridS という2次元配列にする(マップ)
  5. 現在位置 (y, x) を数値に変換して設定
  6. m の方向に応じて y または x を1マス移動
  7. 最後に 移動後の位置が
    • 配列の範囲内であるか?
    • '.'(通れるマス)か?
      をチェック
  8. 条件を満たせば Yes、そうでなければ No を出力






🗒️ まとめ

  • マップは y が行、x が列
  • 範囲外チェック → 0 ≤ y < H, 0 ≤ x < W
  • 方向ごとに座標を変化
  • 移動先が '.' なら Yes'#' or 範囲外なら No




僕の失敗談(´;ω;`)と解決法🐈

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?