4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 88 66
一开始没有看清题意,写的比较乱。。。吃拉面的时候想清楚了。。。回来写写写
本来以为会超时的
1A,嗯。。。并没有啥难度!
下面代码:
/*
━━━━━┒
┓┏┓┏┓┃μ'sic foever!!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=210;
const int INF=8000000;
int dy[maxn][maxn];
int dm[maxn][maxn];
char save[maxn][maxn];
int n,m;
int xy,yy,xm,ym;
struct unit{
int x,y;
};
int result;
int move_x[4]={0,0,1,-1};
int move_y[4]={1,-1,0,0};
map<pair<int,int>,int> kfc;
void bfsy(){
queue<unit> que;
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
dy[i][j]=INF;
}
}
unit start;
start.x=xy,start.y=yy;
que.push(start);
dy[start.x][start.y]=0;
while(que.size()){
//cout<<"i am Y"<<endl;
unit q=que.front();
//cout<<"Y "<<q.x<<" "<<q.y<<" "<<dy[q.x][q.y]<<endl;
que.pop();
if(save[q.x][q.y]=='@'){
//cout<<"Y: "<<dy[q.x][q.y]<<endl;
kfc[make_pair(q.x, q.y)]=dy[q.x][q.y];
//break;
}
for(i=0;i<4;i++){
int nx=q.x+move_x[i];
int ny=q.y+move_y[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&save[nx][ny]!='#'&&dy[nx][ny]==INF){
unit now;
now.x=nx;
now.y=ny;
que.push(now);
dy[nx][ny]=dy[q.x][q.y]+1;
}
}
}
}
void bfsm(){
queue<unit> que;
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
dm[i][j]=INF;
}
}
unit start;
start.x=xm,start.y=ym;
que.push(start);
dm[start.x][start.y]=0;
while(que.size()){
//cout<<"i am M"<<endl;
unit q=que.front();
//cout<<"M: "<<q.x<<" "<<q.y<<" "<<dm[q.x][q.y]<<endl;
que.pop();
if(save[q.x][q.y]=='@'){
//cout<<"M: "<<dm[q.x][q.y]<<" "<<kfc[make_pair(q.x, q.y)]<<endl;
result=min(result,kfc[make_pair(q.x, q.y)]+dm[q.x][q.y]);
//break;
}
for(i=0;i<4;i++){
int nx=q.x+move_x[i];
int ny=q.y+move_y[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&save[nx][ny]!='#'&&dm[nx][ny]==INF){
unit now;
now.x=nx;
now.y=ny;
que.push(now);
dm[nx][ny]=dm[q.x][q.y]+1;
}
}
}
}
int main(){
int i,j;
while(~scanf("%d%d",&n,&m)){
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>save[i][j];
if(save[i][j]=='Y'){
xy=i,yy=j;
}
if(save[i][j]=='M'){
xm=i,ym=j;
}
if(save[i][j]=='@'){
pair<int,int> kfc_point=make_pair(i, j);
kfc[kfc_point]=0;
}
}
}
result=INF;
bfsy();
bfsm();
printf("%d\n",result*11);
}
return 0;
}