I. 贪吃蛇 (bfs迷宫) (2021牛客寒假算法基础集训营6)

传送门

思路: 这就是个很典型的bfs迷宫题,没啥好说的直接上代码。

利用了结构体的代码:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
   
   {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}

int n, m;
char g[105][105];
int vis[105][105];

struct node{
  int x,y,s;
};
queue <node> q;
node bg,nw,ne,ed;

void bfs()
{
    q.push(bg);
    vis[bg.x][bg.y]=1;
    while(q.size()){
        nw=q.front(); q.pop();
        if(nw.x==ed.x&&nw.y==ed.y) {
                cout << nw.s*100 << endl;
                return;
        }
        for(int i = 0; i < 4; i ++){
            ne.x = nw.x+way[i][0];
            ne.y = nw.y+way[i][1];
            if(ne.x<=0 || ne.x>n || ne.y<=0 || ne.y>n || vis[ne.x][ne.y] || g[ne.x][ne.y]=='#') continue;
            vis[ne.x][ne.y]=1;
            ne.s=nw.s+1;
            q.push(ne);
        }
    }
    cout << -1 <<endl;
}

signed main()
{
    IOS;

    cin >> n >> m;
    cin >> bg.x >> bg.y >> ed.x >> ed.y;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j ++)
            cin >> g[i][j];
    bfs();

    return 0;
}

代码2:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
   
   {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}

int n, m, sx, sy, ex, ey;
char g[105][105];
int d[105][105];

int bfs()
{
    queue<pii> q;
    mem(d, -1);
    d[sx][sy] = 0;
    q.push({sx, sy});
    while (q.size()){
        auto t = q.front(); q.pop();
        if(t.first==ex&&t.second==ey) break;
        //枚举四个方向
        for (int i = 0; i < 4; i ++ ){
            int nx = t.first + way[i][0], ny = t.second + way[i][1];
            //如果下一个方向点不越界,可通行,为走过
            if(nx<=0 || nx>n || ny<=0 || ny>n || d[nx][ny]!=-1 || g[nx][ny]=='#') continue;
            d[nx][ny] = d[t.first][t.second] + 1;
            q.push({nx, ny});
        }
    }
    return d[ex][ey];
}
signed main()
{
    IOS;
    cin >> n >> m;
    cin >> sx >> sy >> ex >> ey;
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            cin >> g[i][j];
    int ans = bfs();
    cout << (ans==-1 ? ans:ans*100) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/114419159
今日推荐