Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
29 kB
4
Indexable
Never
Ban Bong Bay
package Ban_Bong_Bay;

import java.io.FileInputStream;
import java.util.Scanner;

public class ban_bong_bay {
	static int n;
	static long Ans;
	static long [] visit;
	static long[] arr;
	
	static void backtrack(long i, long sum, int count) {
		if (count == n) {
			if (Ans < sum) Ans = sum;
			return;
		}
		
		for (int k = 0; k < n; k++) {
			if (visit[k] == 0) {
				visit[k] = 1;
				long tmp = score(k);
				i++;
				backtrack(k, sum+tmp, count + 1);
				i--;
				visit[k] = 0;
			}
		}
	}
	
	static long score(int i) {
		long a = 0;
		long b = 0;
		int l = i;
		int r = i;
		while (l > 0) {
			l--;
			if (visit[l] == 0) {
				a = arr[l];
				break;
			} 
		}
		while (r < n-1) {
			r++;
			if (visit[r] == 0) {
				b = arr[r];
				break;
			}
		}
		
		if (a == 0 && b == 0) return arr[i];
		if (a == 0 && b != 0) return b;
		if (a != 0 && b == 0) return a;
		return a*b;
	}
	
	
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Ban_Bong_Bay//bong_bay.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			n = sc.nextInt();
			arr = new long [n];
			visit = new long [n];
			for(int i = 0; i < n; i++) {
				arr[i] = sc.nextInt();
			}
			Ans = 0;
			backtrack(0, 0, 0);
			System.out.println("#" + test_case + " " + Ans);
		}

	}

}



############################################################
Bao ve NongTrang
package Bao_Ve_Nong_Trai;

import java.io.FileInputStream;
import java.util.Scanner;

public class bao_ve_nong_trai {
	static int MAX_SIZE = 1000000;
	static int [][] queue = new int[2][MAX_SIZE];
	static int rear, front;
	
	static void push(int x, int y) {
		if (rear == MAX_SIZE-1) rear = -1;
		rear++;
		queue[0][rear] = x;
		queue[1][rear] = y;
	}
	static void pop() {
		if (front == MAX_SIZE-1) front = -1;
		front++;
	}
	static int tx() {
		return queue[0][front];
	}
	static int ty() {
		return queue[1][front];
	}
	static boolean isEmpty() {
		return front == rear;
	}
	
	
	static int row, col;
	static int [][] hill;
	static int Ans;
	static int [][] visit;
	static int [] dx = {0, 1, 1, 1, 0, -1, -1, -1};
	static int [] dy = {1, 1, 0, -1, -1, -1, 0, 1};
	
	static boolean flag;
	static void bfs(int x, int y) {
		visit[x][y] = 1;
		push(x, y);
		while (!isEmpty()) {
			pop();
			int a = tx();
			int b = ty();
			for (int i = 0; i < 8; i++) {
				int x1 = a + dx[i];
				int y1 = b + dy[i];
				if (x1 >= 0 && y1 >= 0 && x1 < row && y1 < col) {
					if (hill[a][b] == hill[x1][y1] && visit[x1][y1] == 0) {
						visit[x1][y1] = 1;
						push(x1, y1);
					} else if (hill[a][b] < hill[x1][y1]&& flag) {
						flag = false;
					}
					
				}
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Bao_Ve_Nong_Trai//nongTrai.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		hill = new int [705][705];
		visit = new int [705][705];
		for (int test_case = 1; test_case <= T; test_case++) {
			rear = front = -1;
			
			row = sc.nextInt();
			col = sc.nextInt();
			Ans = 0;
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++) {
					hill[i][j] = sc.nextInt();
					visit[i][j]=0;
				}
			}
			
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++) {
					if (visit[i][j] == 0 && hill[i][j] != 0) {
						flag = true;
						bfs(i, j);
						if (flag) {
							Ans++;
						}
					}
				}
			}
			System.out.println("#" + test_case + " " + Ans);
		}

	}

}




