为啥我的代码,上下左右,先走的顺序不一样,答案就不一样,玄学!
查看原帖
为啥我的代码,上下左右,先走的顺序不一样,答案就不一样,玄学!
226030
HDawn楼主2020/7/14 19:15
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n, m;

string str[310];
int a[310][310];
queue<pair<int, int> > que;
map<char, pair<int, pair<int, int> > > map1;
map<pair<int, int>, pair<int, int> > map2;

void check(int x, int y, int xx, int yy) {
    if (str[x][y] == '.') {
        que.push(make_pair(x, y));
        a[x][y] = a[xx][yy] + 1;
    } else if (str[x][y] == '=') {
        cout << a[xx][yy] + 1 << endl;
        exit(0);
    } else if (str[x][y] >= 'A' && str[x][y] <= 'Z') {
        que.push(make_pair(map2[make_pair(x, y)].first, map2[make_pair(x, y)].second));
        a[map2[make_pair(x, y)].first][map2[make_pair(x, y)].second] = a[xx][yy] + 1;
    }
}

int main() {
    cin >> n >> m;
    int x, y;
    for (int i = 0; i < n; ++i) {
        cin >> str[i];
        for (int j = 0; j < m; ++j) {
            if (str[i][j] >= 'A' && str[i][j] <= 'Z') {
                char c = str[i][j];
                if (map1[c].first == 0) {
                    map1[c].first = 1;
                    map1[c].second.first = i;
                    map1[c].second.second = j;
                } else {
                    map2[map1[c].second] = make_pair(i, j);
                    map2[make_pair(i, j)] = map1[c].second;
                }
            } else if (str[i][j] == '@') {
                x = i, y = j;
            }
        }
    }
    map1.clear();
    que.push(make_pair(x, y));
    while (!que.empty()) {
        x = que.front().first;
        y = que.front().second;
        que.pop();
        if (x + 1 < n && str[x + 1][y] != '#' && a[x + 1][y] == 0)
            check(x + 1, y, x, y);
        if (y + 1 < m && str[x][y + 1] != '#' && a[x][y + 1] == 0)
            check(x, y + 1, x, y);
        if (x - 1 >= 0 && str[x - 1][y] != '#' && a[x - 1][y] == 0)
            check(x - 1, y, x, y);
        if (y - 1 >= 0 && str[x][y - 1] != '#' && a[x][y - 1] == 0)
            check(x, y - 1, x, y);
        if (str[x][y] >= 'A' && str[x][y] <= 'Z') a[x][y] = 0;
    }

    return 0;
}
50 50
##################################################
#DC#A#B..C.D#X.................#........#....#...#
#..#.###.####..................#....#...#..#.#.#.#
#..#..#..##.#..................#....#...#..#.#.#.#
#..#..###...#..................#....#...#..#.#.#.#
#..##..##...#..................#....#...#..#.#.#.#
#.J#I#.......############......#....#...#..#.#.#.#
#..##.#.#.#..#.#.....#.........#....#...#..#.#.#.#
#..#..............##.#..#......#....#...#..#.#.#.#
#..#.......#####...#.#..#......#....#...#..#.#.#.#
#..#.......#S......#.####......#....#...#..#.#.#.#
#..#..######.......#....#......#....#...#..#.#.#.#
#..#..#.............#..M#...........#......#...#.#
##.#####.#.#####################################.#
#..####.........#######....T.U.V..W....V..T..U..W#
#.#####.###############.##########################
#..#.....#..............#.#..#.#...#....#........#
#..#.....#..###########.#...#.#..#...#.....#..#..#
#.E#.....#..#....#..#...####.#.#..#...#.....#....#
#.##......#.#..#..#..#......#.#.#..#...#.....#..S#
#..#.######.#...#..#..#.####.#.#..#...#.....#....#
#..#......#.###..#..#...#...#.#..#...#.....#.#...#
#..######.#.#...###..#..#.#..#.#...#....#.....#..#
#......M#.###......#....#.#..#.#..#..#.#...#.....#
#.#######.#.#.####################################
#.........#.....................................K#
##################################################
#..#...#..........#...#..#................#.....##
#.........#...#..#.......##.#.....#.#....##.#..#K#
#....#.#..........#..#..#...#..#....#....#...#...#
#...........#.....#...............#..............#
##############################.###################
#.........F.....G....H.....G.....I...H......F....#
#.##############################################.#
#.#........#...................................#.#
#J#...###...###...#...###.....###.#...#..###...#.#
##....#..#..#....#.#..#..#....#...##..#..#..#..#.#
#.....#B..#.###.#####.#...#...###.#.#.#..#...#.#.#
#.....#..#..#...#...#.#..#....#...#..##..#..#..#.#
####..###...###.#...#.###.....###.#...#..###...#.#
#..........#...................................#.#
#.##########...................................#.#
#........#........N.N.O.O.R.R.................##.#
########.#.................................###...#
#........#..........................########....Y#
#........######..#Q.#...#############......#######
#.#......#...L#..####...#....#....#.#.....Q#X#...#
#.##.....#..###.........#..#.#..#...########.#Z#.#
#@..APPLE#.....#...........#....#.........Y#.Z.#.=
##################################################

272

  • 下上右左 答案 272
if (x + 1 < n && str[x + 1][y] != '#' && a[x + 1][y] == 0)
            check(x + 1, y, x, y);
        if (x - 1 >= 0 && str[x - 1][y] != '#' && a[x - 1][y] == 0)
            check(x - 1, y, x, y);
        if (y + 1 < m && str[x][y + 1] != '#' && a[x][y + 1] == 0)
            check(x, y + 1, x, y);
        if (y - 1 >= 0 && str[x][y - 1] != '#' && a[x][y - 1] == 0)
            check(x, y - 1, x, y);
        if (str[x][y] >= 'A' && str[x][y] <= 'Z') a[x][y] = 0;
  • 右左下上 答案 273
if (y + 1 < m && str[x][y + 1] != '#' && a[x][y + 1] == 0)
            check(x, y + 1, x, y);
        if (y - 1 >= 0 && str[x][y - 1] != '#' && a[x][y - 1] == 0)
            check(x, y - 1, x, y);
        if (x + 1 < n && str[x + 1][y] != '#' && a[x + 1][y] == 0)
            check(x + 1, y, x, y);
        if (x - 1 >= 0 && str[x - 1][y] != '#' && a[x - 1][y] == 0)
            check(x - 1, y, x, y);
        if (str[x][y] >= 'A' && str[x][y] <= 'Z') a[x][y] = 0;
  • 上下左右 答案 275
if (x - 1 >= 0 && str[x - 1][y] != '#' && a[x - 1][y] == 0)
            check(x - 1, y, x, y);
        if (x + 1 < n && str[x + 1][y] != '#' && a[x + 1][y] == 0)
            check(x + 1, y, x, y);
        if (y - 1 >= 0 && str[x][y - 1] != '#' && a[x][y - 1] == 0)
            check(x, y - 1, x, y);
        if (y + 1 < m && str[x][y + 1] != '#' && a[x][y + 1] == 0)
            check(x, y + 1, x, y);
        if (str[x][y] >= 'A' && str[x][y] <= 'Z') a[x][y] = 0;

有没有大佬知道这是为什么呀???

2020/7/14 19:15
加载中...