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ラーニング レベルアップ問題集 新・Bランクレベルアップメニュー JavaScript 【マップの扱い 2】マップの書き換え・縦横

Posted at

【マップの扱い 2】マップの書き換え・縦横 (paizaランク B 相当)

解答例

隣接をfor (let i = -1; i <= 1; i += 2)を用いて表現した。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");
//盤面の行数を表す整数 H , 盤面の列数を表す整数 W 
const [H, W] = lines[0].split(" ").map(Number);
//盤面の i 行目の文字をまとめた文字列 S
const S = lines.slice(1, H + 1).map(line => line.split(""));

//与えられた座標のマスと上下左右で隣接するマスの最大 5 マスに処理

//文字を書き換えるマスの y , x 座標
const [y, x] = lines[H + 1].split(" ").map(Number);
if (S[y][x] === ".") {
  S[y][x] = "#";
} else {
  S[y][x] = ".";
}
//上下隣接
for (let i = -1; i <= 1; i += 2) {
    if (0 <= y + i && y + i < H) { //マップ内  
      if (S[y + i][x] === ".") {
        S[y + i][x] = "#";
      } else {
        S[y + i][x] = ".";
      }
    }  
}
//左右隣接
for (let j = -1; j <= 1; j += 2) {
  if (0 <= x + j && x + j < W) { //マップ内
    if (S[y][x + j] === ".") {
      S[y][x + j] = "#";
    } else {
      S[y][x + j] = ".";
    }
  }  
}
console.log(S.map(row => row.join("")).join(("\n")));
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?