#######################################################
Cleaning robot
package Cleaning_Robot;

import java.io.FileInputStream;
import java.util.Scanner;

public class cleaning_robot {
	static int MAX_SIZE = 10000;
	static int [][] queue = new int [2][MAX_SIZE];
	static int front, rear;
	
	static void push (int x, int y) {
		if (rear == MAX_SIZE-1) rear = -1;
		rear++;
		queue[0][rear] = x;
		queue[1][rear] = y;
	}
	static void pop() {
		if (front == MAX_SIZE-1) front = -1;
		front++;
	}
	static int tx() {
		return queue[0][front];
	}
	static int ty() {
		return queue[1][front];
	}
	static boolean isEmpty() {
		return rear == front;
	}
	
	static int [] dx = {0, 0, 1, -1};
	static int [] dy = {1, -1, 0, 0};
	
	static void reset() {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				d[i][j] = 0;
			}
		}
	}
	
	static int bfs (int x1, int y1, int x2, int y2) {
		reset();
		front = rear = -1;
		push(x1, y1);
		while (!isEmpty()) {
			pop();
			int a = tx();
			int b = ty();
			for (int i = 0; i < 4; i++) {
				int x = a + dx[i];
				int y = b + dy[i];
				if (x >= 0 && y >= 0 && x < m && y < n && d[x][y] == 0 && map[x][y] != 2) {
					d[x][y] = d[a][b] + 1;
					push(x, y);
				}
				if (x == x2 && y == y2) {
					int h = d[a][b] +1;
					return h;
				}
			}
		}
		return 0;
	}
	
	static void createDirty() {
		for (int i = 0; i < countDirty; i++ ) {
			for (int j = i+1; j <= countDirty; j++) {
				if (i != j) dirty[j][i] = dirty[i][j] = bfs(arr[0][i], arr[1][i], arr[0][j], arr[1][j]);
			}
		}
	}
	
	static void backtrack (int ind, int sumDir, int cntDir) {
		if (cntDir == countDirty) {
			if (sumDir < Ans) Ans = sumDir;
			return;
		}
		if (Ans < sumDir) return;
		for (int i = 1; i <= countDirty; i++) {
			if (visitDirty[i] == 0) {
				visitDirty[i] = 1;
				backtrack(i, sumDir + dirty[ind][i], cntDir+1);
				visitDirty[i] = 0;
			}
		}
		
	}
	
	static int Ans;
	static int m, n;
	static int [][] map, arr, dirty;
	static int [][] d;
	static int [] visitDirty;
	static int rx, ry;
	static int countDirty;
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Cleaning_Robot//robot.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			m = sc.nextInt();
			n = sc.nextInt();
			map = new int [m][n];
			d = new int [m][n];
			arr = new int [2][11];
			dirty = new int [11][11];
			countDirty = 0;
			int k = 1;
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = sc.nextInt();
					if (map[i][j] == 3) {
						arr[0][0] = i;
						arr[1][0] = j;
					}
					if (map[i][j] == 1) {
						arr[0][k] = i;
						arr[1][k] = j;
						k++;
						countDirty++;
					}
				}
			}
			Ans = 1000000;
			createDirty();
			visitDirty = new int [countDirty+1];
			for (int i = 0; i <= countDirty; i++) {
				int sum = 0;
				for (int j = 0; j <= countDirty; j++) {
					sum += dirty[i][j];
				}
				if (sum == 0) {
					Ans = -1;
					break;
				}
			}
			
			System.out.println("Case #" + test_case);
			if (Ans == -1) {
				System.out.println(-1);;
			}
			else {
				Ans = 1000000;
				backtrack(0, 0, 0);
				System.out.println(Ans);
			}
			
		}
	}

}




###################################
Connect_Processor
package Connect_Processor;

import java.io.FileInputStream;
import java.util.Scanner;

