题目大意:给你一张有n * m个网格的图,每个网格可能是如下符号:

“#”:墙

“P”:出发点

“D”:终点

“.”:空地

“*”:传送机

有一个旅行家(假设名叫Mike),他要从点P到达点D,途中必须遵循如下规则:

1、  Mike可以走到空地(“.”),但不可通过墙(“#”)。

2、  Mike也可以走到传送机(“*”),但是当他第一次到达传送机时,下一步只有一种选择:他必须选择到达另一个传送机,然后,下一步会有两种选择:

一、走到相邻的可去的格子中。

二、选择到达另一个传送机,然后遵循同样的规则。

让你计算出Mike从点P到点D的最少步数,如果不能到达,就输出“impossible”。

解题思路:Mike从点P到达点D只可能有两种方式:

1、  经过传送机(“*”), 但图中必须有两个或两个以上的传送机。

2、  不经过传送机,只经过空地(“.”)。

所以只需找出两种方式所需步数的最小值即可。

Ps:程序后面有几组我自己的测试样例,请仔细理解。

具体解法请看程序:

#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI acos(-1.0)
#define inf 0x3fffffff
#define mem(a,b) memset(a,b,sizeof(a)) using namespace std;
const int MAXN = 205 ;
char s[MAXN][MAXN] ;
bool vis[MAXN][MAXN] ;
int dP[MAXN][MAXN] ; // 记录点P到每个点网格中每个点(中间不经过“*”)的最短距离
int dD[MAXN][MAXN] ; // 记录点D到每个点网格中每个点(中间不经过“*”)的最短距离
int ci[MAXN][MAXN] ; // 记录每个“*”点是否能从点P或点D到达。
int ca ;
int m , n ;
int te ; // 统计矩阵中的 “*” 数量。
int X[4] = {0 , 0 , 1 , -1} ; // 四个方向
int Y[4] = {1 , -1 , 0 , 0} ;
int MIN ; // 记录从点P 到 点D 距离的最小值
struct Node
{
int x ;
int y ;
};
Node c , e ; // c代表点P ,e 代表 点D struct Kx // 记录 每个可达的 “*”到点P 和 到点D的最近距离
{
int x ;
int y ;
int d ;
} kkP[MAXN * MAXN] , kkD[MAXN * MAXN] ;
int cntP , cntD ;
void init() // 输入
{
scanf("%d%d" , &m , &n) ;
mem(ci , 0) ;
int i , j ;
te = 0 ;
for(i = 0 ; i < m ; i ++)
{
scanf("%s" , s[i]) ;
for(j = 0 ; j < n ; j ++)
{
if(s[i][j] == '*')
{
te ++ ;
}
else if(s[i][j] == 'P')
{
c.x = i ;
c.y = j ;
}
else if(s[i][j] == 'D')
{
e.x = i ;
e.y = j ;
}
}
}
}
queue<Node> q ;
int cango1(int x , int y)
{
if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && ((s[x][y] == '.' || s[x][y] == 'D')))
return 1 ;
return 0 ;
}
int cango2(int x , int y)
{
if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && s[x][y] != '#')
return 1 ;
return 0 ;
}
bool flag ; // 判断点P是否能不经过点 “*” 到达 点D 。
void bfs(int i , int j , int bb) // 从点P bfs
{
while (!q.empty()) q.pop() ;
Node tmp ;
tmp.x = i ;
tmp.y = j ;
q.push(tmp) ;
vis[i][j] = true ;
while (!q.empty())
{
tmp = q.front() ;
q.pop() ;
int k ;
int tx , ty ;
for(k = 0 ; k < 4 ; k ++)
{
Node tp2 ;
tx = tmp.x + X[k] ;
ty = tmp.y + Y[k] ;
if(bb == 1)
{
if(cango1(tx , ty))
{
dP[tx][ty] = dP[tmp.x][tmp.y] + 1 ;
if(tx == e.x && ty == e.y)
flag = true ;
vis[tx][ty] = true ;
tp2.x = tx ;
tp2.y = ty ;
q.push(tp2) ;
}
}
else
{
if(cango2(tx , ty))
{
dP[tx][ty] = dP[tmp.x][tmp.y] + 1 ;
if(s[tx][ty] == '*')
{
ci[tx][ty] ++ ;
++ cntP ;
kkP[cntP].x = tx ;
kkP[cntP].y = ty ;
kkP[cntP].d = dP[tx][ty] ;
}
if(tx == e.x && ty == e.y)
flag = true ;
vis[tx][ty] = true ;
tp2.x = tx ;
tp2.y = ty ;
if(s[tx][ty] != '*') // 注意此处
q.push(tp2) ;
}
}
}
}
}
void bfs2(int i , int j ) // 从点D bfs
{
while (!q.empty()) q.pop() ;
Node tmp ;
tmp.x = i ;
tmp.y = j ;
q.push(tmp) ;
vis[i][j] = true ;
while (!q.empty())
{
tmp = q.front() ;
q.pop() ;
int k ;
int tx , ty ;
for(k = 0 ; k < 4 ; k ++)
{
Node tp2 ;
tx = tmp.x + X[k] ;
ty = tmp.y + Y[k] ;
if(cango2(tx , ty))
{
dD[tx][ty] = dD[tmp.x][tmp.y] + 1 ;
if(s[tx][ty] == '*')
{
ci[tx][ty] ++ ;
++ cntD ;
kkD[cntD].x = tx ;
kkD[cntD].y = ty ;
kkD[cntD].d = dD[tx][ty] ;
}
vis[tx][ty] = true ;
tp2.x = tx ;
tp2.y = ty ;
if(s[tx][ty] != '*') // 注意此处
q.push(tp2) ;
}
}
}
} void solve()
{
printf("Case %d: " , ++ ca) ;
flag = false ;
mem(dP , 0) ;
mem(dD , 0) ;
mem(vis , 0) ;
cntP = cntD = -1 ;
if(te <= 1)
{
bfs(c.x , c.y , 1) ;
}
else
{
bfs(c.x , c.y , 2) ;
mem(vis , 0) ;
bfs2(e.x , e.y) ;
}
int i , j ;
MIN = inf ;
if(te > 1) // 注意此处,想一想
{
for(i = 0 ; i < m ; i ++)
{
for(j = 0 ; j < n ; j ++)
{
if(ci[i][j] > 1)
{
if(MIN > dP[i][j] + dD[i][j] + 2)
MIN = dP[i][j] + dD[i][j] + 2 ;
}
}
}
}
if(flag)
{
if(te <= 1)
{
if(MIN > dP[e.x][e.y])
MIN = dP[e.x][e.y] ;
printf("%d\n" , MIN) ;
}
else
{
MIN = min(MIN , dP[e.x][e.y]) ;
if(cntP >= 0 && cntD >= 0)
{
if(kkD[0].x == kkP[0].x && kkD[0].y == kkP[0].y)
{
if(cntP > 0)
{
MIN = min(MIN , kkP[1].d + kkD[0].d + 1) ;
}
if(cntD > 0)
{
MIN = min(MIN , kkP[0].d + kkD[1].d + 1) ;
}
}
else
{
MIN = min(MIN , kkD[0].d + kkP[0].d + 1) ;
}
}
printf("%d\n" , MIN) ;
}
}
else
{
if(te <= 1)
{
puts("impossible") ;
return ;
}
else
{
if(cntP < 0 || cntD < 0)
{
puts("impossible") ;
return ;
}
if(kkD[0].x == kkP[0].x && kkD[0].y == kkP[0].y)
{
if(cntP > 0)
{
MIN = min(MIN , kkP[1].d + kkD[0].d + 1) ;
}
if(cntD > 0)
{
MIN = min(MIN , kkP[0].d + kkD[1].d + 1) ;
}
}
else
{
MIN = min(MIN , kkD[0].d + kkP[0].d + 1) ;
}
printf("%d\n" , MIN) ;
return ;
}
}
} int main()
{
int T ;
scanf("%d" , &T) ;
while (T --)
{
init() ;
solve() ;
}
return 0 ;
} /*
9
4 10
##########
#.P..#*..#
#*......D#
##########
3 9
#########
#P.#..D.#
#########
3 7
#######
#P*D#*#
#######
3 8
########
P*.#..D#
####*###
3 5
#####
#P.D#
#####
3 5
#####
#P*D#
#####
3 5
#####
#P..#
#####
5 10
##########
#.P..#*..#
#.....####
#*......D#
##########
3 9
#########
#P*D...*#
######### */

