http://acm.uestc.edu.cn/problem.php?pid=1480&cid=164

#include<iostream>
#include<algorithm>
#include<queue>
#define N 255
using namespace std;
int map[N][N];
int dx[]={1,0,0,-1};
int dy[]={0,1,-1,0};
struct str
{
	int x;
	int y;
	int step;
	bool operator<(const str& cur) const
	{return cur.step<step;}
}start;
int n,m;
int bfs()
{
	priority_queue<str>Q;
	start.x=1,start.y=1,start.step=0;
	Q.push(start);
	map[1][1]=1;
	while(!Q.empty())
	{
		start=Q.top();
		Q.pop();
		int xx=start.x,yy=start.y,minstep=start.step;
		if(xx==n&&yy==m) return minstep;
		for(int i=0;i<4;++i)
		{
			int a=xx+dx[i];                                                
			int b=yy+dy[i];
			if(a>=1&&a<=n&&b>=1&&b<=m&&map[a][b]==0)
			{
				 start.x=a,start.y=b,start.step=minstep+1;
				 if(a==n&&b==m) return start.step;
				 Q.push(start);
				 map[a][b]=1;
			}

		}
	}
	return -1;
}
int main()
{
	int T;
	cin>>T;
	int t=0;
	while(T--)
	{    
		++t;
		cin>>n>>m;
		for(int i=1;i<=n;++i)
			for(int j=1;j<=m;++j)
			 cin>>map[i][j];
		if(map[1][1]==1||map[n][m]==1) cout<<"Case #"<<t<<": -1"<<endl;
		else  cout<<"Case #"<<t<<": "<<bfs()<<endl;
  } return 0;
}

搜索入门题,很水的,,~~~

你可能感兴趣的:(struct)