public class connect_processor {
	static int count;
	static int n;
	static int [][] map;
	static int [] dx = {1, 0, -1, 0};
	static int [] dy = {0, 1, 0, -1};
	static int [][] arr = new int [2][20];
	static int max, Ans;
	/*
	static void Try (int i, int wire, int wire_temp, int core, int core_temp) {
		if (i == count) {
			if (core < core_temp) {
				if (wire > wire_temp) {
					max = core_temp;
					Ans = wire_temp;
				}
			}
			min = wire_temp = core_temp = 0;
			return;
		}
		for (int j = 0; j < 4; j++) {
			if (check(i, j)) {
				Try(i+1, wire, min, core, core_temp + 1);
				map[x1][y1] = 0;
			}
		}
		Try(1, wire, min, core, core_temp);
	}
	*/
	
	static void Try(int tempCore, int coreNum, int wireLen) {
		if (tempCore == count) {
			if (coreNum > max)  {
				max = coreNum;
				Ans = wireLen;
			}
			else if (coreNum == max && wireLen < Ans) Ans = wireLen;
			return;
		}
		boolean flag = true;
		for (int h = 0; h < 4; h++) {
			if (check(tempCore, h)) {
				flag = false;
				int len = go(tempCore, h);
				Try(tempCore + 1, coreNum + 1, wireLen + len);
				go(tempCore, h);
			}
		}
		if (flag) Try(tempCore+1, coreNum, wireLen);
	}
	
	static boolean check(int core, int h) {
		int tempX = arr[0][core] + dx[h];
		int tempY = arr[1][core] + dy[h];
		while(tempX >= 0 && tempX < n && tempY >= 0 && tempY < n) {
			if (map[tempX][tempY] != 0) return false;
			tempX += dx[h];
			tempY+= dy[h];
		}
		return true;
	}
	
	static int go (int core, int h) {
		int len = 0;
		int tempX = arr[0][core] + dx[h];
		int tempY = arr[1][core] + dy[h];
		while(tempX >= 0 && tempX < n && tempY >= 0 && tempY < n) {
			map[tempX][tempY] = 1 - map[tempX][tempY];
			len++;
			tempX += dx[h];
			tempY+= dy[h];
		}
		return len;
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Connect_Processor//processor.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++ ){
			Ans = 10000;
			n = sc.nextInt();
			map = new int [n][n];
			count = 0;
			
			int k = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					
					
					map[i][j] = sc.nextInt();
					if ((i == 0 || j == 0 || i == n-1 || j == n-1) && map[i][j] != 0) {
						map[i][j] = 3;
					}
					if (map[i][j] == 1) {
						count++;
						arr[0][k] = i;
						arr[1][k] = j;
						k++;
					}
				}
			}
			
			max = 0;
			Try(0, 0, 0);
			System.out.println("#" + test_case + " " + Ans);
		}
	}
}



################################################
Cutting_a_Piece_of_Colored_Paper
package Cutting_a_Piece_of_Colored_Paper;

import java.io.FileInputStream;
import java.util.Scanner;

public class cutPaper {
	static int n;
	static int [][] paper;
	static int [] dx = {0, 1, 1};
	static int [] dy = {1, 1, 0};
	static int count1;
	static int count0;
	static void cutting (int x, int y, int n) {
		if (n == 1) {
			if (paper[x][y] == 1) count1++;
			if (paper[x][y] == 0) count0++;
			return;
		}
		boolean check = true;
		for (int i = x; i < x+n-1; i++) {
			if (!check) break;
			for (int j = y; j < y+n-1; j++) {
				if (paper[i][j] != paper[i][j+1] || paper[i][j] != paper[i+1][j+1] || paper[i][j] != paper[i+1][j]) {
					check = false;
					break;
				}
			}
		}
		if (check && paper[x][y] == 1) {
			count1++;
			return;
		} else if (check && paper[x][y] == 0) {
			count0++;
			return;
		}
		if (!check) {
			cutting(x, y, n/2);
			cutting(x + n/2, y, n/2);
			cutting(x, y + n/2, n/2);
			cutting(x + n/2, y + n/2, n/2);
		}
		
	}
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Cutting_a_Piece_of_Colored_Paper//paper.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			n = sc.nextInt();
			paper = new int [n][n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					paper[i][j] = sc.nextInt();
				}
			}
			count1 = 0;
			count0 = 0;
			cutting(0, 0, n);
			System.out.println("Case #"+test_case);
			System.out.println(count0+" "+count1);
		}
	}

}



