A strange matrix

You are given a matrix A containing N rows and M columns and an integer C.

Initially, all cells are assigned some value less or equal to CA[i][j] is the value of the ith row and jth column.

Each second all cell's value is increased by 1 but it can increase maximum up to C after that value of A[i][j] is unchanged.

On the 0th second, you are at (1,1) cell and want to go to (N,M) cell.

At any point in time, you can jump to any adjacent cell. If you are at (i,j), then you can go to any of the adjacent cells, (i1,j)(i+1,j)(i,j+1), and (i,j1). You can move to the adjacent cells only on one condition :

You can move to any adjacent cell if and only if the value of the cell, where you are standing, is equal to the value of the adjacent cell and you can not go outside of the matrix
Note: Jump time is negligible

Your task is to determine the minimum time to reach (N,M) from the cell.

Example:

Input:  n=2, m=2, c=3,  arr[][] = { { 2, 2 }, { 1, 3 } }
Output: 1

Approach

Java


import java.util.Objects;
import java.util.TreeSet;

public class StrangeMatrix {
    public static int nm;
    public static long a[][], mt[][], c;
    final public static long inf = (long1e17;
    public static int d[] = new int[] { -1010, -1 };

    public static boolean ok(int xint y) {
        return 0 <= x && x < n && 0 <= y && y < m;
    }

    public static void main(String[] args)  {
        n = 2;
        m = 2;
        c = 3;
        mt = new long[n][m];
        a = new long[n][m];
        int arr[][] = { { 22 }, { 13 } };
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                mt[i][j] = arr[i][j];
                a[i][j] = inf;
            }
        }
        TreeSet<PairTs = new TreeSet<>();
        a[0][0] = 0l;
        Ts.add(new Pair(0l00));
        while (!Ts.isEmpty()) {
            Pair cur = Ts.pollFirst();
            long tim = cur.a;
            int x = cur.fs;
            int y = cur.sc;
//              System.err.println(tim + " " + x + " " + y);
            for (int i = 0; i < 4; ++i) {
                int nx = x + d[i];
                int ny = y + d[i + 1];
                if (ok(nx, ny)) {
                    long curn = Math.min(mt[x][y] + tim, c);
                    long nxtn = Math.min(mt[nx][ny] + tim, c);
                    long needtobe = Math.max(curn, nxtn);
                    if (curn != nxtn)
                        needtobe = c;
                    long timr = Math.max(tim, Math.max(needtobe - mt[x][y], needtobe - mt[nx][ny]));
                    if (timr < a[nx][ny]) {
                        Ts.remove(new Pair(a[nx][ny], nx, ny));
                        a[nx][ny] = timr;
                        Ts.add(new Pair(a[nx][ny], nx, ny));
                    }
                }
            }
        }
        System.out.println(a[n - 1][m - 1]);
    }

    static class Pair implements Comparable<Pair> {
        long a;
        int fssc;

        Pair() {
        }

        Pair(long a_int fs_int sc_) {
            a = a_;
            fs = fs_;
            sc = sc_;
        }

        @Override
        public String toString() {
            return "Pair{" + "fs=" + fs + ", sc=" + sc + ", a=" + a + '}';
        }

        @Override
        public int compareTo(Pair oth) {
            int ret = Long.compare(a, oth.a);
            if (ret != 0)
                return ret;
            ret = Integer.compare(fs, oth.fs);
            if (ret != 0)
                return ret;
            return Integer.compare(sc, oth.sc);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (!(o instanceof Pair))
                return false;
            Pair pair = (Pair) o;
            return a == pair.a && fs == pair.fs && sc == pair.sc;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, fs, sc);
        }
    }
}


No comments:

Post a Comment