Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cmath>
- #include <map>
- using namespace std;
- int main() {
- int T;
- cin >> T;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- while(T--) {
- int n, m;
- cin >> n >> m;
- char mat[n][m];
- int si, sj, ei, ej;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 'S') {
- si = i;
- sj = j;
- }
- if(mat[i][j] == 'E') {
- ei = i;
- ej = j;
- }
- }
- }
- vector<vector<bool>> visited(n, vector<bool>(m, false));
- queue<int> q;
- q.push(ei);
- q.push(ej);
- q.push(0);
- int distance_from_S_to_E = -1;
- vector<pair<int, int>> wizards;
- while(!q.empty()) {
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(ci == si and cj == sj) {
- distance_from_S_to_E = dist;
- }
- if(isdigit(mat[ci][cj])) {
- wizards.push_back(make_pair(dist, mat[ci][cj] - '0'));
- }
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] != 'T') {
- q.push(ti);
- q.push(tj);
- q.push(dist + 1);
- visited[ti][tj] = true;
- }
- }
- }
- int health = 100;
- for(int i = 0; i < wizards.size(); i++) {
- if(wizards[i].first <= distance_from_S_to_E) {
- health -= wizards[i].second;
- }
- }
- cout << max(0, health) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement