Island Hopping Codechef Solution

Island Hopping Solution Problem Code: ISLNDHOP

The nation of Islandia can be represented as an N×NN×N grid. Let (i,j)(i,j) denote the square at the ii-th row from the top and the jj-th column from the left. A cell (i,j)(i,j) is labelled 11 if it is land and 00 if it is water. Any two cells of the grid which share a side, and are both land are said to belong to the same island.

Islandia holds two contests a year, the first at (xa,ya)(xa,ya) and the second at (xb,yb)(xb,yb). Ashish will need to travel from cell (xa,ya)(xa,ya) to (xb,yb)(xb,yb). To do so, he can perform a sequence of zero or more moves. In each move if he is at (xi,yi)(xi,yi), he can perform one of two actions:

  • Type 1: Move from (xi,yi)(xi,yi) to some land cell (xj,yj)(xj,yj) that lies on a different island at a cost of |xi−xj|+|yi−yj||xi−xj|+|yi−yj|
  • Type 2: Move from (xi,yi)(xi,yi) to some land cell (xj,yj)(xj,yj) that lies on the same island at a cost of 00.

Let the beauty of a sequence of moves be the number of moves of type 11 while traveling from (xa,ya)(xa,ya) to (xb,yb)(xb,yb). We define the simple cost as the minimum cost of a sequence with beauty≤1beauty≤1.

Ashish wants to figure out the maximum beauty possible to go from (xa,ya)(xa,ya) to (xb,yb)(xb,yb) without exceeding the simple cost. However, Ashish only knows (xa,ya)(xa,ya). Find the answer for every possible land cell (xb,yb)(xb,yb).

Note that we DO NOT need to minimize the cost of the trip, only maximize the beauty without exceeding the simple cost.

Input

  • The first line contains an integer TT, the number of test cases. Then TT test cases follow.
  • The first line of each test case contains an integer NN.
  • The following NN lines each contains a string S=s1s2…sNS=s1s2…sN. The string on the ii-th line represents the ii-th row of the grid, where sjsj is 11 if the cell (i,j)(i,j) is land, and 00 if it is water.
  • The last line of each test case contains two integers xaxa and yaya where (xa,ya)(xa,ya) represents the cell where the first contest will be held. It is guaranteed that this cell will be a land cell.

Output

For each test case, print NN lines, each containing the NN space-separated integers. The jj-th value of the ii-th line represents the maximum beauty possible on a journey from (xa,ya)(xa,ya) to (i,j)(i,j) satisfying the mentioned conditions, if the cell (i,j)(i,j) is land. If (i,j)(i,j) is a water cell or belongs to the same island as (xa,ya)(xa,ya), the value to be printed is 0.

Constraints

  • 1≤T≤201≤T≤20
  • 1≤N≤2001≤N≤200
  • 1≤xa,ya≤N1≤xa,ya≤N
  • The sum of N2N2 over all cases does not exceed 4⋅1044⋅104

Subtasks

Subtask #1 (15 points):

  • N≤50N≤50
  • The sum of N2N2 over all cases does not exceed 25002500

Subtask #2 (35 points):

  • N≤100N≤100
  • The sum of N2N2 over all cases does not exceed 104104

Subtask #3 (50 points): Original constraints

Sample Input 1

2
2
00
01
2 2
5
00001
10010
00001
00000
01010
2 4

Sample Output 1

0 0 
0 0 
0 0 0 0 1 
1 0 0 0 0 
0 0 0 0 1 
0 0 0 0 0 
0 2 0 1 0 

Sample Input 2

2
3
011
010
001
2 2
9
110000011
001100001
100100111
011000010
000110011
000010100
000011111
111101001
101110110
3 1

Sample Output 2

0 0 0 
0 0 0 
0 0 1 
1 1 0 0 0 0 0 3 3 
0 0 1 1 0 0 0 0 3 
0 0 0 1 0 0 3 3 3 
0 1 1 0 0 0 0 3 0 
0 0 0 2 2 0 0 3 3 
0 0 0 0 2 0 2 0 0 
0 0 0 0 2 2 2 2 2 
1 1 1 1 0 2 0 0 2 
1 0 1 1 1 0 6 6 0 

Sample Input 3

