坑点:
进了kfc可以从这个建筑物里出去,四面八方的出那种。。。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int v[300][300];
int ans[220][220];
int d[4][2] = {{0,-1},{0,1},{1,0},{-1,0} };//方向分量
struct node{
int x,y,t;
node(){}
node(int x,int y,int t){
this->x = x,this->y = y,this->t = t;
}
};
int bfs(int ii,int jj){
node temp(ii,jj,0);
queue<node> q;
q.push(temp);
while(!q.empty()){
temp = q.front();
q.pop();
for(int i = 0;i < 4;i++){
int a = temp.x + d[i][0],b = temp.y +d[i][1];
if(v[a][b] == 1){
v[a][b] = -1;
q.push(node(a,b,temp.t+1));
}
else if(v[a][b] == 3){
v[a][b] = 4;
ans[a][b] += temp.t + 1;
q.push(node(a,b,temp.t+1));
}
}
}
return -1;
}
char s[220];
int s1,s2,s3,s4;
int main(){
int m,n;
while(~scanf("%d%d",&n,&m)){
memset(v,0,sizeof(v));
memset(ans,0,sizeof(ans));
char c;
for(int i = 1;i <= n;i++){
scanf("%s",s);
for(int j = 0;j < m;j++){
if(s[j] == 'Y'){
s1 = i,s2 = j+1;
v[i][j+1] = 1;
}
else if(s[j] == 'M'){
s3 = i,s4 = j + 1;
v[i][j+1] = 1;
}
else if(s[j] == '.'){
v[i][j+1] = 1;
}
else if(s[j] == '@'){
v[i][j+1] = 3;
}
}
}
bfs(s1,s2);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(v[i][j] == 4)
v[i][j]--;
else if(v[i][j] == -1)
v[i][j] = -v[i][j];
}
}
bfs(s3,s4);
int res = 10000000;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(ans[i][j] != 0 && res > ans[i][j])
res = ans[i][j];
}
}
printf("%d\n",res*11);
}
return 0;
}