点击打开链接
//
// main.cpp
// test
//
// Created by 吴有堃 on 2017/9/11.
// Copyright © 2017年 吴有堃. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
const int maxn=25;
using namespace std;
struct Node{
int x,y,steps;
};
queue<Node>Q;
char Arr[maxn][maxn],vis[maxn][maxn];
int Move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int M=0,N=0,xx=0,yy=0;
void find_loc()
{
int i=0,j=0;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
if(Arr[i][j]=='@'){
xx=i; yy=j;
return ;
}
}
}
}
void BFS()
{
while (!Q.empty()) Q.pop();
memset(vis,0,sizeof(vis));
Node temp1,temp2;
temp1.x=xx; temp1.y=yy;temp1.steps=0;
vis[xx][yy] = 1;
Q.push(temp1);
while (!Q.empty()) {
temp1=Q.front(); Q.pop();
if(Arr[temp1.x][temp1.y]=='*'){
printf("%d\n",temp1.steps);
return;
}
for(int i=0;i<4;i++){
temp2 =temp1;
temp2.x+=Move[i][0]; temp2.y+=Move[i][1];
if(temp2.x>=0&&temp2.x<M&&temp2.y>=0&&temp2.y<N&&!vis[temp2.x][temp2.y]
&&Arr[temp2.x][temp2.y]!='#'){
temp2.steps +=1;
vis[temp2.x][temp2.y] =1;
Q.push(temp2);
}
}
}
printf("-1\n");
}
int main(int argc, const char * argv[]) {
// insert code here...
int i=0;
while (scanf("%d %d",&M,&N)) {
if(M==0&&N==0) break;
for(i=0;i<M;i++) scanf("%s",Arr[i]);
find_loc();
BFS();
}
return 0;
}