####################################################
/*
package DJS;

class djs {
	static int MAX_DJS = 100;
	static int [] data = new int [MAX_DJS];
	
	static void make_set(int x) {
		data[x] = x;
	}
	
	static int find_set(int x) {
		if (x == data[x]) return x;
		else return find_set(data[x]);
	}
	
	static void _union(int x, int y) {
		data[find_set(y)] = find_set(x);
	}
	
	static void init() {
		for (int i = 0; i < MAX_DJS; i++) {
			data[i] = 0;
		}
	}
}

public class DJS {
	public static void main(String[] args) {
		
	}
}
*/




########################################################
Fast_Robot
package Fast_Robot;

import java.io.FileInputStream;
import java.util.Scanner;

public class fast_robot {
	static int MAX_SIZE = 10000;
	static int [][] queue = new int [2][MAX_SIZE];
	static int rear, front;
	
	static void push (int x, int y) {
		if (rear == MAX_SIZE - 1) rear = -1;
		rear++;
		queue[0][rear] = x;
		queue[1][rear] = y;
	}
	static void pop () {
		if (front == MAX_SIZE - 1) front = -1;
		front++;
	}
	static int tx() {
		return queue[0][front];
	}
	static int ty() {
		return queue[1][front];
	}
	static boolean isEmpty() {
		return rear == front;
	}
	
	static int [] dx = {1, 0, -1, 0};
	static int [] dy = {0, 1, 0, -1};
	
	static void bfs(int x, int y) {
		push(x, y);
		while (!isEmpty()) {
			pop();
			int a = tx();
			int b = ty();
			for (int i = 0; i < 4; i++) {
				int x1 = tx() + dx[i];
				int y1 = ty() + dy[i];
				while (x1 >= 0 && y1 >= 0 && x1 < m && y1 < n && map[x1][y1] == 0) {
					if (visit[x1][y1] == -1) {
						visit[x1][y1] = visit[a][b] + 1;
						push(x1, y1);
					}
					if (x1 == ex && y1 == ey) {
						return;
					}
					x1 += dx[i];
					y1 += dy[i];	
				}
			}
		}
	}
	
	static int n, m, sx, sy, ex, ey;
	static int [][] map, visit;
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Fast_Robot//robot.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			rear = front = -1;
			n = sc.nextInt();
			m = sc.nextInt();
			map = new int [m][n];
			visit = new int [m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					visit[i][j] = -1;
				}
			}
			sy = sc.nextInt() - 1;
			sx = sc.nextInt() - 1;
			ey = sc.nextInt() - 1;
			ex = sc.nextInt() - 1;
			sc.nextLine();
			for (int i = 0; i < m; i++) {
				String a = sc.nextLine();
				for (int j = 0; j < n; j++) {
					map[i][j] = a.charAt(j) - '0';
				}
			}
			bfs(sx, sy);
			System.out.println(visit[ex][ey]);
			
		}
	}
}

###################################
FindCycle
package FindCycle;

import java.io.FileInputStream;
import java.util.Scanner;

