import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main
{
public static int dir[][] =
{
{ 0, 1 },
{ 0, -1 },
{ 1, 0 },
{ -1, 0 } };
public static int map[][], map1[][], arr[][];
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
int a, b;
a = sc.nextInt();
b = sc.nextInt();
arr = new int[a][b];
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
arr[i][j] = sc.nextInt();
int change[][] = new int[1 << b][b];
map = new int[a][b];
int chance = Integer.MAX_VALUE;
for (int i = 0; i < 1 << b; i++)
{
for (int j = 0; j < b; j++)
{
change[i][b - 1 - j] = i >> j & 1;
}
}
for (int i = 0; i < change.length; i++)
{
map1 = new int[a][b];
map1[0] = change[i];
int find = Integer.bitCount(i);
for (int j = 1; j < a; j++)
{
for (int k = 0; k < b; k++)
{
int sb = dfs(j - 1, k, a, b);
if (sb == 1)
{
map1[j][k] = 1;
find++;
}
}
}
/----------------------------------------------------------------
boolean sb = false;
for (int j = 0; j < b; j++)
{
int cnm = dfs(a - 1, j, a, b);
if (cnm == 1)
{
sb = true;
}
}
if (sb == true)
{
continue;
}
if (find < chance)
{
map = map1;
chance = find;
}
}
if (chance == Integer.MAX_VALUE)
{
System.out.println("IMPOSSIBLE");
} else
{
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[0].length; j++)
{
if (j==0)
{
System.out.print(map[i][j]);
}else {
System.out.print(" "+map[i][j]);
}
}
System.out.println();
}
}
}
}
public static int dfs(int x, int y, int a, int b)
{
int ok = arr[x][y]+map1[x][y];
for (int i = 0; i < 4; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx >= 0 && xx < a && yy >= 0 && yy < b)
{
ok += map1[xx][yy];
}
}
return ok % 2;
}
}
D - Fliptile POJ - 3279 JAVA
最新推荐文章于 2021-08-30 16:23:40 发布