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;
}

搜索入门题,很水的,,~~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值