Find a way HDU - 2612
思路:本来想着两个队列Y搜一步M搜一步搜到就出来,结果一直TLE,然后用bfs搜两遍就过了
具体代码如下
#include<cstring>
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 210;
struct node{
int x, y, d;
};
int n, m;
char g[N][N];
bool st[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int kfc[N][N];
void bfs(node start){
memset(st,0,sizeof(st));
queue<node>q;
q.push(start);
while(!q.empty()){
node t=q.front();
if(g[t.x][t.y]=='@') kfc[t.x][t.y] += t.d;
q.pop();
for(int i=0;i<4;i++){
int a = t.x + dx[i], b = t.y + dy[i];
if(a <= n && a >= 1 && b <= m && b >=1 && !st[a][b] && g[a][b] != '#'){
st[a][b]=true;
node u;
u.x = a, u.y = b, u.d = t.d + 1;
q.push(u);
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m) == 2){
memset(kfc,0,sizeof(kfc));
node Y,M;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf(" %c",&g[i][j]); //scanf读入单个字符有时候会读取一点奇奇怪怪的东西所以前边加个空格
if(g[i][j]=='Y'){
Y.d=0;
Y.x=i,Y.y=j;
}
else if(g[i][j]=='M'){
M.d=0;
M.x=i,M.y=j;
}
}
bfs(M);
bfs(Y);
int ans=0X3f3f3f3f;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(kfc[i][j]!=0)
ans=min(ans,kfc[i][j]);
}
printf("%d\n",11*ans);
}
return 0;
}