POJ 1753 뒤 집기 게임 매 거 진 (JAVA)

2651 단어 ACM 초기
POJ 1753   제목 링크 http://poj.org/problem?id=1753
import java.util.Scanner;

public class FlipGame {

	/*
	 *        
	 */
	public static boolean isFinish(int[][] fields) {
		//       
		int flag_color = 0;
		for (int i = 0; i < fields.length; i++) {
			for (int j = 0; j < fields[0].length; j++) {
				if (i == 0 && j == 0) {
					flag_color = fields[i][j];
					continue;
				}

				if (fields[i][j] != flag_color) {
					return false;
				}
			}
		}
		return true;

	}

	public static void flip(int[][] fields, int index) {

		//   
		int i = (index - 1) / fields.length;
		int j = (index - 1) % fields.length;
		fields[i][j] = fields[i][j] & 2;

		if (i > 0)
			fields[i - 1][j] = (fields[i - 1][j] + 1) % 2;

		if (j > 0)
			fields[i][j - 1] = (fields[i][j - 1] + 1) % 2;

		if (i < 3)
			fields[i + 1][j] = (fields[i + 1][j] + 1) % 2;

		if (j < 3)
			fields[i][j + 1] = (fields[i][j + 1] + 1) % 2;

	}

	public static boolean combine_recut(int[] arr, int start, int[] result, int index, int NUM, int arr_len,
			int[][] fields) {

		for (int i = start; i < arr_len - (NUM - index) + 1; i++) {
			result[index] = i;
			if (index == NUM - 1) {
				int[][] tmp_f = new int[4][4];
				for (int j = 0; j < fields.length; j++) {
					tmp_f[j] = fields[j].clone();
				}

				for (int j = 0; j < NUM; j++) {
					System.out.print(arr[result[j]] + "\t");
					//     
					flip(tmp_f, arr[result[j]]);
				}	

				if (isFinish(tmp_f)) {
					System.out.println("success");
					return true;
				}

				System.out.print("
"); } else { if (combine_recut(arr, i + 1, result, index + 1, NUM, arr_len, fields)) return true; } } return false; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int[][] fields = new int[4][4]; int lines = -1; String line = null; while (++lines < 4) { line = input.nextLine().trim();; char[] ar = line.toCharArray(); for (int i = 0; i < ar.length; i++) { fields[lines][i] = ar[i] == 'b' ? 1 : 0; } } if (isFinish(fields)) { System.out.println(0); return; } int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; for (int i = 3; i <= arr.length; i++) { System.out.println("--------------------------------------------------------------------"); System.out.println("count : " + i); int[] result = new int[i]; if (combine_recut(arr, 0, result, 0, i, arr.length, fields)) { System.out.println(i); return; } } System.out.println("Impossible"); } }

좋은 웹페이지 즐겨찾기