http://acm.hdu.edu.cn/showproblem.php?pid=4198
分析:计算最小时间时间
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int NM=505;
const int MAX=0x3fffffff;
char str[NM][NM];
int vis[NM][NM],n,m,d,x1,y1,x2,y2;
int a[4][2]={-1,0,1,0,0,1,0,-1};
struct Node{
int x,y,ans;
};
bool operator < (struct Node A,struct Node B)
{
if(A.ans>B.ans) return 1;
else return 0;
}
void BFS()
{
priority_queue<Node>pq1;
Node t,p;
int i;
memset(vis,0,sizeof(vis));
vis[x1][y1]=0;
t.x=x1,t.y=y1;
t.ans=0;
pq1.push(t);
while(!pq1.empty())
{
t=pq1.top();pq1.pop();
if(t.x==x2&&t.y==y2)
{
printf("%d\n",t.ans+1);
return;
}
for(i=0;i<4;i++)
{
p.x=t.x+a[i][0],p.y=t.y+a[i][1];
if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&str[p.x][p.y]!='#'&&!vis[p.x][p.y])
{
p.ans=t.ans+1;
if(str[p.x][p.y]=='@')
p.ans+=d;
pq1.push(p);
vis[p.x][p.y]=1;
}
}
}
}
int main()
{
int i,j,T,f;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&d);
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
for(j=0;j<m;j++)
if(str[i][j]=='S')
x1=i,y1=j;
}
f=0;
for(i=0;i<n;i++)
{
if(str[0][i]!='#')
{
x2=0,y2=i;
f=1;
break;
}
if(str[n-1][i]!='#')
{
x2=n-1,y2=i;
f=1;
break;
}
}
if(!f)
{
for(j=0;j<m;j++)
{
if(str[j][0]!='#')
{
x2=j,y2=0;
break;
}
if(str[j][m-1]!='#')
{
x2=j,y2=m-1;
break;
}
}
}
BFS();
}
return 0;
}