public class findCycle {
	static int [] checkCycle;
	static int x;
	static int y;
	static int [][] arr;
	static boolean check;
	static void Try(int a){
		if(check) return;
		checkCycle[a]=0;
		for(int i=a+1; i< y; i++){
			if(arr[i][0] == arr[a][1]){
				for(int j=0; j<i;j++){
					if(arr[i][1] == arr[j][0]){
						check =true;
					}
					Try(i);
					checkCycle[a]=1;
				}
			}
		}
		
	}
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//FindCycle//findCycle.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int testcase = 1; testcase <= T; testcase++){
			System.out.println("Case #" + testcase);
			int x = sc.nextInt();
			y = sc.nextInt();
			int Ans = 0;
			arr = new int [y][2];
			for (int i = 0; i < y; i++) {
				arr[i][0] = sc.nextInt();
				arr[i][1] = sc.nextInt();
			}
			checkCycle = new int [x];
			check = false;
			Try(0);
			if (check) Ans = 1;
			System.out.println(Ans);
		}
	}
}





##################################
package findMode;

import java.io.FileInputStream;
import java.util.Scanner;

public class findMode {

	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//findMode//mode.txt"));
		Scanner sc = new Scanner(System.in);
		for(int test_case = 1; test_case <= 10; test_case++) {
			sc.nextInt();
			int arr [] = new int [201];
			for (int i = 0; i < 1000; i++) {
				int mark = sc.nextInt();
				arr [mark] += 1;
			}
			int max = 0;
			int Answer = 0;
			for (int i = 0; i < 201; i++) {
				if (arr[i] >= max) {
					max = arr[i]; 
				} 
			}
			for (int i = 200; i >= 0; i--) {
				if (arr[i] == max) {
					Answer = i;
					break;
				}
			}
			System.out.println("#" + test_case + " " + Answer);
		}
	}

}




########################################3
package Gold_mining;

import java.io.FileInputStream;
import java.util.Scanner;

public class gold_mining {
	static int MAX_SIZE = 1000;
	static int [][] queue = new int [2][MAX_SIZE];
	static int rear, front;
	static void push(int x, int y) {
		if (rear == MAX_SIZE-1) {
			rear = -1;
		}
		rear++;
		queue[0][rear] = x;
		queue[1][rear] = y;
	}
	static void pop() {
		if (front == MAX_SIZE-1) {
			front = -1;
		}
		front++;
	}
	static int tx () {
		return queue[0][front];
	}
	static int ty () {
		return queue[1][front];
	}
	static boolean isEmpty() {
		return rear == front;
	}
	
	
	static int n, m;
	static int [][] map;
	static int [][] temp;
	static int [][] arr;
	static int [] check;
	static int [][] gold;
	static int u;
	static int Ans;
	static int max;
	
	static void resetMap() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				map[i][j] = temp[i][j];
			}
		}
		for (int i = 0; i < m; i++) {
			map[arr[0][i]][arr[1][i]] = -1;
		}
	}
	static void resetGold() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				gold[i][j] = 0;
			}
		}
	}
	static int [] dx = {1, 0, -1, 0};
	static int [] dy = {0, 1, 0, -1};
	
	static void bfs(int x, int y) {
		map[x][y] = 0;
		push(x,y);
		while (!isEmpty()) {
			pop();
			int a = tx();
			int b = ty();
			for (int i = 0; i < 4; i++) {
				int x1 = a + dx[i];
				int y1 = b + dy[i];
				if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < n && map[x1][y1] != 0) {
					gold[x1][y1] = gold[a][b] + 1;
					map[x1][y1] = 0;
					push(x1,y1);
				}
			}
		}
	}
	
	
	static void findMax() {
		int a, b = 0;
		for (int i = 0; i < m; i++) {
			a = gold[arr[0][i]][arr[1][i]];
			if (b < a) {
				b = a;
				check[u] = b;
			}
		}
		u++;
	}
	static int findMin() {
		int min = 100000;
		for (int i = 0; i < n*n; i++) {
			if (check[i] != 0) {
				if (min > check[i]) min = check[i];
			}
		}
		for (int i = 0; i < n*n; i++) {
			check[i] = 0;
		}
		return min;
		
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Gold_mining//gold.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			u = 0;
			Ans = 0;
			front = rear = -1;
			max = 0;
			n = sc.nextInt();
			check = new int [n*n];
			map = new int [n][n];
			temp = new int [n][n];
			gold = new int [n][n];
			m = sc.nextInt();
			arr = new int [2][m];
			for (int i = 0; i < m; i++) {
				arr[0][i] = sc.nextInt()-1;
				arr[1][i] = sc.nextInt()-1;
			}
			
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = sc.nextInt();
					temp[i][j] = map[i][j];
				}
			}
			
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (map[i][j] == 1) {
						bfs(i, j);
						findMax();
						resetGold();
						resetMap();
					}
				}
			}
			
			Ans = findMin();
			
			System.out.println("Case #" + test_case);
			System.out.println(Ans);
		}
	}
}




