10和 11 RE
查看原帖
10和 11 RE
981981
little_whitezzz楼主2024/1/8 21:52

求大佬帮忙调整一下

import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class pair_p{
    int first;
    int second;
    int cnt;
    public pair_p(int first,int second,int cnt){
        this.first = first;
        this.second = second;
        this.cnt = cnt;
    }
}
public class Main {
    static int N = 310;
    static int n, m;
    static int sx, sy;
    static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
    static char[][] corn = new char[N][N];
    static boolean[][] st = new boolean[N][N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        n = sc.nextInt();
        m = sc.nextInt();
        HashMap<Character, int[]> map = new HashMap<>();
        for (int i = 0; i < n; i++)
            corn[i] = sc.next().toCharArray();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (corn[i][j] == '@') {
                    sx = i;
                    sy = j;
                }
                if (corn[i][j] >= 'A' && corn[i][j] <= 'Z') {
                    if (map.containsKey(corn[i][j])) {
                        int[] combine = new int[4];
                        for (int k = 0; k < 2; k++)
                            combine[k] = map.get(corn[i][j])[k];
                        // 加入后面
                        combine[2] = i;
                        combine[3] = j;
                        map.put(corn[i][j], combine);
                    } else
                        map.put(corn[i][j], new int[]{i, j});
                }
            }
        }
        // 直接开始bfs
        Queue<pair_p> queue = new LinkedList<>();
        queue.add(new pair_p(sx, sy, 0));
        st[sx][sy] = true;
        while (!queue.isEmpty()) {
            pair_p pairP = queue.poll();
            int x = pairP.first;
            int y = pairP.second;
            int cnt = pairP.cnt;
            if (corn[x][y] == '=') {
                System.out.println(cnt);
                return;
            }
            for (int i = 0; i < 4; i++) {
                int tx = x + dx[i];
                int ty = y + dy[i];
                if (tx >= 0 && tx < n && ty >= 0 && ty < m && st[tx][ty] == false && corn[tx][ty] != '#') {
                    // 判断特殊情况
                    //若此点是 A ~ Z 当中的某个字母
                    if (corn[tx][ty] >= 'A' && corn[tx][ty] <= 'Z') {
                        // 找到他要跳转的地方
                        int[] target = map.get(corn[tx][ty]);
                        if(target.length != 4){
                            st[tx][ty] = true;
                            queue.add(new pair_p(tx,ty,cnt + 1));
                            continue;
                        }
                        if (target[0] == tx) {
                            // 那么直接飞到target[2]去了
                            tx = target[2];
                            ty = target[3];
                            queue.add(new pair_p(tx, ty, cnt + 1));
                        } else if (target[2] == tx) {
                            tx = target[0];
                            ty = target[1];
                            queue.add(new pair_p(tx, ty, cnt + 1));
                        }
                    } else {
                        // 正常地形
                        st[tx][ty] = true;
                        queue.add(new pair_p(tx, ty, cnt + 1));
                    }
                }
            }
        }
        // 查看A的前后位置
    }
}
2024/1/8 21:52
加载中...