简体   繁体   中英

Minimum number of adjacent swaps required to bring largest element to the center of the matrix

We have a matrix of m rows and n columns, for even values of both m and n we have 4 centers. We need to find minimum no. of swaps required to bring the largest element to the center of the matrix. Swaps can be made with horizontally and vertically adjacent elements.

import java.util.ArrayList;
import java.util.Scanner;

public class solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int[][] arr = new int[m][n];
            int max = Integer.MIN_VALUE;
            ArrayList<Integer> x = new ArrayList<>();
            ArrayList<Integer> y = new ArrayList<>();
            for (int i = 0; i < m; i++) {
               for (int j = 0; j < n; j++) {
                   arr[i][j] = sc.nextInt();
                   if (arr[i][j] > max)
                        max = arr[i][j];
               }
           }

           for (int i = 0; i < m; i++) {
               for (int j = 0; j < n; j++) {
                   if (arr[i][j] == max) {
                       x.add(i);
                       y.add(j);
                   }
               }
           }
           ArrayList<Integer> centreX = new ArrayList<>();
           ArrayList<Integer> centreY = new ArrayList<>();
           if (m % 2 != 0 && n % 2 != 0) {
               centreX.add(m / 2);
               centreY.add(n / 2);
           }
           if (m % 2 == 0 && n % 2 == 0) {
               centreX.add(m / 2);
               centreY.add(n / 2);
               centreX.add((m / 2) - 1);
               centreY.add(n / 2);
               centreX.add(m / 2);
               centreY.add((n / 2) - 1);
               centreX.add((n / 2) - 1);
               centreY.add((m / 2) - 1);

        }
        if (m % 2 == 0 && n % 2 != 0) {
            centreX.add(m / 2);
            centreY.add(n / 2);
            centreX.add((m / 2) - 1);
            centreY.add(n / 2);
        }
        if (m % 2 != 0 && n % 2 == 0) {
            centreX.add(m / 2);
            centreY.add(n / 2);
            centreX.add(m / 2);
            centreY.add((n / 2) - 1);
        }
        int min_swap = Integer.MAX_VALUE;
        for (int i = 0; i < x.size(); i++) {
            for (int j = 0; j < centreX.size(); j++) {
                int swap = Math.abs(x.get(i) - centreX.get(j)) + Math.abs(y.get(i) - centreY.get(j));
                if (swap < min_swap)
                    min_swap = swap;
            }
        }
        System.out.println(min_swap);
    }
}
}

I am taking the input and then calculating the center of the matrix based on m and n which are the dimension of the matrix. It is passing the sample cases, but others are failing.

Input Test cases Output

I checked the code during my free time line by line

centreX.add((n / 2) - 1);
centreY.add((m / 2) - 1);

part of my code should be modified to

centreX.add((m / 2) - 1);
centreY.add((n / 2) - 1);

and wow! all test cases are passing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM