|
| 1 | +#include<iostream> |
| 2 | +#include<queue> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <ctype.h> |
| 6 | + |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +struct Tparent{ |
| 11 | + int i,j; |
| 12 | + Tparent(int i, int j) : i(i), j(j) {} |
| 13 | + Tparent(){}; |
| 14 | + bool operator == (const Tparent &uni){ |
| 15 | + return(i==uni.i && j==uni.j); |
| 16 | + } |
| 17 | + |
| 18 | +}; |
| 19 | + |
| 20 | +struct Tpuerta{ |
| 21 | + int i,j; |
| 22 | + double prob; |
| 23 | + Tpuerta(int i, int j, double prob) : i(i), j(j), prob(prob) {} |
| 24 | + Tpuerta(int i, int j) : i(i), j(j), prob(0) {} |
| 25 | + Tpuerta(){}; |
| 26 | + bool operator == (const Tpuerta &uni){ |
| 27 | + return(i==uni.i && j==uni.j); |
| 28 | + } |
| 29 | + |
| 30 | +}; |
| 31 | + |
| 32 | +char grid[20][20]; |
| 33 | +bool visited[20][20]; |
| 34 | +Tparent parent[20][20]; |
| 35 | +Tpuerta puertas[10]; |
| 36 | +int arr[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1,}; |
| 37 | +int n; |
| 38 | +int steps=0; |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +void bfs(int start_i, int start_j){ |
| 43 | + queue <Tparent> q; |
| 44 | + q.push(Tparent(start_i,start_j)); |
| 45 | + visited[start_i][start_j] = true; |
| 46 | + |
| 47 | + while(!q.empty()){ |
| 48 | + Tparent v = q.front(); |
| 49 | + q.pop(); |
| 50 | + for(int k=0; k < 4; k++){ |
| 51 | + int ti = v.i + arr[k][0]; |
| 52 | + int tj = v.j + arr[k][1]; |
| 53 | + |
| 54 | + if(ti>=0 && ti<n && tj>=0 && tj<n){ |
| 55 | + if(!visited[ti][tj]){ |
| 56 | + visited[ti][tj] = true; |
| 57 | + if(grid[ti][tj]=='.'){ |
| 58 | + q.push(Tparent(ti,tj)); |
| 59 | + parent[ti][tj]= Tparent(v); |
| 60 | + //cout<<v.i<<" "<<v.j<<" is parent of "<< ti << " "<<tj<<endl; |
| 61 | + }else if(isdigit(grid[ti][tj])){ |
| 62 | + parent[ti][tj]= Tparent(v); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +void shortest_path(Tparent start, Tparent end){ |
| 73 | + if(start == end || end == Tparent(-1,-1)){ |
| 74 | + }else{ |
| 75 | + steps++; |
| 76 | + shortest_path(start, parent[end.i][end.j]); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +int main(){ |
| 83 | + int cases; |
| 84 | + int nPuerta; |
| 85 | + int bomberX; |
| 86 | + int bomberY; |
| 87 | + |
| 88 | + cin>>cases; |
| 89 | + while(cases--){ |
| 90 | + cin>>n; |
| 91 | + for(int i=0;i<n;i++){ |
| 92 | + for(int j=0;j<n;j++){ |
| 93 | + char c; |
| 94 | + cin>>c; |
| 95 | + if(c=='B'){ |
| 96 | + bomberX=i; |
| 97 | + bomberY=j; |
| 98 | + }else if(isdigit(c)){ |
| 99 | + int puert= c - '0'; |
| 100 | + //cout<<puert<<" "; |
| 101 | + puertas[puert]=Tpuerta(i,j); |
| 102 | + } |
| 103 | + grid[i][j] = c; |
| 104 | + visited[i][j]=false; |
| 105 | + parent[i][j]=Tparent(-1,-1); |
| 106 | + } |
| 107 | + } |
| 108 | + bfs(bomberX,bomberY); |
| 109 | + |
| 110 | + cin>>nPuerta; |
| 111 | + for(int i=0;i<nPuerta;i++){ |
| 112 | + cin>>puertas[i].prob; |
| 113 | + } |
| 114 | + double sum=0; |
| 115 | + for(int i=0; i<nPuerta;i++){ |
| 116 | + steps=0; |
| 117 | + shortest_path(Tparent (bomberX,bomberY), Tparent (puertas[i].i,puertas[i].j)); |
| 118 | + sum+=(double)(steps * (puertas[i].prob * 0.01)); |
| 119 | + } |
| 120 | + cout.precision(2); |
| 121 | + cout<<fixed<<sum<<endl; |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
0 commit comments