Advertisement
Josif_tepe

Untitled

Feb 6th, 2024
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <fstream>
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn = 1e5 + 10;
  9. const int INF = 2e9 + 10;
  10. const ll inf = 1e18 + 10;
  11. int n, m;
  12. char mat[1001][1001];
  13. int di[] = {-1, 1, 0, 0};
  14. int dj[] = {0, 0, -1, 1};
  15. int dist[1001][1001];
  16. int bfs(int si, int sj) {
  17.     vector<vector<bool>> visited(n, vector<bool>(m, false));
  18.     visited[si][sj] = true;
  19.     queue<int> q;
  20.     q.push(si);
  21.     q.push(sj);
  22.     q.push(0);
  23.     while(!q.empty()) {
  24.         int ti = q.front(); q.pop();
  25.         int tj = q.front(); q.pop();
  26.         int distance = q.front(); q.pop();
  27.         dist[ti][tj] = distance;
  28.         for(int it = 0; it < 4; ++it) {
  29.             int ni = ti + di[it];
  30.             int nj = tj + dj[it];
  31.             if(ni < 0 or nj < 0 or ni >= n or nj >= m) continue;
  32.             if(visited[ni][nj]) continue;
  33.             if(mat[ni][nj] == 'T') continue;
  34.             visited[ni][nj] = true;
  35.             q.push(ni);
  36.             q.push(nj);
  37.             q.push(distance + 1);
  38.         }
  39.          
  40.     }
  41.     return 0;
  42. }
  43. int main() {
  44.     ios_base::sync_with_stdio(false);
  45. //    ifstream cin("in.txt");
  46.     int t;
  47.     cin >> t;
  48.     for(int T = 0; T < t; ++T) {
  49.         cin >> n >> m;
  50.         int si, sj; // starting position
  51.         int ei, ej; // ending position
  52.         for(int i = 0; i < n; ++i) {
  53.             for(int j = 0; j < m; ++j) {
  54.                 cin >> mat[i][j];
  55.                 if(mat[i][j] == 'S') {
  56.                     si = i;
  57.                     sj = j;
  58.                 }
  59.                 if(mat[i][j] == 'E') {
  60.                     ei = i;
  61.                     ej = j;
  62.                 }
  63.             }
  64.         }
  65.         for(int i = 0; i < n; ++i) {
  66.             for(int j = 0; j < m; ++j) {
  67.                 dist[i][j] = INF;
  68.             }
  69.         }
  70.         bfs(ei, ej);
  71.         int health = 100;
  72.         for(int i = 0; i < n; ++i) {
  73.             for(int j = 0; j < m; ++j) {
  74.                 if(mat[i][j] >= '0' and mat[i][j] <= '9') {
  75.                     if(dist[i][j] <= dist[si][sj]) {
  76.                         health -= (mat[i][j] - '0');
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         cout << max(health, 0) << endl;
  82.     }
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement