SYUCTACM2023 bfs训练题解

发布时间 2023-04-08 20:46:52作者: 答案说明所有

迷宫

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
char s[105][105];
int vis[105][105];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m;
bool check(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
struct w
{
    int x, y, z;
};

void solve()
{
    cin >> n >> m;
    int stx, sty;
    int edx, edy;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            if (s[i][j] == 'S')
            {
                stx = i;
                sty = j;
            }
            if (s[i][j] == 'T')
            {
                edx = i;
                edy = j;
            }
        }
    }
    queue<w> Q;
    Q.push({stx, sty, 0});
    vis[stx][sty] = 1;
    while (!Q.empty())
    {
        w u = Q.front();
        if (u.x == edx && u.y == edy)
        {
            cout << "yes\n";
            return;
        }
        Q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = u.x + dx[i];
            int ny = u.y + dy[i];
            if (check(nx, ny) && s[nx][ny] != '*' && !vis[nx][ny])
            {
                Q.push({nx, ny, u.z + 1});
                vis[nx][ny] = 1;
            }
        }
    }
    cout << "no\n";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int __ = 1;
    // cin >> __;
    while (__--)
    {
        solve();
    }
    return 0;
}

Red and Black

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
char s[105][105];
int vis[105][105];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m;
bool check(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
struct w
{
    int x, y, z;
};
void solve()
{
    memset(vis, 0, sizeof(vis));
    memset(s, '0', sizeof(s));
    int stx, sty;
    int edx, edy;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            if (s[i][j] == '@')
            {
                stx = i;
                sty = j;
            }
        }
    }
    queue<w> Q;
    w a = {stx, sty, 0};
    Q.push(a);
    vis[stx][sty] = 1;
    int ans = 1;
    while (!Q.empty())
    {
        w u = Q.front();
        Q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = u.x + dx[i];
            int ny = u.y + dy[i];
            if (check(nx, ny) && s[nx][ny] != '#' && !vis[nx][ny])
            {
                w b = {nx, ny, u.z + 1};
                Q.push(b);
                ans++;
                vis[nx][ny] = 1;
            }
        }
    }
    cout << ans << '\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int __ = 1;
    // cin >> __;
    while (cin >> m >> n && m)
    {
        solve();
    }
    return 0;
}

仙岛求药

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
char s[105][105];
int vis[105][105];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m;
bool check(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
struct w
{
    int x, y, z;
};
void solve()
{
    memset(vis, 0, sizeof(vis));
    memset(s, '0', sizeof(s));
    int stx, sty;
    int edx, edy;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            if (s[i][j] == '@')
            {
                stx = i;
                sty = j;
            }
            if (s[i][j] == '*')
            {
                edx = i;
                edy = j;
            }
        }
    }
    queue<w> Q;
    w a = {stx, sty, 0};
    Q.push(a);
    vis[stx][sty] = 1;
    int ans = 1;
    while (!Q.empty())
    {
        w u = Q.front();
        Q.pop();
        if (u.x == edx && u.y == edy)
        {
            cout << u.z << '\n';
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            int nx = u.x + dx[i];
            int ny = u.y + dy[i];
            if (check(nx, ny) && s[nx][ny] != '#' && !vis[nx][ny])
            {
                w b = {nx, ny, u.z + 1};
                Q.push(b);
                ans++;
                vis[nx][ny] = 1;
            }
        }
    }
    cout << -1 << '\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int __ = 1;
    // cin >> __;
    while (cin >> n >> m)
    {
        solve();
    }
    return 0;
}

迷宫(三)

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
char s[105][105];
int vis[105][105];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m;
bool check(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
struct w
{
    int x, y, z;
};
void solve()
{
    memset(vis, 0, sizeof(vis));
    memset(s, '0', sizeof(s));
    int stx, sty;
    int edx, edy;
    int ans = 1e9 + 7;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            if (s[i][j] == '@')
            {
                stx = i;
                sty = j;
            }
        }
    }
    queue<w> Q;
    w a = {stx, sty, 0};
    Q.push(a);
    vis[stx][sty] = 1;
    while (!Q.empty())
    {
        w u = Q.front();
        Q.pop();
        if (u.x == 1 || u.x == n || u.y == 1 || u.y == m)
        {
            ans = min(ans, u.z);
            continue;
        }
        for (int i = 0; i < 4; i++)
        {
            int nx = u.x + dx[i];
            int ny = u.y + dy[i];
            if (check(nx, ny) && s[nx][ny] != '#' && !vis[nx][ny])
            {
                w b = {nx, ny, u.z + 1};
                Q.push(b);
                vis[nx][ny] = 1;
            }
        }
    }
    if (ans == 1e9 + 7)
        cout << -1 << '\n';
    else
        cout << ans << '\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int __ = 1;
    // cin >> __;
    while (cin >> n >> m)
    {
        solve();
    }
    return 0;
}

