• zoj 2081 BFS 延迟标记 读入问题


    Mission Impossible

    Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                            

                Now a spy is besieged in a maze. He knows that there is a telegraph transmitter  in the maze somewhere. So he must get there to use the telegraph transmitter to  ask for help. 

    But the mission is very dangerous, because there are some mines in the map   (see the figure above). When the spy steps on it, it will immediately explode   and the mission will fail. The spy doesn't know where the mines are, thus he   will use his best strategy to get to the destination (he will always go along   the shortest path). There may be several shortest paths for the spy to choose,   and the probability to choose each path from the start point to the destination   is the same.

    Now your task is to write a program to find the probability that the spy can   get to the telegraph transmitter.

    Input

    The input file begins with an integer T, indicating the number of test cases.   Each test case begins with two integers N, M, indicating the height and width   of the maze. (N <= 10, M <= 10) In the following N lines, each line contains   M characters describing the map. There is one blank line after each map. Spaces   denotes empty square, '#' denotes a wall, 'S' denotes the spy, 'M' denotes a   mine, and 'T' denotes the telegraph transmitter. It's guaranteed that the four   sides of the map are all walls.

      Output

    For each maze, first output the number of the test case (`Mission #1:', ` Mission   #2:', etc.) in a line of its own.

    If it is possible for the spy to get to the telegraph transmitter, print a   line containing the probability that the spy can get to the telegraph transmitter,   exact to two digit to the right of the decimal point. Adhere to the output format   shown in the sample below.

    If the spy can't get to the destination, output a line containing the statement   `Mission Impossible.'
      Output a blank line after each test case.

    Sample Input

    2   6 10   ##########   # M   T  #   #  ###   #   #  ###   #   # S      #   ##########

    6 10   ##########   # M  T   #   #  ###   #   #  ###   #   # S      #   ##########

    Sample Output

    Mission #1:   The probability for the spy to get to the telegraph transmitter is 50.00%.

    Mission #2:   Mission Impossible.


                                Author: YE, Kai                                         Source: ZOJ Monthly, February 2004

    题意:间谍只走最短路,而这些路上可能埋有炸弹,请问间谍安全到达目的地的可能性多少。

    题解:BFS记录最短路径个数以及这些路径中踩到炸弹的路径的个数,最后换算个百分比就行了。

    注意需要延迟标记,还有图中有空格,读入不能用scanf。

      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 
     10 #define N 101
     11 #define M 15
     12 #define mod 1000000007
     13 #define mod2 100000000
     14 #define ll long long
     15 #define maxi(a,b) (a)>(b)? (a) : (b)
     16 #define mini(a,b) (a)<(b)? (a) : (b)
     17 
     18 using namespace std;
     19 
     20 int T;
     21 char s[N][N];
     22 int vis[N][N];
     23 int cc,tot;
     24 int n,m;
     25 int dirx[]={1,-1,0,0};
     26 int diry[]={0,0,1,-1};
     27 
     28 typedef struct
     29 {
     30     int x;
     31     int y;
     32     int now;
     33     int boom;
     34 }PP;
     35 
     36 PP start,end;
     37 
     38 int ok(PP next)
     39 {
     40     if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && vis[next.x][next.y]==0 && s[next.x][next.y]!='#'){
     41         if(s[next.x][next.y]=='M')
     42             next.boom=1;
     43         return next.boom;
     44     }
     45     return 0;
     46 }
     47 
     48 void BFS()
     49 {
     50     queue<PP> q;
     51     q.push(start);
     52     int first=-1;
     53     PP te,next;
     54     int i;
     55     int re;
     56     while(q.size()!=0)
     57     {
     58         te=q.front();
     59         vis[te.x][te.y]=1;
     60         q.pop();
     61        // printf(" %d %d
    ",te.x,te.y);
     62         for(i=0;i<4;i++){
     63             next.x=te.x+dirx[i];
     64             next.y=te.y+diry[i];
     65             next.now=te.now+1;
     66             next.boom=te.boom;
     67             re=ok(next);
     68             next.boom=re;
     69             if(re==0) continue;
     70             if(next.x==end.x && next.y==end.y)
     71             {
     72               //  printf(" %d %d first=%d now=%d %d
    ",next.x,next.y,first,next.now,next.boom);
     73                 if(first==-1) first=next.now;
     74                 else{
     75                     if(next.now!=first) return;
     76                 }
     77                 tot++;
     78                 if(next.boom==2) cc++;
     79             }
     80             else{
     81                 q.push(next);
     82             }
     83         }
     84     }
     85 }
     86 
     87 int main()
     88 {
     89     int i,j;
     90     //freopen("data.in","r",stdin);
     91     scanf("%d",&T);
     92     getchar();
     93     for(int cnt=1;cnt<=T;cnt++)
     94     //while(T--)
     95     //while(scanf("%d%d",&n,&q)!=EOF)
     96     {
     97         //q.clear();
     98         cc=tot=0;
     99         memset(vis,0,sizeof(vis));
    100         scanf("%d%d",&n,&m);
    101         getchar();
    102         for(i=0;i<n;i++){
    103             gets(s[i]);
    104         }
    105         for(i=0;i<n;i++){
    106             for(j=0;j<m;j++){
    107                 if(s[i][j]=='S'){
    108                     start.x=i;
    109                     start.y=j;
    110                     start.boom=2;
    111                     start.now=0;
    112                 }
    113                 if(s[i][j]=='T'){
    114                     end.x=i;
    115                     end.y=j;
    116                 }
    117             }
    118         }
    119         //printf("%d %d %d %d
    ",start.x,start.y,end.x,end.y);
    120         BFS();
    121         printf("Mission #%d:
    ",cnt);
    122         if(cc==0){
    123             printf("Mission Impossible.
    ");
    124         }
    125         else{
    126             printf("The probability for the spy to get to the telegraph transmitter is %.2f%%.
    ",(1.0)*100*cc/tot);
    127         }
    128          printf("
    ");
    129 
    130     }
    131 
    132     return 0;
    133 }
  • 相关阅读:
    hdoj 5311 Hidden String(KMP)
    hdoj 1532 Drainage Ditches(最大网络流)
    ubuntu中php+mysql环境搭建
    android:clipToPadding 和 android:clipChildren
    Js实现动画框架
    JavaScript初步认识
    JavaScript鼠标进入与退出监听动画
    Android框架集合
    Android 6.0(Android M)动态授权
    Android应用申请ROOT权限
  • 原文地址:https://www.cnblogs.com/njczy2010/p/3917599.html
Copyright © 2020-2023  润新知