LA 5966 Blade and Sword (双向bfs + 想法) - from lanshui_Yang的更多相关文章

  1. UVALive 5966 Blade and Sword -- 搜索(中等题)

    题意:给一幅地图,P为起点,D为终点,'*'为传送阵,到达传送阵可以传到任意一个其他的传送阵,传到以后可以正常走或者再传回来,问P->D最短步数. 分析:这题一定要细心,分析要到位才能搞定,错一 ...

  2. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  3. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  4. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  5. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  6. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  7. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  9. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

随机推荐

  1. mysql5.7慢查询开启配置

    1.问题 当然又被度娘(我冤枉)坑了噻,网友说配置文件写下面三行: log-slow-queries=D:\Program Files\mysql-5.7.11-winx64\data\low.log ...

  2. opencv如何截取子图像

     首先用GetSubRect函数确定子图像的区域 GetSubRect 返回输入的图像或矩阵的矩形数组子集的矩阵头 CvMat* cvGetSubRect( const CvArr* arr, CvM ...

  3. [问题解决] 启动mongod 时,出现addr already in use错误

    错误: 启动mongod root@wangyuyu-Vostro-:/usr/bin# ./mongod 错误提示: Sat Aug :: [initandlisten] ERROR: listen ...

  4. USB2.0的基本学习

    SB2.0是在1.0的基础上于2000年提出来的,在1.0的基础上曾加了480Mbps的数据传输率.USB2.0具有以下的优点: 1.每个USB系统中有一个主机,通过级联的方式连接多个外部设备,最多可 ...

  5. 使用 Node.js 做 Function Test

    Info 上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿.现在迫切需要个简单的模板项目能快速搭建function t ...

  6. LFM 隐语义模型

    隐语义模型: 物品       表示为长度为k的向量q(每个分量都表示  物品具有某个特征的程度) 用户兴趣 表示为长度为k的向量p(每个分量都表示  用户对某个特征的喜好程度) 用户u对物品i的兴趣 ...

  7. opensatck 使用devstack在 laptop上的 网络配置

    http://docs.openstack.org/developer/devstack/guides/neutron.html Physical Network Setup In most case ...

  8. 在CGridView调用CJuiDialog的弹出层

    <?php $this->widget('zii.widgets.grid.CGridView', array(    'id'=>'user-grid',    'dataProv ...

  9. Acdream1084 寒假安排 求n!中v因子个数

    题目链接:pid=1084">点击打开链接 寒假安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 128000/64000 ...

  10. ContextMenuStrip 添加在窗体。点击右键不能显示问题解答

    ContextMenuStrip控件,系统默认为点击菜单,使用时.需要与你的窗体绑定.如图所示,,form窗体上包括一个contextMenustrip信息.默认状态是无.所以在使用contextMe ...