Untitled

 avatar
unknown
plain_text
2 years ago
1.8 kB
1
Indexable
package TurnOverGame;

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

public class turnOverGame {
	static int [][] arr;
	static int m;
	static int min;
	static void Try (int x, int y, int step) {
		if (check()) {
			if (step < m) m = step;
			return;
		}
		if (x >= 4) return;
		if (step > m) return;
		if (y+1 < 4) {
			lat(x,y);
			Try(x, y+1, step+1);
			lat(x,y);
			Try(x, y+1, step);
			
		} else {
			lat(x,y);
			Try(x+1, 0, step+1);
			lat(x,y);
			Try(x+1, 0, step);
		}
	}
	static int [] dx = {-1, 0, 1, 0};
	static int [] dy = {0, 1, 0, -1};
	static void lat(int x, int y) {
		arr[x][y] = 1 - arr[x][y];
		for (int i = 0; i < 4; i++) {
			int x1 = x + dx[i];
			int y1 = y + dy[i];
			if (x1 >= 0 && x1 < 4 && y1 >= 0 && y1 < 4) {
				arr[x1][y1] = 1 - arr[x1][y1];
			}
		}
		
	}
	static boolean check() {
		int num = arr[0][0];
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (arr[i][j] != num) return false;
			}
		}
		return true;
	}
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//TurnOverGame//turngame.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		sc.nextLine();
		for (int test_case = 1; test_case <= T; test_case++) {
			System.out.println("Case #" + test_case);
			arr = new int [4][4];
			for (int i = 0; i < 4; i++) {
				String s = sc.nextLine();
				for (int j = 0; j < 4; j++) {
					char c = s.charAt(j);
					if (c == 'b') arr[i][j] = 0;
					else if (c == 'w') arr[i][j] = 1;
				}
			}
			m = 10000;
			Try (0,0,0);
			if (m == 10000) System.out.println("impossible");
			else System.out.println(m);
		}
	}
}