제출 #359017

#제출 시각아이디문제언어결과실행 시간메모리
359017betterdanjoeTracks in the Snow (BOI13_tracks)C++11
100 / 100
1528 ms137360 KiB
#include <bits/stdc++.h>
using namespace std;

int moves[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

int main(){
  int n, m;
  cin >> n >> m;
  char arr[n][m];
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      cin >> arr[i][j];
    }
  }
  int num[n][m];
  memset(num, 0, sizeof(num));
  num[0][0] = 1;
  deque<pair<int, int>> q;
  q.push_back({0, 0});
  int ans = 0;
  while(!q.empty()){
    pair<int, int> x = q.front();
    q.pop_front();
    ans = max(num[x.first][x.second], ans);
    for(int i = 0; i < 4; i++){
      int x1 = x.first + moves[i][0], y1 = x.second + moves[i][1];
      if(x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || num[x1][y1] != 0 || arr[x1][y1] == '.') continue;
      if(arr[x1][y1] != arr[x.first][x.second]){
        num[x1][y1] = num[x.first][x.second] + 1;
        q.push_back({x1, y1});
      } else {
        num[x1][y1] = num[x.first][x.second];
        q.push_front({x1, y1});
      }
    }
  }
  cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...