Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- char x[1005][1005];
- int visted[1005][1005];
- int y[1005][1005];
- int main()
- {
- int di[]={0,1,-1,0};
- int dj[]={1,0,0,-1};
- int t;
- cin >> t;
- for(int T = 0; T < t; T++) {
- int n,m;
- cin>>n>>m;
- for(int i = 0;i<n;i++){
- for(int j = 0;j<m;j++){
- cin>>x[i][j];
- }
- }
- for(int i = 0; i < 1004; i++) {
- for(int j = 0; j < 1004; j++) {
- visted[i][j] = false;
- y[i][j] = 2e9;
- }
- }
- int si = 0;
- int sj = 0;
- int ei = 0;
- int ej = 0;
- for(int i = 0;i<n;i++){
- for(int j = 0;j<m;j++){
- if(x[i][j]=='E'){
- ei=i;
- ej=j;
- }
- if(x[i][j]=='S'){
- si=i;
- sj=j;
- }
- }
- }
- int cekori = 0;
- queue<int>bfs;
- bfs.push(ei);
- bfs.push(ej);
- bfs.push(cekori);
- while(!bfs.empty()){
- int ci=bfs.front();
- bfs.pop();
- int cj=bfs.front();
- bfs.pop();
- int cekor=bfs.front();
- bfs.pop();
- y[ci][cj]=cekor;
- for(int i=0;i<4;i++){
- int ti=ci+di[i];
- int tj=cj+dj[i];
- if(ti>=0 && ti<n && tj>=0 && tj<m && x[ti][tj]!='T' && visted[ti][tj]!=true){
- visted[ti][tj]=true;
- bfs.push(ti);
- bfs.push(tj);
- bfs.push(cekor+1);
- }
- }
- }
- int dist = y[si][sj];
- int dist2=0;
- int cnt=0;
- int hu=100;
- for(int i = 0;i<n;i++){
- for(int j = 0;j<m;j++){
- if(x[i][j] >= '1' && x[i][j]<='9'){
- dist2=y[i][j];
- if(dist2 <= dist) {
- int c = x[i][j] - '0';
- hu -= c;
- }
- }
- }
- }
- if(hu<0){
- cout<<0<<endl;
- }else{
- cout<<hu<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement