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?

【マップの扱い 1】マップの書き換え・1 マス

Posted at

今回は paiza の「【マップの扱い 1】マップの書き換え・1 マス」の問題に挑戦!

これからまた、新しいテーマの問題を解いていく!


問題概要

◯ 入力

  • 行数 H、列数 W
  • H 行の盤面
  • 書き換える座標 (y, x)

◯ 処理

  • 指定された (y, x) のマスを「.#」に反転する

◯ 出力

  • 書き換え後の盤面を H 行出力




入力例:

3 3
...
...
...
0 0

出力例:

#..
...
...






✅ OK例:

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

const lines = [];

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

rl.on('close', () => {
    const [H, W] = lines[0].split(' ').map(Number);
    const [y, x] = lines[H+1].split(' ').map(Number);
    

    // 盤面を2次元配列に変換
    const map = lines.slice(1, H+1).map(line => line.split(''));
    

    // 指定座標の反転処理
    if (map[y][x] === '.') { 
        map[y][x] = '#';
    } else if (map[y][x] === '#') {
        map[y][x] = '.';
    }
    

    // 出力
    map.forEach(m => console.log(m.join('')));
});



if-else でも良いけど、三項演算子で短く書ける:

map[y][x] = map[y][x] === '.' ? '#' : '.';






🗒️ ポイント


◯ 盤面を2次元配列に変換する

  • 文字列のままだと1文字だけ置き換えができない。
  • .map(line => line.split'')) で配列にすれば map[y][x] のように直接アクセス・更新できる。

◯ 三項演算子で反転処理を短く書ける

  • if-else でもOKだが、条件がシンプルなので三項演算子が向いている。




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

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?