【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=2612
题目意思
两个人约在肯德基见面,但是肯德基有多家,所以让你找出两人时间和最少的一家肯德基。每次移动是11分钟,‘@’为肯德基,‘。’为路,‘#’为墙
解题思路
跑两遍广搜,记录到的时间,找出两次和最小的就可以了
代码部分
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1e5+5;
struct node
{
int x,y,step;
}s1,s2;
char maps[205][205];
int vis[205][205];
int step[205][205]; ///记录步数
int dir[4][2]={1,0,-1,0,0,-1,0,1}; ///方向
int Max = inf;
int n,m;
void bfs (node s, int k)
{
queue <node>q;
q.push(s);
vis[s.x][s.y] = 1;
while (!q.empty())
{
node t,tt;
t = q.front();
q.pop();
tt.step = t.step+11;
if (maps[t.x][t.y] == '@') ///到肯德基时加上步数
{
step[t.x][t.y] += t.step;
if (step[t.x][t.y] < Max && k == 2) ///如果是第二个人到看下是否跟新最小和
Max = step[t.x][t.y];
}
for (int i = 0; i < 4; i++)
{
tt.x = t.x+dir[i][0];
tt.y = t.y+dir[i][1];
if (tt.x >= 0 && tt.y >= 0 && tt.x < n && tt.y < m && !vis[tt.x][tt.y] && maps[tt.x][tt.y] !='#')
{
vis[tt.x][tt.y] = 1;
q.push(tt);
}
}
}
}
int main()
{
while (cin>>n>>m)
{
Max = inf;
memset(step,0,sizeof(step));
memset(vis,0,sizeof (vis));
for (int i =0; i < n; i++)
for (int j = 0; j < m;j++)
{
cin>>maps[i][j];
if (maps[i][j] == 'Y')
{
s1.x = i;
s1.y = j;
s1.step = 0;
}
if (maps[i][j] == 'M')
{
s2.x = i;
s2.y = j;
s2.step = 0;
}
}
bfs(s1,1);
memset(vis,0,sizeof(vis));
bfs(s2,2);
cout << Max << endl;
}
return 0;
}