Submission #854758

#TimeUsernameProblemLanguageResultExecution timeMemory
854758cj03Tracks in the Snow (BOI13_tracks)C++14
82.50 / 100
1680 ms1048576 KiB
#include <bits/stdc++.h>
using namespace std;

int a[4005][4005];
vector<pair<pair<int,int>, int>> adj[4005][4005];
int d[4005][4005];

int main() {
    ios::sync_with_stdio(0); cin.tie(0); 
 
    int h, w; cin >> h >> w;
    memset(d, -1, sizeof(d));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        char c; cin >> c;
        if (c=='.') a[i][j]=1;
        if (c=='R') a[i][j]=2;
        if (c=='F') a[i][j] = 3;
      }
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        int x = a[i][j]; if (x==1) continue;
        if (i==h && j==w) continue;
        if (i+1 <= h && a[i+1][j]!=1) adj[i][j].push_back({{i+1, j}, (x==5-a[i+1][j])});
        if (i-1 >= 1 && a[i-1][j]!=1) adj[i][j].push_back({{i-1, j}, (x==5-a[i-1][j])});
        if (j+1 <= w && a[i][j+1]!=1) adj[i][j].push_back({{i, j+1}, (x==5-a[i][j+1])});
        if (j-1 >= 1 && a[i][j-1] != 1) adj[i][j].push_back({{i, j-1}, (x==5-a[i][j-1])});
      }
    }
    d[1][1] = 1;
    deque<pair<int,int>> q;
    q.push_back({1, 1});
    while (!q.empty()) {
      int i = q.front().first, j=q.front().second; 
      q.pop_front();
      for (auto p : adj[i][j]) {
        int x= p.first.first, y=p.first.second, c = p.second;
        if (d[x][y]==-1 || d[x][y] > d[i][j]+c) {
          d[x][y] = d[i][j]+c;
          if (c==0) {
            q.push_front({x, y});
          } else {
            q.push_back({x, y});
          }
        }
      }
    }
    
    int ans = 1;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        ans = max(ans, d[i][j]);
      }
    }
    cout << ans << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...