93分,错了最后一个点,求助,感谢大佬
查看原帖
93分,错了最后一个点,求助,感谢大佬
1217960
mugglelll楼主2024/4/4 11:07
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <numeric>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <utility>
#define LL long long
#define endl '\n'
#define LOG(a) cout<<a
using namespace std;

int n, m;
char mp[310][310];
int had[310][310];
int blarr[310][310];
const int dx[5] = {0,0,1,0,-1};
const int dy[5] = {0,1,0,-1,0};
struct node {
    int x, y;
} nodes;
LL anstm = 0x3f3f3f3f;
int main() {
    cin >> n >> m;
    int sx, sy, fx, fy;
    map<char, vector<node> >stlmp;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> mp[i][j];
            if (mp[i][j] == '@') {
                sx = i;
                sy = j;
            }
            if (mp[i][j] == '=') {
                fx = i;
                fy = j;
            }
            if (mp[i][j] >= 'A' && mp[i][j] <= 'Z') {
                stlmp[mp[i][j]].push_back({i,j});
            }
        }
    }
    // memset(had, 0x3f, sizeof(had));
    queue<node>q;
    q.push({sx,sy});
    had[sx][sy] = 0;
    blarr[sx][sy] = 1;
    while (!q.empty()) {
        auto it = q.front();
        q.pop();
        // for (int i = 1;i <= n;i++) {
        //     for (int j = 1;j <= m;j++) {
        //         cout << had[i][j];
        //     }
        //     cout << endl;
        // }
        // cout<<endl;
        if (mp[it.x][it.y] == '=') {
            anstm = had[it.x][it.y];
            break;
        }
        if (mp[it.x][it.y] <= 'Z' && mp[it.x][it.y] >= 'A') {
            int tmpi;
            if (stlmp[mp[it.x][it.y]].front().x == it.x && stlmp[mp[it.x][it.y]].front().y == it.y) {
                tmpi = 1;
            } else {
                tmpi = 0;
            }
            int ttx = stlmp[mp[it.x][it.y]][tmpi].x;
            int tty = stlmp[mp[it.x][it.y]][tmpi].y;
            had[ttx][tty] = had[it.x][it.y];
            it.x = ttx;
            it.y = tty;
        }
        for (int i = 1;i <= 4;i++) {
            int tx = it.x + dx[i];
            int ty = it.y + dy[i];
            if (tx > n || ty > m || tx < 1 || ty < 1 || mp[tx][ty] == '#' || blarr[tx][ty] == 1) {
                continue;
            }
            blarr[tx][ty] = 1;
            had[tx][ty] = had[it.x][it.y] + 1;
            q.push({tx,ty});
        }

    }

    cout << anstm;
    return 0;
}

最后的15号点错了,答案是5,但是程序的结果是4,这个是哪里有问题,有些知道错哪里了,但是不会改,感谢大佬了

2024/4/4 11:07
加载中...