Java输入数据长度不合法如何解决
  • 板块P1141 01迷宫
  • 楼主Xing_ke
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/9/22 11:04
  • 上次更新2023/11/2 18:47:18
查看原帖
Java输入数据长度不合法如何解决
247546
Xing_ke楼主2023/9/22 11:04

80分RE后三个点,下载数据后,显示输入数据长度不合法,怎么解决啊qwq(网上没有相关解决办法,dalao救救

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        input.nextLine();
        String in;
        int[] fa = new int[n * n];
        int[][] map = new int[n][n];
        HashMap<Integer,Integer> has  =new HashMap<>();
        for (int i = 0; i < n; ++i) {
            in = input.nextLine();
            for (int j = 0, x; j < n; ++j) {
                map[i][j] = (int) in.charAt(j) - 48;
                x = i * n + j;
                fa[x] = x;
            }
        }
        int[] size = new int[1];
        for (int i = 0, x, y,id,Fa; i < m; ++i) {
            x = input.nextInt() - 1;
            y = input.nextInt() - 1;


            id = x*n+y;
            Fa = Findfa(id,fa);
            if(has.get(Fa)!=null){
                System.out.println(has.get(Fa));
            }else{
                size[0] =1;
                BFS(x,y,fa,map,n,size);
                has.put(Fa,size[0]);
                System.out.println(size[0]);
            }
        }
    }

    public static boolean Check(int x, int y, int n) {
        return x >= 0 && y >= 0 && x < n && y < n;
    }

    public static int Findfa(int x, int[] fa) {//x=i*n+j
        if (fa[x] == x) return x;
        fa[x] = Findfa(fa[x], fa);
        return fa[x];
    }

    public static void BFS(int x, int y, int[] fa, int[][] map, int n, int[] size) {
        int tx = 0, ty = 0;
        for (int i = 0; i < 4; ++i) {
            if (i == 0) {
                tx = x - 1;
                ty = y;
            } else if (i == 1) {
                tx = x + 1;
                ty = y;
            } else if (i == 2) {
                tx = x;
                ty = y - 1;
            } else {
                tx = x;
                ty = y + 1;
            }
            if (!Check(tx, ty, n)) continue;
            if(map[x][y]==map[tx][ty])continue;
            int sn = x * n + y, tn = tx * n + ty;
            if (Findfa(sn, fa) != Findfa(tn, fa)) {
                fa[tn] = fa[sn];
                size[0]++;
                BFS(tx, ty, fa, map, n, size);
            }
        }
    }
}
2023/9/22 11:04
加载中...