传送门

总时间限制: 
1000ms

内存限制: 
65536kB
描述

公主被恶人抓走,被关押在牢房的某个地方。牢房用N*M (N, M <= 200)的矩阵来表示。矩阵中的每项可以代表道路(@)、墙壁(#)、和守卫(x)。 
英勇的骑士(r)决定孤身一人去拯救公主(a)。我们假设拯救成功的表示是“骑士到达了公主所在的位置”。由于在通往公主所在位置的道路中可能遇到守卫,骑士一旦遇到守卫,必须杀死守卫才能继续前进。 
现假设骑士可以向上、下、左、右四个方向移动,每移动一个位置需要1个单位时间,杀死一个守卫需要花费额外的1个单位时间。同时假设骑士足够强壮,有能力杀死所有的守卫。

给定牢房矩阵,公主、骑士和守卫在矩阵中的位置,请你计算拯救行动成功需要花费最短时间。

输入
第一行为一个整数S,表示输入的数据的组数(多组输入)
随后有S组数据,每组数据按如下格式输入 
1、两个整数代表N和M, (N, M <= 200). 
2、随后N行,每行有M个字符。"@"代表道路,"a"代表公主,"r"代表骑士,"x"代表守卫, "#"代表墙壁。
输出
如果拯救行动成功,输出一个整数,表示行动的最短时间。
如果不可能成功,输出"Impossible"
样例输入
2
7 8
#@#####@
#@a#@@r@
#@@#x@@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@
13 40
@x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x
xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@
#@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x
@##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@#
@#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@
#xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x#####
#x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x
xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x
x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@
#x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x
x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@
#@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x
#x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@
样例输出
13
7 【思路】
广搜+优先队列维护
错误:结构体忘记写比较函数,优先队列里定义的是Node
【code】
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int endx,endy,strx,stry,t,n,m;
char map[][];
int dx[]={,,,-},
dy[]={,-,,};
int b[][];
int ans;
struct node
{
int x,y,val;
bool operator < (const node a) const
{
return a.val<val;
}
};
priority_queue<node>q; int bfs(int x,int y)
{ while(!q.empty())
{
node now=q.top();q.pop();
for(int i=;i<;i++)
{
int xx=now.x+dx[i],yy=now.y+dy[i];
if(xx>=&&xx<n&&yy>=&&yy<m&&!b[xx][yy])
{
if(map[xx][yy]=='x')
{
b[xx][yy]=;
q.push((node){xx,yy,now.val+});
}
if(map[xx][yy]=='@')
{
b[xx][yy]=;
q.push((node){xx,yy,now.val+});
}
if(xx==endx&&yy==endy)
{
ans=now.val+;
return ;
}
}
}
}
return ;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(b,,sizeof(b));
scanf("%d%d",&n,&m);
while(!q.empty())q.pop();
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<m;j++)
{
if(map[i][j]=='r')
{
strx=i;stry=j;
}
if(map[i][j]=='a')
{
endx=i;endy=j;
}
}
}
q.push((node){strx,stry,});
if(bfs(strx,stry))printf("%d\n",ans);
else printf("Impossible\n");
}
return ;
}

最新文章

  1. scrapy 学习笔记
  2. 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参
  3. GCD使用dispatch_group_notify、dispatch_group_enter、dispatch_group_leave处理多线程同步操作
  4. 简单几何(线段相交+最短路) POJ 1556 The Doors
  5. Redis高级实践之————Redis短连接性能优化
  6. CodeForces 489C (贪心) Given Length and Sum of Digits...
  7. Largest prime factor
  8. 996.ICU
  9. hdu 3068 最长回文(manacher&amp;amp;最长回文子串)
  10. 工欲善其事,必先利其器-ecplise配置和优化
  11. sap 下载程序
  12. 学习笔记之1001 Inventions That Changed the World
  13. 电脑cpu100%的原因
  14. 采用PowerDesigner 设计数据库
  15. qperf测量网络带宽和延迟
  16. pip常用命令总结(转载)
  17. Java学习(一)Scanner报错java.util.NoSuchElementException
  18. 【HTML】百度地图webAPI使用
  19. python技巧之下划线(二)
  20. mybatis 打印SQL

热门文章

  1. MVC项目总结
  2. 安装部署zookeeper集群
  3. 如何在IntelliJ IDEA在线查看源码的API文档
  4. linux 打印系统时间操作
  5. 九度OJ 1029:魔咒词典 (排序)
  6. Linux就该这么学--了解Shell脚本
  7. 20170325 ABAP调用webservice
  8. ABAP-创建客户
  9. Mac下php版本不支持imagetfftext函数问题
  10. wamp server 安装后 Apache80端口占用