#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int mapp[130][130];
int flag[130][130];
int res;
int N;
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
struct Node
{
int x,y,step;
bool friend operator<(Node m,Node n)
{
return m.step>n.step;
}
};
void BFS()
{
Node xx,yy;
int i;
xx.x=0;
xx.y=0;
xx.step=mapp[0][0];
priority_queue<Node>Find;
Find.push(xx);
while(!Find.empty())
{
xx=Find.top();
Find.pop();
if(xx.x==N-1&&xx.y==N-1)
{
res=xx.step;
return;
}
for(i=0;i<=3;i++)
{
yy.x=xx.x+dir[i][0];
yy.y=xx.y+dir[i][1];
if(yy.x>=0&&yy.x<=N-1&&yy.y>=0&&yy.y<=N-1&&flag[yy.x][yy.y]==0)
{
yy.step=xx.step+mapp[yy.x][yy.y];
flag[yy.x][yy.y]=1;
Find.push(yy);
}
}
}
}
int main()
{
int i,j,Case=1;
while(~scanf("%d",&N)&&N)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&mapp[i][j]);
}
}
memset(flag,0,sizeof(flag));
flag[0][0]=1;
BFS();
printf("Problem %d: %d\n",Case++,res);
}
return 0;
}
杭电3152
最新推荐文章于 2021-02-22 14:35:11 发布