2
7
1011101
0000000
0000000
0000000
0000000
0000000
0000000
1 1
9
111111101
100000000
100000000
100000000
100000000
100000000
100000000
100000000
111111101
9 9

Sample Output 3

0 0 1 1 1 0 2 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
1 1 1 1 1 1 1 0 4 
1 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 0 0 

Explanation

Sample 1, test case 2:

The simple cost from (2,4)(2,4) to (5,2)(5,2) is |2−5|+|4−2|=3+2=5|2−5|+|4−2|=3+2=5. We can achieve a beauty of 22 without exceeding the simple cost in the following manner:

  • Perform a move of type 11 from (2,4)(2,4) to (5,4)(5,4) at a cost of 33.
  • Perform a move of type 11 from (5,4)(5,4) to (5,2)(5,2) at a cost of 22.

Sample 3, test case 1:

The simple cost from (1,1)(1,1) to (1,7)(1,7) is |1−1|+|1−7|=0+6=6|1−1|+|1−7|=0+6=6. However we can achieve a beauty of 22 without exceeding the simple cost in the following manner:

  • Perform a move of type 11 from (1,1)(1,1) to (1,3)(1,3) at a cost of 22.
  • Perform a move of type 22 from (1,3)(1,3) to (1,5)(1,5) at a cost of 00.
  • Perform a move of type 11 from (1,5)(1,5) to (1,7)(1,7) at a cost of 22.

Note that the cost of some sequence with beauty >1>1 might be lower than the simple cost.

Sample 3, test case 2:

In the second case of third sample, the simple cost from (9,9)(9,9) to (1,9)(1,9) is |9−1|+|9−9|=8+0=8|9−1|+|9−9|=8+0=8. However we can achieve a beauty of 44 without exceeding the simple cost in the following manner:

  • Perform a move of type 11 from (9,9)(9,9) to (9,7)(9,7) at a cost of 22.
  • Perform a move of type 22 from (9,7)(9,7) to (1,7)(1,7) at a cost of 00.
  • Perform a move of type 11 from (1,7)(1,7) to (1,9)(1,9) at a cost of 22.
  • Perform a move of type 11 from (1,9)(1,9) to (1,7)(1,7) at a cost of 22.
  • Perform a move of type 11 from (1,7)(1,7) to (1,9)(1,9) at a cost of 22.

Also note that in the same sample the answer for (1,7)(1,7) will be 11, not 33 as the simple cost from (9,9)(9,9) to (1,7)(1,7) is 22. This can be achieved by performing a move of type 11 from (9,9)(9,9) to (9,7)(9,7) at cost of 22 and then a move of type 22 from (9,7)(9,7) to (1,7)(1,7) at a cost of 00.

Program:


import java.util.*;
import java.io.*;
class Main  {
    static FastReader in=new FastReader();

    static long mod=1000000007L;


    public static void main(String args[]) throws IOException {
        int t=in.nextInt();
        
        int cse=1;
        while(t-->0)
        {
          StringBuilder res=new StringBuilder();
          int n=in.nextInt();
         
          ArrayList<Integer> a1=new ArrayList<>();
          ArrayList<Integer> a2=new ArrayList<>();
           
          for(int i=0;i<n;i++)
          {
             int k=in.nextInt();
             
             if(k%2==1){
                  a2.add(k);
                  
              }
             
              else if(k%2 == 0)
              {
                  a1.add(k);
                  
              }
              
          }
          for(int i=0;i<a1.size();i++){
              res.append(a1.get(i)+" ");
          }
          for(int i=0;i<a2.size();i++){
              res.append(a2.get(i)+" ");
          }
          
        
          
          
          
          print(res);
          

        }

    }




   

    static < E > void print(E res)
    {
        System.out.println(res);
    }


   

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

        int [] readintarray(int n) {
            int res [] = new int [n];
            for(int i = 0; i<n; i++)res[i] = nextInt();
            return res;
        }
        long [] readlongarray(int n) {
            long res [] = new long [n];
            for(int i = 0; i<n; i++)res[i] = nextLong();
            return res;
        }
    }

}


Weekly Contest 247

Biweekly Contest 55

June Long Challenge 2021 Solutions

March Long Challenge 2021 Solutions

April Long Challenge 2021 Solutions

Codechef Long Challenge Solutions

February Long Challenge 2021

January Long Challenge 2021

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS

Related :

Related :

Leave a Comment