Turn_Over_Game
public class Turn_Over_Game {
static int length = 4;
static char table_game[][];
static char table_game_test[][];
static int day_nhiphan[];
static int length_nhiphan = 16;
static int solandoi;
static int min;
static int move[][]={{0,0},{1,0},{-1,0},{0,1},{0,-1}};
// public static void printresult(){
// for (int i=0;i<length_nhiphan;i++){
// System.out.print(day_nhiphan[i]+" ");
// }
// System.out.println();
// }
public static void nhiphan(int i) {
int k;
for (k = 0; k <= 1; k++) {
day_nhiphan[i] = k;
if (i == length_nhiphan-1) {
//
xuly();
if (check() == 1) {
if (solandoi < min) {
min = solandoi;
if (min == 5)
{
for (int l=0;l<length_nhiphan;l++){
System.out.print(day_nhiphan[l]+" ");
}
for (int z = 0; z < length; z++) {
for (int t = 0; t < length; t++) {
System.out.print(table_game_test[z][t]+"");
}
System.out.println();
}
}
}
}
} else
nhiphan(i + 1);
}
}
public static int check() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (table_game_test[0][0] != table_game_test[i][j]) {
return 0;
}
}
}
return 1;
}
public static void xuly() {
int dem = 0;
solandoi = 0;
//table_game_test = table_game;
for (int z = 0; z < length; z++) {
for (int t = 0; t < length; t++) {
table_game_test[z][t] = table_game[z][t];
}
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (day_nhiphan[dem] == 1) {
solandoi++;
for (int l=0;l<5;l++){
if(0<=i+move[l][0]&&i+move[l][0]<4&&0<=j+move[l][1]&&j+move[l][1]<4){
int x= i+move[l][0];
int y= j+move[l][1];
if(table_game_test[x][y]=='w'){
table_game_test[x][y]='b';
}else{
table_game_test[x][y]='w';
}
}
}
}
dem++;
}
}
//System.out.println(dem + " ");
}
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("input5.txt"));
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
int botest = Integer.parseInt(test);
for (int testcase = 1; testcase <= botest; testcase++) {
min = 1000;
String s;
day_nhiphan = new int[16];
table_game = new char[16][16];
table_game_test = new char[16][16];
for (int i = 0; i < length; i++) {
s = sc.nextLine();
for (int j = 0; j < length; j++) {
table_game[i][j] = s.charAt(j);
}
}
// if(testcase==9)
// {
// for (int i = 0; i < length; i++) {
// for (int j = 0; j < length; j++) {
// System.out.print(table_game[i][j]+" ");
// }
// System.out.println();
// }
// }
nhiphan(0);
System.out.println("Case #" + testcase);
if (min != 1000) {
System.out.println(min);
} else {
System.out.println("impossible");
}
}
}
}
static int length = 4;
static char table_game[][];
static char table_game_test[][];
static int day_nhiphan[];
static int length_nhiphan = 16;
static int solandoi;
static int min;
static int move[][]={{0,0},{1,0},{-1,0},{0,1},{0,-1}};
// public static void printresult(){
// for (int i=0;i<length_nhiphan;i++){
// System.out.print(day_nhiphan[i]+" ");
// }
// System.out.println();
// }
public static void nhiphan(int i) {
int k;
for (k = 0; k <= 1; k++) {
day_nhiphan[i] = k;
if (i == length_nhiphan-1) {
//
xuly();
if (check() == 1) {
if (solandoi < min) {
min = solandoi;
if (min == 5)
{
for (int l=0;l<length_nhiphan;l++){
System.out.print(day_nhiphan[l]+" ");
}
for (int z = 0; z < length; z++) {
for (int t = 0; t < length; t++) {
System.out.print(table_game_test[z][t]+"");
}
System.out.println();
}
}
}
}
} else
nhiphan(i + 1);
}
}
public static int check() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (table_game_test[0][0] != table_game_test[i][j]) {
return 0;
}
}
}
return 1;
}
public static void xuly() {
int dem = 0;
solandoi = 0;
//table_game_test = table_game;
for (int z = 0; z < length; z++) {
for (int t = 0; t < length; t++) {
table_game_test[z][t] = table_game[z][t];
}
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (day_nhiphan[dem] == 1) {
solandoi++;
for (int l=0;l<5;l++){
if(0<=i+move[l][0]&&i+move[l][0]<4&&0<=j+move[l][1]&&j+move[l][1]<4){
int x= i+move[l][0];
int y= j+move[l][1];
if(table_game_test[x][y]=='w'){
table_game_test[x][y]='b';
}else{
table_game_test[x][y]='w';
}
}
}
}
dem++;
}
}
//System.out.println(dem + " ");
}
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("input5.txt"));
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
int botest = Integer.parseInt(test);
for (int testcase = 1; testcase <= botest; testcase++) {
min = 1000;
String s;
day_nhiphan = new int[16];
table_game = new char[16][16];
table_game_test = new char[16][16];
for (int i = 0; i < length; i++) {
s = sc.nextLine();
for (int j = 0; j < length; j++) {
table_game[i][j] = s.charAt(j);
}
}
// if(testcase==9)
// {
// for (int i = 0; i < length; i++) {
// for (int j = 0; j < length; j++) {
// System.out.print(table_game[i][j]+" ");
// }
// System.out.println();
// }
// }
nhiphan(0);
System.out.println("Case #" + testcase);
if (min != 1000) {
System.out.println(min);
} else {
System.out.println("impossible");
}
}
}
}
Nhận xét
Đăng nhận xét