阿里巴巴2021校招

阿里巴巴2021校招

在这里插入图片描述

  static List<String> val = new ArrayList<>();

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        //数据组数
        int num = sc.nextInt();
        int a = 1;
        for (int i = 0; i < num; i++) {
            //行数
            int N = sc.nextInt();
            //列数
            int L = sc.nextInt();
            int[][] arr = getInput(N, L, sc);
            int[][] targetArr = getInput(N, L, sc);
            getPath(arr, targetArr);
        }
        for (String s : val) {
            System.out.println(s);
        }
    }
//        1 测试组
//        3 2 矩阵3行2列
//        01 11 10 源矩阵
//        11 00 10 目标矩阵
	//输入函数
    public static int[][] getInput(int N, int L, Scanner sc) {
        int[][] arr = new int[N][L];
        for (int j = 0; j < N; j++) {
            String next = sc.next();
            for (int k = 0; k < L; k++) {
                arr[j][k] = next.charAt(k) & 1;
            }
        }
        return arr;
    }
	
    public static void getPath(int[][] arr, int[][] targetArr) {
        //第某列
        int pre = Integer.MIN_VALUE;
        int index = 0;
        if (index == 0 && getWay(arr, targetArr)) {
            val.add(0 + "");
            return;
        }
        while (true) {
            index++;
            int maxDiff = getMaxDiff(arr, targetArr);

            setVal(arr, maxDiff);
            if (getWay(arr, targetArr)) {
                val.add(index + "");
                break;
            }
            if (maxDiff == -1||maxDiff == pre || index == arr[0].length) {
                val.add("impossible");
                break;
            }
            pre = maxDiff;
        }
    }

    //找出最多不同的列
    public static int getMaxDiff(int[][] arr, int[][] targetArr) {

        for (int j = 0; j < targetArr[0].length; j++) {
            int count = 0;
            int targetCount = 0;
            for (int i = 0; i < targetArr.length; i++) {
                count += targetArr[i][j];
            }
            for (int i = 0; i < arr.length; i++) {
                targetCount += arr[i][j];
            }
            if (count + targetCount == arr.length) {
                return j;
            }

        }
        return -1;
    }
    //设置值
    public static void setVal(int[][] arr, int L) {

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[j].length; j++) {
                if (j == L) {
                    if (arr[i][j] == 1) {
                        arr[i][j] = 0;
                    }else{
                        arr[i][j] = 1;
                    }
                }
            }
        }
    }
	//判断是否为目标矩阵
    public static boolean getWay(int[][] arr, int[][] targetArr) {
        List<String> arrList = new ArrayList<>();
        List<String> targetList = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            String str = "";
            for (int e : arr[i]) {
                str += e;
            }
            arrList.add(str);
        }
        for (int i = 0; i < targetArr.length; i++) {
            String str = "";
            for (int e : targetArr[i]) {
                str += e;
            }
            targetList.add(str);
        }
        boolean[] flag = new boolean[arr.length];

        for (String str : arrList) {
            //判定条件
            if (targetList.indexOf(str) != -1 && flag[targetList.indexOf(str)] != true) {
                flag[targetList.indexOf(str)] = true;
            }
        }
        for (boolean e : flag) {
            if (!e) {
                return false;
            }
        }
        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值