Contents
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
- Maximum Product Difference Between Two Pairs
- Cyclically Rotating a Grid
- Number of Wonderful Substrings
- Count Ways to Build Rooms in an Ant Colony
Biweekly Contest 55
- Remove One Element to Make the Array Strictly Increasing
- Remove All Occurrences of a Substring
- Maximum Alternating Subsequence Sum
- Design Movie Rental System
June Long Challenge 2021 Solutions
- Maximum Frequent Subarray Sum solution codechef
- Dual Distance solution codechef
- Optimal Xor Set solution codechef
- Minimum Subtree Cover solution codechef
- Minimum Dual Area solution codechef
- Bitwise Tuples solution codechef
- Shortest Route solution codechef
- Bella ciao solution codechef
- Summer Heat solution codechef
March Long Challenge 2021 Solutions
- An Interesting Sequence ISS SOLUTION
- Tree House THOUSES SOLUTION
- Valid Paths VPATH SOLUTION
- Modular Equation MODEQ SOLUTION
- Tic Tac Toe TCTCTOE SOLUTION
- Xor Equality XOREQUAL SOLUTION
- Golf LKDNGOLF SOLUTION
- Solubility SOLBLTY SOLUTION
April Long Challenge 2021 Solutions
- Chef and Dice SDICE Solution
- Worthy Matrix KAVGMAT Solution
- Binary String MEX MEXSTR Solution
- Boolean Game BOOLGAME Solution
- Tree Permutations TREEPERM Solution
- Destroy the EMP Chip CHAOSEMP Solution
- Chef and Pair Flips PAIRFLIP Solution
- String Power STRPOW Solution
- Brahma and Shiva SHRINES Solution
- Water Sort Puzzle (Challenge) WTRSORT Solution
- World Record BOLT Solution
- Strong Language SSCRIPT Solution
- Valid Pair SOCKS1 Solution
Codechef Long Challenge Solutions
February Long Challenge 2021
- 1. Frog Sort Solution Codechef
- 2. Chef and Meetings Solution Codechef
- 3. Maximise Function Solution Codechef
- 4. Highest Divisor Solution Codechef
- 5. Cut the Cake Challenge Solution Codechef
- 6. Dream and the Multiverse Solution Codechef
- 7. Cell Shell Solution Codechef
- 8. Multiple Games Solution Codechef
- 9. Another Tree with Number Theory Solution Codechef
- 10. XOR Sums Solution Codechef
- 11. Prime Game Solution CodeChef
- 12. Team Name Solution Codechef
January Long Challenge 2021
- Chef and Division 3 DIVTHREE SOLUTION Code Chef
- Encoded String DECODEIT SOLUTION Code Chef
- Point Of Impact BILLRD SOLUTION Code Chef
- Fair Elections FAIRELCT SOLUTION Code Chef
- Watching CPL WIPL SOLUTION Code Chef
- Chef and Ants ANTSCHEF SOLUTION Code Chef
- Blackjack BLKJK SOLUTION Code Chef
- And-Or Game ORAND SOLUTION Code Chef
- Stack-Queue Sort (Challenge) SQSORT SOLUTION Code Chef
- Expected Number of SCCs RCTEXSCC SOLUTION Code Chef
- Curious Matrix CURMAT SOLUTION Code Chef
- Cool Subsets COOLSBST SOLUTION Code Chef
- Sequence Creation ARCRT SOLUTION Code Chef
- Greedy Students GRDSTD SOLUTION Code Chef
November Challenge 2020 SOLUTION CodeChef
- Ada and Dishes SOLUTION ADADISH
- Iron Magnet and Wall SOLUTION FEMA2
- Magical Candy Store SOLUTION CNDYGAME
- Unusual Queries SOLUTION UNSQUERS
- Red-Black Boolean Expression SOLUTION RB2CNF
- Chef and the Combination Lock SOLUTION CHEFSSM
- Scalar Product Tree SOLUTION SCALSUM
- Connect on a Grid (Challenge) SOLUTION CONGRID
October Lunchtime 2020 CodeChef SOLUTIONS
- AND Plus OR SOLUTION ANDOR
- Chef and Subtree MEXs SOLUTION SUBMEXS
- Chef Likes Good Sequences SOLUTION GSUB
- Cute Chef Gift SOLUTION COPAR
- Chef Is Just Throwing Random Words SOLUTION SSO
- Counting Spaghetti SOLUTION CDSUMS
- Chef and Edge Flipping SOLUTION EFLIP
Related :
- A. Shandom Ruffle SOLUTION
- B. Pear TreaP SOLUTION
- C. Sneetches and Speeches 3 SOLUTION
- D. The Grim Treaper SOLUTION
- Y. Sneetches and Speeches 1 SOLUTION
- Z. Trick or Treap SOLUTION
- A. Floor Number SOLUTION CODE FORCES
- B. Symmetric Matrix SOLUTION CODE FORCES
- C. Increase and Copy SOLUTION CODE FORCES
- D. Non-zero Segments SOLUTION CODE FORCES
- E. Rock, Paper, Scissors SOLUTION CODE FORCES
- F. Number of Subsequences SOLUTION CODE FORCES
Related :
- Chef and Easy Queries SOLUTIONS CHEFEZQ
- Covid Run SOLUTIONS CVDRUN OCTOBER CHALLENGE
- Positive AND SOLUTIONS POSAND
- Replace for X SOLUTIONS REPLESX
- Village Road Network SOLUTIONS VILLNET
- Random Knapsack SOLUTIONS RANDKNAP
- D-Dimensional MST SOLUTIONS DDIMMST
- Compress all Subsegments SOLUTIONS SEGCOMPR
- Adding Squares SOLUTIONS ADDSQURE
- Inversions SOLUTIONS INVSMOD2 OCOTBER CHALLENGE
- Rooted Minimum Spanning Tree SOLUTIONS ROOTMST