廣度優先搜索

以UOJ上的騎士遊歷爲例
傳送門

struct note {
    int x;
    int y;
    int s;
} que[2501];


que[tail].x = 1;    //x座標 
que[tail].y = 1;    //y座標 
que[tail].s = 0;   //記錄步數 
tail++;
b[1][1] = 1;     //標記數組 
while(head < tail) {  //如果所有的都被走過了 head == tail 
    for(int i = 0; i<4; i++) {
        tx = que[head].x + next[i][0];   //嘗試4個方向 
        ty = que[head].y + next[i][1];
        if(tx > n || tx < 1 || ty > m || ty < 1) {
            continue;
        }
        if(!b[tx][ty]) {
            b[tx][ty] = 1;
            que[tail].x = tx;   //將新的擴張點插入隊尾 
            que[tail].y = ty;
            que[tail].s = que[head].s+1;    //到達該點的步數爲前驅的步數+1 
            tail++;     //尾指針++ 
        }
        if(tx == n && ty == m) {    //判斷是否到達目標點 
            judge = 1;
            break;
        }
    }
    if(judge) {
        break;
    }
    head++;     //頭指針一定要++ 
}

令我印象深刻的一道廣搜。

P1141 01迷宮

第一次,沒有反應過來,有100000組詢問,而我第一次每次詢問都去廣搜了一遍,結果70分。
第二次,請教了學長,發現從任意一點出發,與從那一點出發能到達的所有點中的任意一點出發,能到達的格數是一樣的,據說這叫連通塊。所以只用廣搜一次,從每個未被訪問過的點出發,直到遍歷完全圖。據說這叫“預處理”。代碼沒有體現面向對象的原則……

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node {
    int x;
    int y;
    int s;
    node() {
        x = 0;
        y = 0;
        s = 0;
    }
} que[1000000];
int map[1001][1001],b[1001][1001];
int n,m;
int read_In() {
    int a = 0;
    bool minus = false;
    char ch = getchar();
    while (!(ch == '-' || (ch >= '0' && ch <= '9'))) ch = getchar();
    if (ch == '-') {
        minus = true;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        a = a * 10 + (ch - '0');
        ch = getchar();
    }
    if (minus) a = -a;
    return a;
}
void printOut(int x) {
    char buffer[20];
    int length = 0;
    bool minus = x < 0;
    if (minus) x = -x;
    do {
        buffer[length++] = x % 10 + '0';
        x /= 10;
    } while (x);
    if (minus) buffer[length++] = '-';
    do {
        putchar(buffer[--length]);
    } while (length);
    putchar('\n');
}
void bfs(int start_X,int start_Y) {

    const int next[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
    int head = 0,tail = 0;
    b[start_X][start_Y] = 1;
    que[tail].x = start_X;
    que[tail].y = start_Y;
    que[tail].s = map[start_X][start_Y];
    tail++;
    int tx,ty;
    while(head < tail) {
        for(int i = 0; i<4; i++) {
            tx = que[head].x + next[i][0];
            ty = que[head].y + next[i][1];
            if(tx > n || ty > n || tx < 1 || ty < 1) {
                continue;
            }
            if(!b[tx][ty] && map[tx][ty] + que[head].s == 1) {
                b[tx][ty] = 1;
                que[tail].x = tx;
                que[tail].y = ty;
                que[tail].s = map[tx][ty];
                tail++;
            }
        }
        head++;
    }
    for(int i = 0; i<tail; i++) {
        b[que[i].x][que[i].y] = tail;
    }
}
int main() {
    n = read_In();
    m = read_In();
    for(int i = 1; i<=n; i++) {
        for(int j = 1; j<=n; j++) {
            char ch = getchar();
            while (!(ch >= '0' && ch <= '1')) ch = getchar();
            map[i][j] = ch - '0';
        }
    }
    for(int i = 1; i<=n; i++) {
        for(int j = 1; j<=n; j++) {
            if(b[i][j]){
                continue;
            }
            if(!b[i][j]) {
                bfs(i,j);
            }
        }
    }
    for(int i = 1; i<=m; i++) {
        int t1,t2;
        t1 = read_In();
        t2 = read_In();
        printOut(b[t1][t2]);
    }
    return 0;
}
發佈了50 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章