迷宫问题

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int a[10][10];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
bool check(int x, int y)
{
    return x >= 0 && x <= 4 && y >= 0 && y <= 4;
}
pair<int, int> pre[20][20];
int vis[20][20];
stack<pair<int, int> > sta;
int main()
{
    for (int i = 0; i <= 4; i++)
    {
        for (int j = 0; j <= 4; j++)
        {
            cin >> a[i][j];
        }
    }
    queue<pair<int, int> > que;
    que.push(make_pair(0, 0)); //{0,0}
    vis[0][0] = 1;
    while (!que.empty())
    {
        pair<int, int> u = que.front();
        que.pop();
        if (u.first == 4 && u.second == 4)
        {
            break;
        }
        for (int i = 0; i < 4; i++)
        {
            int nx = dx[i] + u.first;
            int ny = dy[i] + u.second;
            if (check(nx, ny) && a[nx][ny] != 1 && !vis[nx][ny])
            {
                que.push(make_pair(nx, ny));
                vis[nx][ny] = 1;
                pre[nx][ny] = make_pair(u.first, u.second);
            }
        }
    }
    int tx = 4;
    int ty = 4;
    while (tx != 0 || ty != 0)
    {
        int k = tx;
        sta.push(make_pair(tx, ty));
        tx = pre[tx][ty].first;
        ty = pre[k][ty].second;
    }
    sta.push(make_pair(0, 0));
    while (!sta.empty())
    {
        cout << "(" << sta.top().first << ", " << sta.top().second << ")\n";
        sta.pop();
    }
    return 0;
}

Find a way

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int n, m;
char s[205][205];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int vis[205][205];
bool check(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
struct w
{
    int x, y, val;
};
int dis[205][205];
int dist[205][205];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while (cin >> n >> m)
    {
        int st1_x;
        int st1_y;
        int st2_x;
        int st2_y;
        memset(dis, 0x3f, sizeof(dis));
        memset(dist, 0x3f, sizeof(dist));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> s[i][j];
                if (s[i][j] == 'Y')
                {
                    st1_x = i;
                    st1_y = j;
                }
                if (s[i][j] == 'M')
                {
                    st2_x = i;
                    st2_y = j;
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        queue<w> que;
        que.push({st1_x, st1_y, 0});
        vis[st1_x][st1_y] = 1;
        dis[st1_x][st1_y] = 0;
        while (que.size())
        {
            w u = que.front();
            que.pop();
            for (int i = 0; i < 4; i++)
            {
                int nx = dx[i] + u.x;
                int ny = dy[i] + u.y;
                if (check(nx, ny) && s[nx][ny] != '#' && !vis[nx][ny])
                {
                    que.push({nx, ny, u.val + 1});
                    vis[nx][ny] = 1;
                    dis[nx][ny] = u.val + 1;
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        que.push({st2_x, st2_y, 0});
        vis[st2_x][st2_y] = 1;
        dist[st2_x][st2_y] = 0;
        while (que.size())
        {
            w u = que.front();
            que.pop();
            for (int i = 0; i < 4; i++)
            {
                int nx = dx[i] + u.x;
                int ny = dy[i] + u.y;
                if (check(nx, ny) && s[nx][ny] != '#' && !vis[nx][ny])
                {
                    que.push({nx, ny, u.val + 1});
                    vis[nx][ny] = 1;
                    dist[nx][ny] = u.val + 1;
                }
            }
        }
        int ans = 1e9 + 7;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (s[i][j] == '@')
                {
                    ans = min(ans, dis[i][j] + dist[i][j]);
                }
            }
        }
        cout << ans * 11 << '\n';
    }

    return 0;
}

Oil Deposits

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
char s[105][105];
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int vis[105][105];
int n, m;
bool check(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    while (cin >> n >> m && n)
    {
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> s[i][j];
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (s[i][j] == '@' && !vis[i][j])
                {
                    ans++;
                    queue<pair<int, int>> que;
                    que.push({i, j});
                    vis[i][j] = 1;
                    while (que.size())
                    {
                        auto u = que.front();
                        que.pop();
                        for (int k = 0; k < 8; k++)
                        {
                            int nx = u.first + dx[k];
                            int ny = u.second + dy[k];
                            if (check(nx, ny) && s[nx][ny] == '@' && !vis[nx][ny])
                            {
                                que.push({nx, ny});
                                vis[nx][ny] = 1;
                            }
                        }
                    }
                }
            }
        }
        cout << ans << '\n';
    }

    return 0;
}