Submission #284364

#TimeUsernameProblemLanguageResultExecution timeMemory
284364antontsiorvasAwesome Arrowland Adventure (eJOI19_adventure)C++14
50 / 100
2097 ms1792 KiB
#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)

adventure.cpp: In function 'int main()':
adventure.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |  scanf("%d%d",&rows,&cols);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
adventure.cpp:88:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   88 |  for(int i=0; i<rows; i++) scanf("\n%s",data[i]);
      |                            ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...