This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
map<char, vector<vector<int>>> dir;
int n, m;
bool check(int x, int y){
if (x >= 0 && x < n && y >= 0 && y < m){
return true;
}
return false;
}
void init(){
dir['N'] = {{0, -1, 0},{1, 0, 1}, {0, 1, 2}, {-1, 0, 3}};
dir['E'] = {{0, -1, 3},{1, 0, 0}, {0, 1, 1}, {-1, 0, 2}};
dir['S'] = {{0, -1, 2},{1, 0, 3}, {0, 1, 0}, {-1, 0, 1}};
dir['W'] = {{0, -1, 1},{1, 0, 2}, {0, 1, 3}, {-1, 0, 0}};
dir['X'] = {};
}
int bfs(vector<vector<char>> ma, vector<vector<int>> costs){
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
q.push({0, 0, 0});
costs[0][0] = 0;
vector<vector<int>> us(m, vector<int>(n, 0));
while(!q.empty()){
vector<int> u = q.top();
q.pop();
vector<vector<int>> moves = dir[ma[u[2]][u[1]]];
for (auto el : moves){
int x = el[0] + u[1], y = el[1] + u[2];
if (check(x, y) && costs[y][x] > costs[u[2]][u[1]] + el[2]){
costs[y][x] = costs[u[2]][u[1]] + el[2];
if (us[y][x] == 0){
q.push({costs[y][x], x, y});
}
us[y][x];
}
}
}
return costs[m - 1][n - 1];
}
int main(){
cin>>m>>n;
vector<vector<char>> ma;
vector<vector<int>> costs;
char r;
for (int i = 0; i < m; i++){
ma.push_back({});
costs.push_back({});
for (int j = 0; j < n; j++){
cin>>r;
ma[i].push_back(r);
costs[i].push_back(INF);
}
}
init();
int res = bfs(ma, costs);
if (res == INF){
cout<<-1;
}
else{
cout<<res;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |