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\n",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\n",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\n",start.x,start.y,end.x,end.y);
120         BFS();
121         printf("Mission #%d:\n",cnt);
122         if(cc==0){
123             printf("Mission Impossible.\n");
124         }
125         else{
126             printf("The probability for the spy to get to the telegraph transmitter is %.2f%%.\n",(1.0)*100*cc/tot);
127         }
128          printf("\n");
129
130     }
131
132     return 0;
133 }

zoj 2081 BFS 延迟标记 读入问题

时间: 2024-10-06 03:12:41

zoj 2081 BFS 延迟标记 读入问题的相关文章

codevs 1082 线段树练习 3 区间更新+延迟标记

题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数, 再接下来一个正整数Q,每行表示操作的个数, 如果第一个数是1,后接3个正整数, 表示在区间[a,b]内每个数增加X,如果是2, 表示操作2询问区间[a,b]的和是多少. pascal选手请不要使用readln读入 输出描述 Output Description 对于每个询问输出一行

[uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)

题目链接:https://vjudge.net/problem/UVA-11992 题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询.查询返回子矩阵和.最小值.最大值 n很小(<=20),所以可以开20棵线段树,每次操作按行更新. 特别小心put和add两个延迟标记,坑老惨了. put初始化-1最简单的坑,略过. build的时候要每一个节点都要clear,不能只clear叶子.因为会有直接差没操作的子矩阵(因为初始化都是0). 数组开大... add的话,什么都不用管

HDU 1698 Just a Hook (区间更新+延迟标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31096    Accepted Submission(s): 15313 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 5785 Interesting manacher + 延迟标记

题意:给你一个串,若里面有两个相邻的没有交集的回文串的话,设为S[i...j] 和 S[j+1...k],对答案的贡献是i*k,就是左端点的值乘上右端点的值. 首先,如果s[x1....j].s[x2....j].s[x3....j]....s[xn....j].是回文串,而且s[j+1...y1].s[j+1...y2].s[j+1...y3].也是回文串,那么这些串对答案的贡献一共就是(x1+x2+...+xn)*(y1+y2+....+yn) 所以想到了用cntL[i]表示:以i这个字符为

Codeforces Round #262 (Div. 2)C(二分答案,延迟标记)

这是最大化最小值的一类问题,这类问题通常用二分法枚举答案就行了. 二分答案时,先确定答案肯定在哪个区间内.然后二分判断,关键在于怎么判断每次枚举的这个答案行不行. 我是用a[i]数组表示初始时花的高度,b[i]表示要达到当前枚举的答案(即mid的值)需要这朵花再涨多少.这两个数组很好算,关键是一次浇连续的w朵花,如何更新区间(暴力的O(n2)的去更新就超时了)?可以用线段树,但是这道题没有涉及区间查询,就是在一个数组上更新区间,用线段树未免小题大做.那么其实这种更新就用延迟标记的思想(懒操作)就

E - Just a Hook HDU 1698 (线段树+类似延迟标记)

E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several

浅析__线段树延迟标记

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 当中有文字系转载! 区间更新是指更新某个区间内的叶子节点的值,由于涉及到的叶子节点不止一个,而叶子节点会影响其对应的非叶父节点,那么回溯须要更新的非叶子节点也会有非常多,假设一次性更新完,操作的时间复杂度肯定不是O(lgn),比如当我们要更新区间[0,3]内的叶子节点时,须要更新除了叶子节点3,9外的全部其它节点.为此引入了线段树中的延迟标记概念,这也是线段树的精华所在. 延迟标记

HDU 1890 Robotic Sort 伸展树的区间反转与延迟标记

延迟标记像极了线段树,不再多说. 区间反转在树伸展到位之后,也变成了简单的递归交换左右儿子. 愈发感觉到伸展树简直太漂亮了,伸展操作更是诱惑到不行 ,总之数据结构太有魅力了. 比较简单,就直接上模板了. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #in

杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)

欢迎"热爱编程"的高考少年--报考杭州电子科技大学计算机学院 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20889    Accepted Submission(s): 10445 Problem Description In the game of DotA, Pudge's meat hook