#############################################
package Grid_Axit;

import java.io.FileInputStream;
import java.util.Scanner;

public class grid_acid {
	static int MAX_SIZE = 100000;
	static int [][] queue = new int [MAX_SIZE][2];
	static int rear, front;
	static void push(int x, int y) {
		if(rear == MAX_SIZE - 1) rear = -1;
		rear++;
		queue[rear][0] = x;
		queue[rear][1] = y;
	}
	
	static void pop() {
		if(front == MAX_SIZE -1) front = -1;
		front++;
	}
	
	static boolean isEmpty() {
		return rear == front;
	}
	
	
	static int m, n, ex, ey, ax, ay;
	static int count1, count2;
	static int [][] map;
	static int [] dx = {0, 1, 0, -1};
	static int [] dy = {1, 0, -1, 0};
	
	static void bfs(int x, int y) {
		push(x, y);
		while (!isEmpty()) {
			pop();
			int a = queue[front][0];
			int b = queue[front][1];
			for(int i = 0; i < 4; i++) {
				int x1 = a + dx[i];
				int y1 = b + dy[i];
				if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && map[x1][y1] == 1) {
					map[x1][y1] = map[a][b] + 1;
					push(x1, y1);
				}
			}
			System.out.println();
		}
	}
	
	static void checkCell() {
		for(int i = 0; i < 4; i++) {
			int x1 = ex + dx[i];
			int y1 = ey + dy[i];
			if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m) {
				if (map[x1][y1] > 1 || (map[x1][y1] == 1 && x1 == ax && y1 == ay)) {
					if (map[x1][y1] > count1) {
						count1 = map[x1][y1];
					}
				} else {
					count1 = count2 = -1;
					return;
				}
				
			}
		}
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++){
				if (map[i][j] == 1) {
					count2 = -1;
					return;
				} else if (map[i][j] > count2){
					count2 = map[i][j];
				}
			}
		}
	}
	
	
	
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Grid_Axit//acid.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			rear = front = -1;
			n = sc.nextInt();
			m = sc.nextInt();
			ax = sc.nextInt() - 1;
			ay = sc.nextInt() - 1;
			map = new int [n][m];
			boolean flag = true;
			count1 = count2 = -1;
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < m; j++){
					map[i][j] = sc.nextInt();
					if(map[i][j] == 2) {
						if(i == 0 || i == n - 1 || j == 0 || j == m - 1) {
							System.out.println("Case #" + test_case);
							System.out.println(count1 + " " + count2);
							flag = false;
						}
						ex = i;
						ey = j;
					}
				}
			}
			if (!flag) continue;
			boolean check = true;
			
			// neu acid do vao da thi return -1
			if (map[ax][ay] != 0) {
				bfs(ax, ay);
			} 
			else {
				check = false;
			}
			
			if (check) {
				checkCell();
			}
			
			System.out.println("Case #" + test_case);
			System.out.println(count1 + " " + count2);
		}

	}

}
 




#####################################
package Hugo;

import java.io.FileInputStream;
import java.util.Scanner;

