原题链接

水了一天bfs了

题意:2个人分别从Y,M出发,到达其中任意一个“@” (图中有多个“@”点),2人到达的必须是同一个“@”点,求最短的路程和

思路:bfs搜2次,用一个2维数组记录到达各个“@”的距离之和 再遍历求最小的(用一个数组就可以的原因是Y可以到达的地方M一定也能到达,因为Y,M必然可以相遇,所以Y和M必然是连通,一开始还怀疑了一下自己)

#include "map"
#include "queue"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "iostream"
#include "algorithm"
19:01:0019:01:162016-08-05#define abs(x) x > 0 ? x : -x
#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b using namespace std; int d[][] = {{,},{,},{,-},{-,}};
int Map[][],l[][];
bool vis[][]; struct Node
{
int xx,yy;
int step;
}; void Bfs(int x,int y)
{
memset(vis,,sizeof(vis));
Node now,next;
queue<Node>Q; now.xx = x;
now.yy = y;
now.step = ;
vis[x][y] = ; Q.push(now); while(!Q.empty())
{
now = Q.front();
Q.pop(); if(Map[now.xx][now.yy]==)
l[now.xx][now.yy] += now.step; for(int i=; i<; i++)
{
next.xx = now.xx + d[i][];
next.yy = now.yy + d[i][];
next.step = now.step + ; if(Map[next.xx][next.yy]!= && !vis[next.xx][next.yy])
{
vis[next.xx][next.yy] = ;
Q.push(next);
}
}
}
} int main()
{
int x1,y1,x2,y2,n,m,ans,i,j;
char c;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(Map,,sizeof(Map));
memset(l,,sizeof(l));
for(i=; i<=n; i++)
{
getchar();
for(j=; j<=m; j++)
{
scanf("%c",&c);
if(c=='.')
Map[i][j] = ;
if(c=='#')
Map[i][j] = ;
if(c=='Y')
{
Map[i][j] = ;
x1 = i,y1 = j;
}
if(c=='M')
{
Map[i][j] = ;
x2 = i,y2 = j;
}
if(c=='@')
Map[i][j] = ;
}
}
Bfs(x1,y1);
Bfs(x2,y2); ans = ;
for(i=; i<=n; i++)
for(j=; j<=m; j++)
{
if(l[i][j]!=)
ans = min(ans,l[i][j]);
}
printf("%d\n",ans*);
}
return ;
}

最新文章

  1. UIModalPresentationStyle和UIModalTransitionStyle
  2. 使用 .bash_profile与.bashrc修改字符集
  3. merge into 的用法
  4. jquery获取高度错误(可以获取到宽度,但获取不到高度),及解决办法
  5. ORACLE的分组统计之ROLLUP(一)
  6. 解决Jsoup网页抓取过程中需要cookie的问题
  7. 在Windows下基于libx264.a的Qt 4.8.2视频压缩
  8. (贪心5.1.1)POJ 1230 Pass-Muraille
  9. hibernate的3种状态
  10. Java+7入门经典 - 6 扩展类与继承 Part 1/2
  11. Maven的声明周期(Lifecycle )和命令(Phase)
  12. Vue的组件为什么要export default
  13. 虚拟DOM和react中的diff算法总结
  14. readLine()的注意点
  15. Thymeleaf教程入门到深入1:基础介绍
  16. 【PAT】B1079 延迟的回文数(20 分)
  17. eclipse启动tomcat后,无法通过路径访问项目
  18. jQuery 文件上传插件:uploadify、swfupload
  19. Informatica 常用组件Lookup之四 查找组件
  20. C++中break/Continue,exit/return的理解

热门文章

  1. RTP 与RTCP 解释. 含同步时间戳
  2. cocos2d-x CCScrollView和CCTableView的使用(转载)
  3. android:layout_weight属性详解 (转)
  4. 【RQNOJ356】myt的格斗
  5. 程序员应该是使用git
  6. 彻底删除Kafka中的topic
  7. Struts2零配置介绍(约定访问)
  8. JMeter参数化(一)
  9. css布局2
  10. js事件绑定细节说明