4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

電脳少女プログラミング2088 -「思い出の屋上」のコードレビューの修正をChatGPTにお願いしてみた

Last updated at Posted at 2025-02-01

はじめに

Sランク問題『思い出の屋上』のコードについて、レイミさんから受けたレビューをもとに、修正を ChatGPT に行わせてみたいと思います。

初回提出

提出コード

以下のコードを提出します。シンプルなコードになっていると思います。

use std::collections::BinaryHeap;

const D4: [(usize, usize); 4] = [(1, 0), (0, 1), (!0, 0), (0, !0)];
fn main() {
    let (H, W, M) = get!(usize, usize, usize);
    let mut que = BinaryHeap::new();
    for _i in 0..M {
        let (r, c, s) = get!(usize, usize, i32);
        que.push((s, r - 1, c - 1));
    }
    let mut T = vec![vec![-1e9 as i32; W]; H];
    while let Some((s, r, c)) = que.pop() {
        if T[r][c] > s {
            continue;
        }
        for d in D4 {
            let nr = r.wrapping_add(d.0);
            let nc = c.wrapping_add(d.1);
            if nr < H && nc < W && T[nr][nc] < s - 1 {
                T[nr][nc] = s - 1;
                que.push((s - 1, nr, nc));
            }
        }
    }
    let mut ans = -1;
    for r in 0..H {
        for c in 0..W {
            ans = ans.max(-T[r][c] - 1);
        }
    }
    println!("{}", ans);
}

#[macro_export]
macro_rules! get {
      ($t:ty) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.trim().parse::<$t>().unwrap()
          }
      };
      ($($t:ty),*) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              let mut iter = line.split_whitespace();
              (
                  $(iter.next().unwrap().parse::<$t>().unwrap(),)*
              )
          }
      };
      ($t:ty; $n:expr) => {
          (0..$n).map(|_|
              get!($t)
          ).collect::<Vec<_>>()
      };
      ($($t:ty),*; $n:expr) => {
          (0..$n).map(|_|
              get!($($t),*)
          ).collect::<Vec<_>>()
      };
      ($t:ty ;;) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.split_whitespace()
                  .map(|t| t.parse::<$t>().unwrap())
                  .collect::<Vec<_>>()
          }
      };
      ($t:ty ;; $n:expr) => {
          (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
      };
}

レイミちゃんのコメント

reimi01.png

あなた酷いことをおっしゃる

ChatGPT に修正をお願いする

ChatGPT に以下のメッセージを送信しました。

上のコードにレビューを受けました。指摘に従い修正してください。

<レビュー内容>

ふん、なかなか興味深いアプローチだな。BinaryHeap を使って効率的に処理している点は評価できる。
しかし、コードの可読性には改善の余地がある。コメントを追加し、変数名をもっと明確にしろ。
そうすれば、他の者も理解しやすくなるはずだ。
今のままでは、貴様以外誰も理解できないゴミクズのようなコードだ。さっさと直せ!

ChatGPT から生成されたコード

use std::collections::BinaryHeap;

// 4方向の移動を表す (下、右、上、左)
const DIRECTION_4: [(usize, usize); 4] = [(1, 0), (0, 1), (!0, 0), (0, !0)];

fn main() {
    // 入力を取得 (H: 高さ, W: 幅, M: 特殊セルの数)
    let (height, width, num_special) = get!(usize, usize, usize);

    // 優先度付きキュー (BinaryHeap) を使用して、BFS の逆の要領で処理
    let mut heap: BinaryHeap<(i32, usize, usize)> = BinaryHeap::new();

    for _ in 0..num_special {
        let (row, col, strength) = get!(usize, usize, i32);
        heap.push((strength, row - 1, col - 1)); // 0-indexed に変換
    }

    // 各セルの最大影響値を格納するグリッドを初期化
    let mut grid = vec![vec![-1_000_000_000; width]; height];

    // 優先度付きキューを用いた探索 (最大値から処理)
    while let Some((strength, row, col)) = heap.pop() {
        // すでに強い影響を受けた場合はスキップ
        if grid[row][col] > strength {
            continue;
        }

        // 4方向に拡散
        for &(dr, dc) in &DIRECTION_4 {
            let new_row = row.wrapping_add(dr);
            let new_col = col.wrapping_add(dc);

            // 範囲内かつ、より強い影響を与えられる場合は更新
            if new_row < height && new_col < width && grid[new_row][new_col] < strength - 1 {
                grid[new_row][new_col] = strength - 1;
                heap.push((strength - 1, new_row, new_col));
            }
        }
    }

    // 影響の最小値を求める
    let mut max_distance = -1;
    for row in 0..height {
        for col in 0..width {
            max_distance = max_distance.max(-grid[row][col] - 1);
        }
    }

    println!("{}", max_distance);
}

// 標準入力から値を取得するマクロ
#[macro_export]
macro_rules! get {
      ($t:ty) => {
          {
              let mut line = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.trim().parse::<$t>().unwrap()
          }
      };
      ($($t:ty),*) => {
          {
              let mut line = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              let mut iter = line.split_whitespace();
              (
                  $(iter.next().unwrap().parse::<$t>().unwrap(),)*
              )
          }
      };
      ($t:ty; $n:expr) => {
          (0..$n).map(|_| get!($t)).collect::<Vec<_>>()
      };
      ($($t:ty),*; $n:expr) => {
          (0..$n).map(|_| get!($($t),*)).collect::<Vec<_>>()
      };
      ($t:ty ;;) => {
          {
              let mut line = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.split_whitespace()
                  .map(|t| t.parse::<$t>().unwrap())
                  .collect::<Vec<_>>()
          }
      };
      ($t:ty ;; $n:expr) => {
          (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
      };
}

image.png

これ、レイミさんのコメント伝える必要あったか?

再提出

ChatGPT で生成したコードを再度提出してみます。

再提出後のレイミちゃんのコメント

reimi02.png

無事、ご納得いただけたようです

おわりに

今回は ChatGPT に問題文を教えなかったのですが、コードを良い感じに理解し、コメントや変数名をつけてくれていることに驚きました。
私は変数名を考えるのが苦手なので、適当にコードを書いた後 ChatGPT に修正を依頼する形で使っていこうと思いました。

みなさんもドSなレイミちゃんのレビューや、イヤな上司のネチネチコードレビューの修正を ChatGPT に投げてみるのはいかがでしょうか?


おまけ

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?