public class hugo1 {
		static int MAX_SIZE = 10000;
		static int [][] queue = new int [2][MAX_SIZE];
		static int rear, front;
		
		static void push (int x, int y) {
			if (rear == MAX_SIZE - 1) rear = -1;
			rear++;
			queue[0][rear] = x;
			queue[1][rear] = y;
		}
		
		static void pop() {
			if (front == MAX_SIZE - 1) front = -1;
			front++;
		}
		static boolean isEmpty() {
			return rear == front;
		}
		static int m, n;
		static int hx, hy;
		static int [][] visit, fire, lake, diamond, exit;
		static int sumDia;
		static int [] dx = {1, 0, -1, 0};
		static int [] dy = {0, 1, 0, -1};
		
		static void bfsFire() {
			while (!isEmpty()) {
				pop();
				int a = queue[0][front];
				int b = queue[1][front];
				for (int i = 0; i < 4; i++) {
					int x1 = a + dx[i];
					int y1 = b + dy[i];
					if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && lake[x1][y1] == 0 && fire[x1][y1] == 10000) {
						fire[x1][y1] = fire[a][b] + 1;
						push(x1, y1);
					}
				}
			}
		}
		
		static void backtrackHugo(int x, int y, int time, int sum) {
			if (exit[x][y] == 1) {
				if (sumDia < sum) sumDia = sum;
			}
			
			for (int i = 0; i < 4; i++) {
				int x1 = x + dx[i];
				int y1 = y + dy[i];
				if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && visit[x1][y1] == 0) {
					if(time + 1 < fire[x1][y1] && lake[x1][y1] == 0) {
						visit[x1][y1] = 1;
						backtrackHugo(x1, y1, time+1, sum+diamond[x1][y1]);
						visit[x1][y1] = 0;
					} else if (lake[x1][y1] == 1) {
						visit[x1][y1] = 1;
						backtrackHugo(x1, y1, time+2, sum+diamond[x1][y1]);
						visit[x1][y1] = 0;
					}
					
				}
			}
		}
		
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Hugo//hugo.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			front = rear = -1;
			n = sc.nextInt();
			m = sc.nextInt();
			visit = new int [n][m];
			fire = new int [n][m];
			lake = new int [n][m];
			exit = new int [n][m];
			diamond = new int [n][m];
			
			// input hugo 
			hx = sc.nextInt()-1;
			hy = sc.nextInt()-1;
			
			//input fire
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					fire[i][j] = 10000;
				}
			}
			int a = 0;
			int b = 0;
			int num = sc.nextInt();
			for (int i = 0; i < num; i++) {
				a = sc.nextInt()-1;
				b = sc.nextInt()-1;
				fire[a][b] = 0;
				push(a, b);
			}
			
			//input lake
			num = sc.nextInt();
			for (int i = 0; i < num; i++) {
				a = sc.nextInt()-1;
				b = sc.nextInt()-1;
				lake[a][b] = 1;
			}
			
			//input exit
			num = sc.nextInt();
			for (int i = 0; i < num; i++) {
				a = sc.nextInt()-1;
				b = sc.nextInt()-1;
				exit[a][b] = 1;
			}
			
			//input diamond
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					a = sc.nextInt();
					if (a != 0) diamond[i][j] = a;
				}
			}
			sumDia = -1;
			bfsFire();
			visit[hx][hy] = 1;
			backtrackHugo(hx, hy, 0, diamond[hx][hy]);
			System.out.println("Case #" + test_case);
			System.out.println(sumDia);
		}
	}

}




#######################################
package Hugo_Giao_Hang;

import java.io.FileInputStream;
import java.util.Scanner;

public class hugo_giao_hang {
	static int hx, hy, sx, sy, n, Ans;
	static int [][] arr, map;
	static int [] visit;
	
