BFS之旅

一、hdu 1312

题目大意:’#‘不能通过,’.'可以通过, '@'起始位置,输出从起始位置能到达的砖块数。
核心思路:简单的BFS,跟老鼠走迷宫差不多。

#include <bits/stdc++.h>
using namespace std;
char room[23][23]; //储存输入数据
int dir[4][2] = {
    {-1, 0},//左
    {0, -1},//上
    {1, 0},//右
    {0, 1}//下
};//定义方向
int Wx, Hy, num;//num记录可行走的位置,Wx,Hy是记录矩阵的行列数
#define CHECK(x, y) (x < Wx && x >= 0 && y >= 0 && y < Hy) //是否在room中
struct node{int x, y;};//定义结构体来储存坐标
void BFS(int dx, int dy)
{
    num = 1;          //起点也在砖块内
    queue<node> q;    //队列中放坐标点
    node start, next; //结构体定义
    start.x = dx;
    start.y = dy;
    q.push(start); //入队列(起始点)
    while (!q.empty())
    {
        start = q.front();          //
        q.pop();                    //得到并弹出队列第一个数
        for (int i = 0; i < 4; i++) //按上下左右四个方向顺时针搜索
        {
            next.x = start.x + dir[i][0];
            next.y = start.y + dir[i][1];
            if (CHECK(next.x, next.y) && room[next.x][next.y] == '.')
            {
                room[next.x][next.y] = '#';//防止多次记录
                num++;
                q.push(next);//入队
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int x, y, dx, dy;
    while (cin >> Wx >> Hy)
    {
        if (!Wx && !Hy)
            break;
        for (y = 0; y < Hy; y++)
            for (x = 0; x < Wx; x++)
            {
                cin >> room[x][y];
                if (room[x][y] == '@') //读入起点
                {
                    dx = x;
                    dy = y;
                }
            }
        num = 0;
        BFS(dx, dy);
        cout << num << endl;
    }
    return 0;
}

二、nefu 558 迷宫寻路-搜索

#include <bits/stdc++.h>
using namespace std;
char room[1005][1005];
int Wx, Hy; //max
int fx[6][2] = {
    {0, -1},  //上
    {1, 0},   //右
    {0, 1},   //下
    {-1, 0}   //左
};            //模拟四个方向
bool ans = 0; //判断能否出去
struct zb{int x, y;}; //结构体坐标
bool check(int x, int y)
{
    if (x <= Wx && x >= 1 && y <= Hy && Hy >= 1)
        return 1;
    return 0;
} //判断是否在迷宫内
void BFS(int x1, int y1, int x2, int y2)
{
    zb start, next;
    queue<zb> data;
    start.x = x1;
    start.y = y1;
    data.push(start);
    while (!data.empty())
    {
        start = data.front();
        data.pop();
        for (int i = 0; i < 4; i++) //模拟四个方向
        {
            next.x = start.x + fx[i][0];
            next.y = start.y + fx[i][1];
            if (check(next.x, next.y) && room[next.x][next.y] == '*')
            {
                room[next.x][next.y] = '#'; //防止多计算
                data.push(next);
            }
            if (next.x == x2 && next.y == y2) //判断终点
            {
                ans = 1;
                break;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int x, y;
    int x1, y1, x2, y2;
    while (cin >> Hy >> Wx)
    {
        ans = 0;
        for (y = 1; y <= Hy; y++)
            for (x = 1; x <= Wx; x++)
            {
                cin >> room[x][y];
                if ((x == 1 || y == 1) && room[x][y] == '*') //当作起点
                {
                    x1 = x;
                    y1 = y;
                }
                else if ((x == Wx || y == Hy) && room[x][y] == '*') //当作终点
                {
                    x2 = x;
                    y2 = y;
                }
            }
        BFS(x1, y1, x2, y2);
        if (ans == 1)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

三、洛谷P1162

#include <bits/stdc++.h>
using namespace std;
int room[35][35];
int n;
int fx[5][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
#define check(x, y) (x >= 0 && y >= 0 && x <= n+1 && y <= n+1)
struct zb
{
    int x, y;
};
void BFS(int x, int y)
{
    queue<zb> q;
    zb start, next;
    start.x = x;
    start.y = y;
    q.push(start);
    room[x][y]=2;
    while (!q.empty())
    {
        start = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            next.x = start.x + fx[i][0];
            next.y = start.y + fx[i][1];
            if (check(next.x, next.y) && room[next.x][next.y] == 0)
            {
                room[next.x][next.y]=2;
                q.push(next);
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    bool flag = 1;
    int tx, ty;
    for (int y = 1; y <= n; y++)
        for (int x = 1; x <= n; x++)
            cin >> room[x][y];
    BFS(0,0);
    for (int y = 1; y <= n; y++)
        for (int x = 1; x <= n; x++)
        {
            cout << 2-room[x][y];
            if (x == n)
                cout << endl;
            else
                cout << " ";
        }
    return 0;
}

四、POJ 3278

#include <iostream>
#include <queue>
using namespace std;
const int M = 1e5+10;
#define check(x) (x >= 0 && x <= M-10)
int ans;
int vis[M],step[M];//vis代表已经走过这个,step代表步数
int fx(int i, int x)//分三种情况
{
    if (!i)
        return x - 1;
    else if (i == 1)
        return x + 1;
    else
        return 2 * x;
}
int BFS(int n, int k)
{
    queue<int> q;
    int start, next;
    start = n;
    q.push(start);
    vis[n]=1;
    step[n]=0;
    while (!q.empty())
    {
        start = q.front();
        q.pop();
        for (int i = 0; i < 3; i++)
        {
            next = fx(i, start);
            if (check(next)&&!vis[next])
            {
                q.push(next);
                vis[next]=1;
                step[next]=step[start]+1;
            }
            if(next==k)
                return step[next];
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    if (n < k)
        cout << BFS(n, k) << endl;
    else
        cout << n-k << endl;
    return 0;
}

五、POJ 1426

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
long long BFS(int n)
{
    queue<long long>q;
    q.push(1);
    long long tmp;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp%n==0)
            return tmp;
        q.push(tmp*10);
        q.push(tmp*10+1);
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    int n;
    while (scanf("%d",&n)&&n)
    {
        long long ans=BFS(n);
        printf("%lld\n",ans);
    }
    return 0;
}

六、POJ 3126

#include <iostream>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
int vis[10010], flag[10010]; //vis是素数筛的判断,flag是BFS的去重
vector<int> num;             //储存素数
void E_sushu()               //素数筛
{
    for (int i = 2; i * i <= 9999; i++)
    {
        if (!vis[i])
        {
            for (int j = i * i; j <= 9999; j += i)
                vis[j] = 1;
        }
    }
    for (int i = 1000; i <= 9999; i++)
        if (!vis[i])
            num.push_back(i);
}

bool check(int num1, int num2) //判断函数
{
    if (flag[num1] == 1) //如果储存过就flase
        return 0;
    int tmp = 0;
    for (int i = 1; i <= 4; i++)
    {
        if (num1 % 10 == num2 % 10) //每一位进行判断数字是否相同
            tmp++;
        num1 /= 10;
        num2 /= 10;
    }
    if (tmp == 3) //题意:只变换一位
        return 1;
    else
        return 0;
}

struct cf //每一步进行储存
{
    int step, num2; //step代表步数
};

int BFS(int s1, int s2)
{
    memset(flag, 0, sizeof(flag)); //清空标记
    cf start, next;
    queue<cf> q;
    start.num2 = s1;
    start.step = 0;
    q.push(start);
    while (!q.empty())
    {
        start = q.front();
        q.pop();
        for (int i = 0; i < num.size(); i++)
        {
            next.num2 = num[i];
            next.step = start.step + 1;
            if (check(next.num2, start.num2))
            {
                if (next.num2 == s2)
                    return next.step;
                else
                {
                    q.push(next);
                    flag[next.num2] = 1;
                }
            }
        }
    }
    return -1;
}
int main()
{
    ios::sync_with_stdio(0);
    E_sushu();
    int t, s1, s2;
    cin >> t;
    while (t--)
    {
        cin >> s1 >> s2;
        if (s1 == s2)
            cout << "0" << endl;
        else
        {
            int ans = BFS(s1, s2);
            if (ans == -1)
                cout
                    << "Impossible" << endl;
            else
                cout << ans << endl;
        }
    }
    return 0;
}

七、牛客网(DBFS)小A与小B

#include <bits/stdc++.h>
using namespace std;
char r[1005][1005];
bool vis[2][1005][1005];
int Hx, Hy;
int fx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int fy[8] = {-1, 1, 0, 0, -1, 1, 1, -1};
struct zb
{
    int x, y;
};
#define check(x, y) (x >= 1 && x <= Hx && y >= 1 && y <= Hy)
queue<zb> q[2];
bool bfs(int id) //走一层
{
    int t = q[id].size();
    while (t--)
    {
        zb n = q[id].front();
        q[id].pop();
        for (int i = 0; i < 4 + (id == 0 ? 4 : 0); i++)
        {
            int tx = n.x + fx[i];
            int ty = n.y + fy[i];
            if (check(tx, ty) && !vis[id][tx][ty] && r[tx][ty] != '#')
            {
                if(vis[id^1][tx][ty])
                    return 1;
                q[id].push({tx, ty});
                vis[id][tx][ty] = 1;
            }
        }
    }
    return 0;
}
int dbfs(int x1, int y1, int x2, int y2)
{
    int step = 0;
    q[0].push({x1, y1});
    q[1].push({x2, y2});
    vis[0][x1][y1] = vis[1][x2][y2] = 1;
    while (!q[0].empty() || !q[1].empty())
    {
        step++;
        if (bfs(0))
            return step;
        if (bfs(1))
            return step;
        if (bfs(1))
            return step;
    }
    return -1;
}
int main()
{
    ios::sync_with_stdio(false);
    int tx1, ty1, tx2, ty2;
    cin >> Hy >> Hx;
    for (int y = 1; y <= Hy; y++)
        for (int x = 1; x <= Hx; x++)
        {
            cin >> r[x][y];
            if (r[x][y] == 'C')
            {
                tx1 = x;
                ty1 = y;
            }
            if (r[x][y] == 'D')
            {
                tx2 = x;
                ty2 = y;
            }
        }
    int ans = dbfs(tx1, ty1, tx2, ty2);
    if (ans == -1)
        cout << "NO" << endl;
    else
    {
        cout << "YES" << endl;
        cout << ans << endl;
    }
    return 0;
}
发布了33 篇原创文章 · 获赞 33 · 访问量 6166

猜你喜欢

转载自blog.csdn.net/acm_durante/article/details/104016538
BFS