RQNOJ 34 紧急援救 解题报告
SPFA,直接搜索就行,代码如下:
#include <stdio.h> #include <stdlib.h> #define Q_MAX 100000 char map[1000][1000]; int dis[1000][1000]; struct node{ int x, y; }queue[Q_MAX]; int head, rear; int used[1000][1000]; void enqueue(int a, int b) { if(used[a][b]){ return; } used[a][b] = 1; queue[rear].x = a; queue[rear].y = b; rear = (rear + 1) % Q_MAX; } void exqueue(int *a, int *b) { *a = queue[head].x; *b = queue[head].y; used[*a][*b] = 0; head = (head + 1) % Q_MAX; } int main(int argc, char **argv) { int i, j; int a, b; int x, y; int n; scanf("%d\n", &n); for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ scanf("%c", &map[i][j]); dis[i][j] = 0xFFFFFFF; map[i][j] -= '0'; } scanf("\n"); } scanf("%d%d", &a, &b); dis[a - 1][b - 1] = 0; enqueue(a - 1, b - 1); scanf("%d%d", &a, &b); a--, b--; while(head != rear){ exqueue(&x, &y); #define deal(a, b) if(((a) >= 0 && (a) < n && (b) >= 0 && b < n) && (!map[a][b]) && dis[a][b] > dis[x][y] + 1){\ dis[a][b] = dis[x][y] + 1;\ enqueue((a), (b));\ } deal(x + 1, y); deal(x - 1, y); deal(x, y - 1); deal(x, y + 1); } printf("%d\n", dis[a][b]); return 0; }