D - Fliptile POJ - 3279 JAVA

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;
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值