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。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 101
#define M 15
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
char s[N][N];
int vis[N][N];
int cc,tot;
int n,m;
int dirx[]={,-,,};
int diry[]={,,,-}; typedef struct
{
int x;
int y;
int now;
int boom;
}PP; PP start,end; int ok(PP next)
{
if(next.x>= && next.x<n && next.y>= && next.y<m && vis[next.x][next.y]== && s[next.x][next.y]!='#'){
if(s[next.x][next.y]=='M')
next.boom=;
return next.boom;
}
return ;
} void BFS()
{
queue<PP> q;
q.push(start);
int first=-;
PP te,next;
int i;
int re;
while(q.size()!=)
{
te=q.front();
vis[te.x][te.y]=;
q.pop();
// printf(" %d %d\n",te.x,te.y);
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
next.now=te.now+;
next.boom=te.boom;
re=ok(next);
next.boom=re;
if(re==) continue;
if(next.x==end.x && next.y==end.y)
{
// printf(" %d %d first=%d now=%d %d\n",next.x,next.y,first,next.now,next.boom);
if(first==-) first=next.now;
else{
if(next.now!=first) return;
}
tot++;
if(next.boom==) cc++;
}
else{
q.push(next);
}
}
}
} int main()
{
int i,j;
//freopen("data.in","r",stdin);
scanf("%d",&T);
getchar();
for(int cnt=;cnt<=T;cnt++)
//while(T--)
//while(scanf("%d%d",&n,&q)!=EOF)
{
//q.clear();
cc=tot=;
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&m);
getchar();
for(i=;i<n;i++){
gets(s[i]);
}
for(i=;i<n;i++){
for(j=;j<m;j++){
if(s[i][j]=='S'){
start.x=i;
start.y=j;
start.boom=;
start.now=;
}
if(s[i][j]=='T'){
end.x=i;
end.y=j;
}
}
}
//printf("%d %d %d %d\n",start.x,start.y,end.x,end.y);
BFS();
printf("Mission #%d:\n",cnt);
if(cc==){
printf("Mission Impossible.\n");
}
else{
printf("The probability for the spy to get to the telegraph transmitter is %.2f%%.\n",(1.0)**cc/tot);
}
printf("\n"); } return ;
}

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

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

    题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下 ...

  2. HDU5785 Interesting(Manacher + 延迟标记)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5785 Description Alice get a string S. She think ...

  3. FZU 2171(线段树的延迟标记)

    题意:容易理解. 分析:时隔很久,再一次写了一道线段树的代码,之前线段树的题也做了不少,包括各种延迟标记,但是在组队分任务之后,我们队的线段树就交给了另外一个队友在搞, 然后我就一直没去碰线段树的题了 ...

  4. [HDOJ4578]Transformation(线段树,多延迟标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 四种操作:查询.加法.乘法.改数.应该是需要维护三个lazy标记,然后就是套路了.查询是区间内所 ...

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

    欢迎"热爱编程"的高考少年--报考杭州电子科技大学计算机学院 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memor ...

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

    题目链接:https://vjudge.net/problem/UVA-11992 题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询.查询返回子矩阵和.最小值.最大值 n很小 ...

  7. Tree(树链剖分+线段树延迟标记)

    Tree http://poj.org/problem?id=3237 Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12 ...

  8. 【左偏树+延迟标记+拓扑排序】BZOJ4003-城池攻占

    [题目大意] 有n个城市构成一棵树,除1号城市外每个城市均有防御值h和战斗变化参量a和v. 现在有m个骑士各自来刷副本,每个其实有一个战斗力s和起始位置c.如果一个骑士的战斗力s大于当前城市的防御值h ...

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

    这是最大化最小值的一类问题,这类问题通常用二分法枚举答案就行了. 二分答案时,先确定答案肯定在哪个区间内.然后二分判断,关键在于怎么判断每次枚举的这个答案行不行. 我是用a[i]数组表示初始时花的高度 ...

随机推荐

  1. 原生js格式化json的方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. python之编码的进阶

    识记点: ascii 不支持中文 gbk 国标 中文2 英文1 unicode 万国码 英文2 中文4 utf-8 英文1 欧洲2 亚洲3 硬盘中存储的是字节 用什么编码就用什么解码 # 一段文字的转 ...

  3. Repbase library|divergence rate|self-sequence alignment|genomic rearrangement|cutoffs|breakpoint

    (Panda, dog and human repeat comparison):与其他动物比较重复序列 我们使用Repbase 库(重复序列库)+已知的转录原件序列+识别软件,评估出转录原件占比,并 ...

  4. bootstrap 两端对齐的导航

    您可以在屏幕宽度大于768px时,通过在分别使用.nav .nav-tabs或.nav .nav-pills的同时使用class.nav-justified,让标签式或胶囊式导航菜单与父元素等宽,在更 ...

  5. iterator方法和for方法 遍历数据库user表结果集ResultSet

    首先,把连接数据库的语句做成工具类,因为会一直用到这几句 代码如下: package com.swift.jdbc; import java.sql.Connection; import java.s ...

  6. JS原型链(二)--new运算符的原理

    new运算符的原理: 第一步:创建一个空对象,该对象继承构造函数的原型对象 第二步:执行这个构造函数,并且把this指向该空对象 第三步:返回:如果构造函数执行后返回的结果是一个object类型,则返 ...

  7. vue新手入坑之mounted和created的区别(生命周期)

    这几个月用vue框架新做了一个项目,也算是边学习边实践吧.学习中也看过一些别人的开源项目,起初对mounted和created有一些疑惑,查询相关资料发现,这和vue的生命周期有关,在此也就做一个总结 ...

  8. (转) iOS程序国际化

    IOS程序国际化  本文转自http://www.cnblogs.com/zhidao-chen/archive/2012/07/08/2581977.html 1.1 新建一个Single View ...

  9. C# 反射总结

    反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的 ...

  10. luogu3809 后缀排序 后缀数组

    ref and 挑战程序设计竞赛. 主要是发现自己以前写得代码太难看而且忘光了,而且我字符串死活学不会啊,kmp这种东西我都觉得是省选+难度啊QAQ #include <iostream> ...