Untitled

 avatar
unknown
plain_text
a year ago
3.5 kB
8
Indexable
public class HugoDemVung {
	public static int N, maxCount, demVung;
	public static int[][] map, visited, visited0;
	public static int[] count;
	public static Queue queue = new Queue();
	public static int[] dx = {-1, 0, 1, 0};
	public static int[] dy = {0, 1, 0, -1};
	
	public static void bfs() {
		queue.reset();
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (map[i][j] == 0 && visited[i][j] == 0) {
					maxCount = 0;
					int index = -1;
					visited[i][j] = 1;
					queue.enqueue(i);
					queue.enqueue(j);
					count = new int[6];
					
					while(!queue.isEmpty()) {
						int newx = queue.dequeue();
						int newy = queue.dequeue();
						
						for (int k = 0; k < 4; k++) {
							int nextx = newx + dx[k];
							int nexty = newy + dy[k];
							
							if (nextx >= 0 && nextx < N && nexty >= 0 && nexty < N && visited[nextx][nexty] == 0) {
								if (map[newx][newy] == 0) {
									count[map[nextx][nexty]]++;
									queue.enqueue(nextx);
									queue.enqueue(nexty);
									visited[nextx][nexty] = 1;
								} 
								else {
									if (map[nextx][nexty] == map[newx][newy]) {
										count[map[nextx][nexty]]++;
										queue.enqueue(nextx);
										queue.enqueue(nexty);
										visited[nextx][nexty] = 1;
									}
								}
							}
						}
					}
				
					for (int k = 1; k < 6; k++) {
						if (maxCount <= count[k]) {
							maxCount = count[k];
							index = k;
						}
					}
					
					for (int k = 0; k < N; k++) {
						for (int h = 0; h < N; h++) {
							if (map[k][h] == 0 && visited[k][h] == 1) {
								map[k][h] = index;
								visited[k][h]=-1;
							}
							if (visited[k][h] == 1 && map[k][h] != 0) {
								visited[k][h] = 0;
							}
						}
					}
				}
			}
		}
	}
	
	public static void bfsDemVung() {
		visited = new int[N][N];
		queue.reset();
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (visited[i][j] == 0) {
					visited[i][j] = 1;
					demVung++;
					
					queue.enqueue(i);
					queue.enqueue(j);
					
					while(!queue.isEmpty()) {
						int newx = queue.dequeue();
						int newy = queue.dequeue();
						
						for (int k = 0; k < 4; k++) {
							int nextx = newx + dx[k];
							int nexty = newy + dy[k];
							
							if (nextx >= 0 && nextx < N && nexty >= 0 && nexty < N && visited[nextx][nexty] == 0
									&& map[nextx][nexty] == map[newx][newy]) {
								queue.enqueue(nextx);
								queue.enqueue(nexty);
								visited[nextx][nexty] = 1;
							}
						}
					}
				}
			}
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream("C:/Users/srv_training/workspace/testabc/src/input.txt"));
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for (int t1 = 1; t1 <= t; t1++) {
			N = sc.nextInt();
			map = new int[N][N];
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					map[i][j] = sc.nextInt();
				}
			}
//			for (int i = 0; i < N; i++) {
//				for (int j = 0; j < N; j++) {
//					System.out.print(map[i][j] + " ");
//				}
//				System.out.println();
//			}
			visited = new int[N][N];
			demVung = 0;
			bfs();
//			System.out.println();
//			for (int i = 0; i < N; i++) {
//				for (int j = 0; j < N; j++) {
//					System.out.print(map[i][j] + " ");
//				}
//				System.out.println();
//			}
			bfsDemVung();
			System.out.println("Case #" + t1);
			System.out.println(demVung);
		}
		sc.close();
	}
}
Editor is loading...
Leave a Comment