	static int distance(int i, int j) {
		int a = arr[i][0] - arr[j][0];
		int b = arr[i][1] - arr[j][1];
		if (a < 0) a = 0 - a;
		if (b < 0) b = 0 - b;
		return a+b;
	}
	
	
	static void createMatrix() {
		for (int i = 0; i < n + 2; i++) {
			for (int j = 0; j < n + 2; j++) {
				int x = distance(i, j);
				map[i][j] = x;
			}
		}
	}
	
	static void Try(int x, int sum_distance, int count) {
		if (count == n) {
			sum_distance += map[x][n+1];
			if (sum_distance < Ans) Ans = sum_distance;
			return;
		}
		if (Ans < sum_distance) return;
		for (int i = 1; i <= n; i++) {
			if (visit[i] == 0) {
				visit[i] = 1;
				Try(i, sum_distance + map[x][i], count+1);
				visit[i] = 0;
			}
		}
	}
	
	
	
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Hugo_Giao_Hang//giao_hang.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			sx = sc.nextInt();
			sy = sc.nextInt();
			hx = sc.nextInt();
			hy = sc.nextInt();
			n = sc.nextInt();
			arr = new int [n+2][2];
			for(int i = 1; i < n+1; i++) {
				arr[i][0] = sc.nextInt();
				arr[i][1] = sc.nextInt();
			}
			arr[0][0] = sx;
			arr[0][1] = sy;
			arr[n+1][0] = hx;
			arr[n+1][1] = hy;
			map = new int [n+2][n+2];
			createMatrix();
			Ans = 1000000000;
			visit = new int [n+2];
			visit[0] = 1;
			Try(0, 0, 0);
			
			
			System.out.println("Case #" + test_case + " " + Ans);
		}
	}
}





###############################
package inorderTraversal;

import java.io.FileInputStream;
import java.util.Scanner;

public class inorder {
	static void Inorder(char [] c, int [] left, int [] right, int i) {
		if (c[i] == 0) return;
		Inorder(c, left, right, left[i]);
		System.out.print(c[i]);
		Inorder(c, left, right, right[i]);
	}
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//inorderTraversal//inorder.txt"));
		Scanner sc = new Scanner(System.in);
		int N;
		int[] Firstchild, Secondchild;
		char[] Alpha;
		for(int test_case = 1; test_case <= 10; test_case++) {
			N = sc.nextInt();
			Firstchild = new int [N+1];
			Secondchild = new int [N+1];
			Alpha = new char[N+1];

			for(int i = 0; i < N; i++) {
				int addr = sc.nextInt();
				String buf = sc.next();
				
				Alpha[addr] = buf.charAt(0);
				
				if(addr*2 <= N) {
					Firstchild[addr] = sc.nextInt();
					if(addr*2 + 1 <= N) {
						Secondchild[addr] = sc.nextInt();
					}
				}
			}
			System.out.print("#" + test_case + " ");
			Inorder(Alpha, Firstchild, Secondchild, 1);
			System.out.println();
		}
	}
}



################################3
package Ladder1;

import java.io.FileInputStream;
import java.util.Scanner;

public class ladder {
	static int SIZE = 100;
	static int [][] matrix = new int [SIZE][SIZE];
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Ladder1//ladder.txt"));
		Scanner sc = new Scanner(System.in);
		for(int test_case = 1; test_case <= 10; test_case++) {
			int T = sc.nextInt();
			int x = 0;
			int y = 0;
			for (int i = 0; i < SIZE; i++) {
				for (int j = 0; j < SIZE; j++) {
					matrix[i][j] = sc.nextInt();
					if (matrix[i][j] == 2) {
						x = i;
						y = j;
					}
				}
			}
			while (x > 0) {
				matrix[x][y] = 0;
				if (y-1 >= 0 && matrix[x][y-1] == 1) {
					y--;
				} else if (y+1 < 100 && matrix[x][y+1] == 1) {
					y++;
				} else {
					x--;
				}
			}
			System.out.println("#"+test_case+ " "+y);
			
		}
	}
}