| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
单向BFS,WA了十几次,求助!!!#include <stdio.h>
#define MaxSize 310
int num, n, startx, starty, endx, endy;
bool map[MaxSize][MaxSize];
int dir[8][2] = { {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1} };
typedef struct point {
int x, y, step;
}pointQueue[MaxSize];
int front = 0, rear = 0;
int bfs() {
point now; now.x = startx; now.y = starty; now.step = 0;
pointQueue queue; queue[rear] = now; rear = (rear + 1) % MaxSize;
point first, next;
while (front != rear) {
first = queue[front]; front = (front + 1) % MaxSize;
if (first.x == endx && first.y == endy)
return first.step;
for (int i = 0; i < 8; i++) {
int x = first.x + dir[i][0], y = first.y + dir[i][1];
if (x >= 0 && x < n&&y >= 0 && y < n && !map[x][y]) {
map[x][y] = true;
next.x = x; next.y = y; next.step = first.step + 1;
queue[rear] = next; rear = (rear + 1) % MaxSize;
}
}
}
return 0;
}
int main() {
scanf("%d", &num);
while (num--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
map[i][j] = false;
scanf("%d%d%d%d", &startx, &starty, &endx, &endy);
map[startx][starty] = true;
front = rear = 0;
printf("%d\n", bfs());
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator