#include <stdio.h>
#include <string.h>
typedef struct
{
int x,y;
int sec;
}point;
typedef struct
{
point a[12000];
int front,rear;
int size;
}que;
que q;
int row,col, count;
int map[102][102];
int dire[4][2] = {{0,1}, {1,0}, {0,-1}, {-1,0}};
point start, now;
void init_que()
{
q.front = q.rear= q.size = 0;
}
void in_que(point p)
{
q.a[q.rear] = p;
q.rear = (q.rear+1)%12000;
q.size++;
}
void de_que(point *p)
{
// *p = q.a[q.front];
p->x= q.a[q.front].x;
p->y= q.a[q.front].y;
p->sec = q.a[q.front].sec;
q.front = (q.front+1)%12000;
q.size--;
}
void broadfs()
{
point p,tmp;
int i,flag;
while(q.size > 0)
{
de_que(&p);
for(i = 0; i < 4; i++)
{
tmp.x = p.x+dire[i][0];
tmp.y = p.y+dire[i][1];
if(map[tmp.x][tmp.y] == 1)
{
now = tmp;
map[now.x][now.y] = p.sec+1;
now.sec = p.sec+1;
in_que(now);
}
}
}
}
int main(void)
{
int tc, T, i,j;
//freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for(tc = 0; tc < T; tc++)
{
scanf("%d %d", &col, &row);
memset(map, 0, sizeof(map));
for(i = 1; i <= row; i++)
{
for(j = 1; j <= col; j++)
{
scanf("%d", &map[i][j]);
}
}
scanf("%d %d", &start.x, &start.y);
map[start.x][start.y] = 0;
init_que();
start.sec = 1;
in_que(start);
broadfs();
printf("%d\n", now.sec);
for(i = 1; i <= row; i++)
{
for(j = 1; j <= col; j++)
{
printf("%3d", map[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;//Your program should return 0 on normal termination.
}
宽度优先示例
最新推荐文章于 2024-04-09 12:08:14 发布