BFS的学习

书上P278的代码:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int maxn = 100;

struct node{
    int x;
    int y;
}grid;     //格子 

int m,n;
int matrix[maxn][maxn];
bool cover[maxn][maxn]={false};
int x1[] = {0, 0, 1, -1};
int y1[] = {1, -1, 0, 0};

bool judge(int a, int b){
    if(a >= m || a < 0 || b >= m || b < 0)    return false;
    if(cover[a][b] == true || matrix[a][b] == 0) return false;
     
    return true;
}

void BFS(int i, int j){
    queue<node> q;
    grid.x = i;
    grid.y = j;
    q.push(grid);
    cover[i][j] = true;
    while(!q.empty()){
        node top = q.front();
        q.pop();
        
        for(int k = 1; k <= 4; k++){    //邻接的4个格子 
            int newx = top.x + x1[k];
            int newy = top.y + y1[k];
            if(judge(newx, newy)){
                grid.x = newx; grid.y = newy;
                q.push(grid);
            }
            
        }
    }
    
}


int main(void){
    scanf("%d%d",&m,&n);
    for(int i = 0; i < m; i++)
        for(int j = 0; j < n; j++)
            scanf("%d",matrix[i][j]);     //输入0-1矩阵 
        
    int ans = 0;     //用于存储整块的数量
    for(int i = 1; i < m; i++){
        for(int j = 1; j < n; j++){
            if(matrix[i][j] == 1 && cover[i][j] == false){
                ans++;
                BFS(i, j);     //把邻近的单元全部标记 
            }
        }
    } 
    printf("%d\n",ans);
    
}

P279页的题:

自己代码,和书上差不多:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int maxn = 100;

struct Node{
    int x, y;
    int step;
}node,S,T;

int n, m;    //行数和列数
char maze[maxn][maxn];     //迷宫
bool cover[maxn][maxn] = false;     //判断是否入过队列 
int x1[] = {0, 0, 1, -1};
int y1[] = {1, -1, 0, 0};

bool judge(int i, int j){
    if(i >= n || j >= n || i < 0 || j < 0) return false;
    if(cover[i][j] == true || maze[i][j] == '*') return false;
    return true;
}


int BFS(int i, int j){
    queue<Node> q;
    node.x = i; node.y = j; node.step = 0;
    q.push(node);
    
    while(!q.empty()){    
        Node top = q.front();
        q.pop();
        
        if(top.x == T.x && top.y == T.y)
            return top.step;
        
        for(int k = 1; k <= 4; k++){
            int newx = top.x + x1[k];
            int newy = top.y + y1[k];
            
            if(judge(newx, newy)){
                node.x = newx; node.y = newy; node.step = top.step + 1;
                q.push(node);
                cover[node.x][node.y] = true;
            }
        }
    }
    
    return -1;     //没有找到一条路径可以通往出口 
}


int main(void)
{
    scanf("%%d",&n,&m);
    for(int i = 0; i < n; i++){
        getchar();     //过滤掉后面每一层的换行符 
        for(int j = 1; j < m; j++)
            scanf("%c",&maze[i][j]);
    }
    
    scanf("%d%d%d%d",&S.x,&S.y,&T.x,&T.y);
    S.step = 0;
    printf("%d\n",BFS(S.x, S.y));
        
    
    return 0;
}

P281页的尝试代码:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

struct Node{
    int data;
}a[10];

queue<Node> q;

int main(void){
    for(int i = 0; i <= 3; i++){
        a[i].data = i;
    }
    
    q.push(a[3]);
    printf("%d %d\n",q.front().data, a[3].data);
    
    q.front().data = 200;
    printf("%d %d\n",q.front().data, a[3].data);
    
    a[3].data = 165;
    printf("%d %d\n",q.front().data, a[3].data);
    
    return 0;
}

尝试2代码:

#include <cstdio>
#include <queue>
using namespace std;

struct Node{
    int data;
}a[10];

int main(void){
    queue<int> q;
    for(int i = 1; i <= 3; i++)
        a[i].data = i;
    q.push(3);
    
    printf("%d\n",a[q.front()].data);
    
    a[q.front()].data= 400;
    printf("%d\n", a[3].data);
    return 0;
}

 

posted @ 2019-03-02 22:21  pha创噬  阅读(133)  评论(0编辑  收藏  举报