USACO March. 2012

时间:2022-02-18 12:36:26

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