Advertisement
Infernale

Escape! If you want to survive [NCTU Floor 13]

Dec 18th, 2019
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int row, col;
  7.  
  8. bool validPosition(int n, int m){
  9.     if(n < 0 || n == row || m < 0 || m == col)
  10.         return false;
  11.     return true;
  12. }
  13.  
  14. void fireSpread(vector <string> &map){
  15.     vector <string> modified = map;
  16.     for(int i = 0; i < row; i++){
  17.         for(int j = 0; j < col; j++){
  18.             if(map[i][j] == 'F'){
  19.                 if(validPosition(i - 1, j))
  20.                     modified[i - 1][j] = 'F';
  21.                 if(validPosition(i + 1, j))
  22.                     modified[i + 1][j] = 'F';
  23.                 if(validPosition(i, j - 1))
  24.                     modified[i][j - 1] = 'F';
  25.                 if(validPosition(i, j + 1))
  26.                     modified[i][j + 1] = 'F';
  27.             }
  28.         }
  29.     }
  30.     map = modified;
  31. }
  32.  
  33. int findMinimum(vector <string> map, int n, int m, vector <vector<int>> dp, vector <vector<bool>> visited){
  34.     if(n == 0 || m == 0 || n == (row - 1) || m == (col - 1))
  35.         return 0;
  36.     if(dp[n][m] != -1)
  37.         return dp[n][m];
  38.     visited[n][m] = true;
  39.  
  40.     int up, down, left, right;
  41.     up = down = left = right = 1e6;
  42.  
  43.     fireSpread(map);
  44.     if(map[n - 1][m] == '.'){
  45.         if(!visited[n - 1][m]){
  46.             up = 1 + findMinimum(map, n - 1, m, dp, visited);
  47.             //cout << "Up!" << endl;
  48.         }
  49.     }
  50.     if(map[n + 1][m] == '.'){
  51.         if(!visited[n + 1][m]){
  52.             down = 1 + findMinimum(map, n + 1, m, dp, visited);
  53.             //cout << "Down!" << endl;
  54.         }
  55.     }
  56.     if(map[n][m - 1] == '.'){
  57.         if(!visited[n][m - 1]){
  58.             left = 1 + findMinimum(map, n, m - 1, dp, visited);
  59.             //cout << "Left!" << endl;
  60.         }
  61.     }
  62.     if(map[n][m + 1] == '.'){
  63.         if(!visited[n][m + 1]){
  64.             right = 1 + findMinimum(map, n, m + 1, dp, visited);
  65.             //cout << "Right!" << endl;
  66.         }
  67.    
  68.     }
  69.  
  70.     dp[n][m] = min(up, min(down, min(left, right)));
  71.     return dp[n][m];
  72. }
  73.  
  74. int solve(vector <string> map){
  75.     int x = -1, y = -1;
  76.     for(int i = 0; i < row && x == -1; i++){
  77.         for(int j = 0; j < col; j++){
  78.             if(map[i][j] == 'J'){
  79.                 x = i;
  80.                 y = j;
  81.                 break;
  82.             }
  83.         }
  84.     }
  85.     vector <vector<int> > dp(row, vector<int>(col, -1));
  86.     vector <vector<bool> > visited(row, vector<bool>(col, false));
  87.  
  88.     int res = findMinimum(map, x, y, dp, visited);
  89.  
  90.     return (res >= 1e6 ? -1 : res);
  91. }
  92.  
  93. int main(){
  94.     cin >> row >> col;
  95.     string str;
  96.     vector <string> map;
  97.     for(int i = 0; i < row; i++){
  98.         cin >> str;
  99.         map.push_back(str);
  100.     }
  101.     int res = 1 + solve(map);
  102.     if(res - 1 == -1)
  103.         cout << "N" << endl;
  104.     else
  105.         cout << res << endl;
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement