#include "iostream"<br />#include "queue"</p><p>using namespace std;</p><p>typedef struct Pos{</p><p> int x;<br /> int y;<br /> int step;<br />};</p><p>Pos start,temp,t,rout[100];<br />int cunt,col,row,delta[4][2]={{-1,0},{0,1},{1,0},{0,-1}};<br />char map[15][15];<br />bool visit[15][15];<br />double M,R;</p><p>int Check(int c)<br />{<br /> // for(int i=0;i<c;i++)<br /> // printf("%d %d/n",rout[i].x,rout[i].y);</p><p> for(int i=0;i<c;i++)<br /> if(map[rout[i].x][rout[i].y]=='M')<br /> return true;<br /> return false;<br />} </p><p>void Dfs(int x1,int y1,int cunt,int c)<br />{</p><p> // printf("%d %d %d %d/n",x1,y1,cunt,c);<br /> if(cunt==0&&map[x1][y1]=='S')<br /> {</p><p> R++;<br /> if(Check(c))<br /> M++;<br /> //printf("/n");<br /> }<br /> if(cunt>0)<br /> {<br /> visit[x1][y1]=true;<br /> for(int i=0;i<4;i++)<br /> {<br /> int x2=x1+delta[i][0];<br /> int y2=y1+delta[i][1];<br /> if(!visit[x2][y2]&&x2>=0&&x2<col&&y2>=0&&y2<row&&map[x2][y2]!='#')<br /> {<br /> rout[c].x=x2;<br /> rout[c].y=y2;<br /> Dfs(x2,y2,cunt-1,c+1);<br /> }<br /> }<br /> }<br /> visit[x1][y1]=false;</p><p>}</p><p>void Bfs()<br />{</p><p> queue<Pos> q;<br /> visit[start.x][start.y]=true;<br /> start.step=0;<br /> q.push(start);<br /> while(!q.empty())<br /> {<br /> temp=q.front();<br /> q.pop();<br /> //printf("%d %d %d %d %d/n",temp.x,temp.y,temp.step,temp.r,pre[temp.r].r);<br /> if(map[temp.x][temp.y]=='T')<br /> {<br /> // printf("%d/n",temp.step);<br /> memset(visit,false,sizeof(visit));<br /> rout[0].x=temp.x;<br /> rout[0].y=temp.y;<br /> Dfs(temp.x,temp.y,temp.step,1);<br /> break;<br /> }<br /> for(int i=0;i<4;i++)<br /> {<br /> t.x=temp.x+delta[i][0];<br /> t.y=temp.y+delta[i][1];<br /> if(t.x>=0&&t.y>=0&&t.x<col&&t.y<row&&!visit[t.x][t.y]&&map[t.x][t.y]!='#')<br /> {<br /> t.step=temp.step+1;<br /> visit[t.x][t.y]=true;<br /> q.push(t);<br /> }<br /> }<br /> }<br />} </p><p>int main()<br />{</p><p> freopen("data.in","r",stdin);<br /> int test,cas=1;<br /> scanf("%d",&test);<br /> while(test--)<br /> {<br /> M=0;R=0;<br /> scanf("%d%d",&col,&row);<br /> memset(visit,false,sizeof(visit));<br /> getchar();<br /> for(int i=0;i<col;i++)<br /> {<br /> gets(map[i]);<br /> for(int j=0;j<row;j++)<br /> if(map[i][j]=='S')<br /> {<br /> start.x=i;<br /> start.y=j;<br /> }</p><p> }<br /> Bfs();<br /> // printf("%lf %lf/n",R,M);<br /> printf("Mission #%d:/n",cas++);<br /> if(R!=M)<br /> {<br /> printf("The probability for the spy to get to the telegraph transmitter is ");<br /> printf("%.2lf",100*((R-M)/R));<br /> cout<<"%."<<endl<<endl;<br /> }<br /> else<br /> printf("Mission Impossible./n/n");<br /> } </p><p> while(1);<br /> return 0;<br />} </p><p>