计蒜客 - T1212 仙岛求药
https://vjudge.net/problem/%E8%AE%A1%E8%92%9C%E5%AE%A2-T1212
AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,m;
int sx,sy;
int tx,ty;
char g[1500][1500];
struct node{
int x,y,step;
}t1,t2;
int main()
{
while(cin>>n>>m)
{
getchar();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char ch;
ch=getchar();
while(ch!='.' && ch!='@' && ch!='#' && ch!='*')
ch=getchar();
g[i][j]=ch;
if(g[i][j]=='@')
{
sx=i; sy=j;
}
else if(g[i][j]=='*')
{
tx=i; ty=j;
}
}
}
queue<node> q;
t1.x=sx;
t1.y=sy;
t1.step=0;
q.push(t1);
//出发点是否标记
g[sx][sy]='#';
int dt[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
while(q.size())
{
t1=q.front();
q.pop();
int f=0;
for(int i=0;i<4;i++)
{
int dx=dt[i][0]+t1.x;
int dy=dt[i][1]+t1.y;
if(dx>0 && dx<=n && dy>0 && dy<=m && g[dx][dy]!='#')
{
t2.x=dx;
t2.y=dy;
t2.step=t1.step+1;
q.push(t2);
if(g[dx][dy]=='*')
{
f=1;
break;
}
else
g[dx][dy]='#';
}
}
if(f)
break;
}
if(q.size())
cout<<q.back().step<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
本文来自博客园,作者:斯文~,转载请注明原文链接:https://www.cnblogs.com/zhiweb/p/15483337.html

浙公网安备 33010602011771号