USACO March. 2012

Connect the Cows

Times17

水题

Landscaping

Flowerpot

Tractor

广搜 搜到边界就可以终止了 没什么难度

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int MAX = 1010;
int a[MAX][MAX];
int dir[4][2] = {0,1,0,-1,1,0,-1,0};
struct node
{
	int x;
	int y;
	int step;
	bool friend operator < (node a,node b)
	{
		return a.step > b.step;
	}
}s;

void bfs()
{
	int i;
	s.step = 0;
	priority_queue <node> q;
	q.push(s);
	while(!q.empty())
	{
		node p = q.top();
		q.pop();
		if(p.x == 0 || p.y == 0 || p.x == 1001 || p.y == 1001)
		{
			printf("%d\n",p.step);
			return;
		}
		for(i = 0;i < 4; i++)
		{
			node t;
			t.x = p.x + dir[i][0];
			t.y = p.y + dir[i][1];
			t.step = p.step;
			if(a[t.x][t.y] == -1)
				continue;
			if(a[t.x][t.y])
				t.step++;
			a[t.x][t.y] = -1;
			q.push(t);
		}
			
	}
}
int main()
{
	int n,x,y;
	scanf("%d %d %d",&n,&s.x,&s.y);
	while(n--)
	{
		scanf("%d %d",&x,&y);
		a[x][y] = 1;
	}
	bfs();
	return 0;
}


 

Haybale Restacking

 

转载于:https://www.cnblogs.com/riasky/p/3430897.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值