Submission #72575063


Source Code Expand

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

int main(void){
  vector<int> vx = {-1,0,1,0};
  vector<int> vy = {0,-1,0,1};
  int h,w;
  cin >> h >> w;
  vector<string> s(h);
  for(int i=0;i<h;i++){
    cin >> s.at(i);
  }
  vector<vector<int>> seen(h,vector<int>(w, 1e8));
  seen.at(0).at(0) = 0;
  queue<pair<int,int>> q;
  q.push({0,0});
  while(!q.empty()){
    auto p = q.front();
    q.pop();
    for(int i=0;i<4;i++){
      int x = p.first + vx.at(i);
      int y = p.second + vy.at(i);
      if(x >= 0 && x < h && y >= 0 && y < w && seen.at(x).at(y) > seen.at(p.first).at(p.second) + 1 && s.at(x).at(y) != s.at(p.first).at(p.second)){
        seen.at(x).at(y) = seen.at(p.first).at(p.second) + 1;
        q.push({x,y});
      }
    }
  }
  // for(int i=0;i<h;i++){
  //   for(int j=0;j<w;j++){
  //     cout << seen.at(i).at(j) << ' ';
  //   }
  //   cout << endl;
  // }
  if(seen.at(h-1).at(w-1) == 1e8){
    cout << -1 << endl;
  }else{
    cout << seen.at(h-1).at(w-1) << endl;
  }
  return 0;
}

Submission Info

Submission Time
Task F - EGFパス
User guild2026_207
Language C++23 (GCC 15.2.0)
Score 100
Code Size 1099 Byte
Status AC
Exec Time 60 ms
Memory 9576 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 25
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3660 KiB
00_sample_01.txt AC 1 ms 3660 KiB
00_sample_02.txt AC 1 ms 3452 KiB
01_random_00.txt AC 60 ms 9436 KiB
01_random_01.txt AC 43 ms 7720 KiB
01_random_02.txt AC 5 ms 4200 KiB
01_random_03.txt AC 18 ms 5288 KiB
01_random_04.txt AC 59 ms 9464 KiB
01_random_05.txt AC 58 ms 9548 KiB
01_random_06.txt AC 59 ms 9516 KiB
01_random_07.txt AC 16 ms 9488 KiB
01_random_08.txt AC 58 ms 9516 KiB
01_random_09.txt AC 16 ms 9436 KiB
01_random_10.txt AC 44 ms 9488 KiB
01_random_11.txt AC 45 ms 9576 KiB
01_random_12.txt AC 59 ms 9576 KiB
01_random_13.txt AC 59 ms 9576 KiB
01_random_14.txt AC 7 ms 4328 KiB
01_random_15.txt AC 28 ms 6476 KiB
01_random_16.txt AC 51 ms 8720 KiB
01_random_17.txt AC 43 ms 7976 KiB
01_random_18.txt AC 2 ms 3744 KiB
01_random_19.txt AC 59 ms 9576 KiB
01_random_20.txt AC 36 ms 7144 KiB
01_random_21.txt AC 10 ms 4428 KiB