# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
284364 | antontsiorvas | Awesome Arrowland Adventure (eJOI19_adventure) | C++14 | 2097 ms | 1792 KiB |
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 <cstdio>
int rows, cols, dist[505][505], cnt, point[2];
bool visited[505][505];
char data[505][505];
int find_rots(char a, char b){
if(a == 'N'){
if(b == 'N') return 0;
if(b == 'E') return 1;
if(b == 'S') return 2;
if(b == 'W') return 3;
}
if(a == 'E'){
if(b == 'N') return 3;
if(b == 'E') return 0;
if(b == 'S') return 1;
if(b == 'W') return 2;
}
if(a == 'S'){
if(b == 'N') return 2;
if(b == 'E') return 3;
if(b == 'S') return 0;
if(b == 'W') return 1;
}
if(a == 'W'){
if(b == 'N') return 1;
if(b == 'E') return 2;
if(b == 'S') return 3;
if(b == 'W') return 0;
}
return 0;
}
void find_min(){
int minimum = 2099999999;
point[0] = point[1] = -1;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(!visited[i][j] && minimum > dist[i][j]){
point[0] = i;
point[1] = j;
minimum = dist[i][j];
}
}
}
}
bool isWithinBounds(int r, int c){ return 0 <= r && r < rows && 0 <= c && c < cols; }
int dijkstra(){
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
dist[i][j] = 2099999999;
if(data[i][j] == 'X') visited[i][j] = true;
}
}
dist[0][0] = 0;
int points = rows*cols, temp;
while(cnt < points && !visited[rows-1][cols-1]){
find_min();
if(point[0] == -1) return 2099999999;
int row = point[0], col = point[1];
visited[row][col] = true;
cnt++;
temp = dist[row][col]+find_rots(data[row][col],'N');
if(isWithinBounds(row-1,col) && !visited[row-1][col] && temp < dist[row-1][col]){
dist[row-1][col] = temp;
}
temp = dist[row][col]+find_rots(data[row][col],'S');
if(isWithinBounds(row+1,col) && !visited[row+1][col] && temp < dist[row+1][col]){
dist[row+1][col] = temp;
}
temp = dist[row][col]+find_rots(data[row][col],'W');
if(isWithinBounds(row,col-1) && !visited[row][col-1] && temp < dist[row][col-1]){
dist[row][col-1] = temp;
}
temp = dist[row][col]+find_rots(data[row][col],'E');
if(isWithinBounds(row,col+1) && !visited[row][col+1] && temp < dist[row][col+1]){
dist[row][col+1] = temp;
}
}
return dist[rows-1][cols-1];
}
int main(){
scanf("%d%d",&rows,&cols);
for(int i=0; i<rows; i++) scanf("\n%s",data[i]);
data[rows-1][cols-1] = 'F';
int ans = dijkstra();
if(ans == 2099999999) printf("-1");
else printf("%d",ans);
}
Compilation message (stderr)
# | 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... |