[kuangbin带你飞]专题一 简单搜索 D - Fliptile

本文探讨了一道经典的算法题POJ-3279,旨在通过最少的操作使矩阵所有元素变为白色。文章详细解析了题目的背景、输入输出要求,并给出了具体的实现思路与代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ - 3279

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".

Input

Line 1: Two space-separated integers: M and N 
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white

Output

Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

Sample Input

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

这道题不大会,查了题解后写的ԅ(¯﹃¯ԅ)

大佬的解题思路:

于一个点翻转两次则返回原来的状态,所以最优解每个点最多翻转一次,但是2^(M*N)过大,所以2^N枚举第一行的所有翻转方式(逆字典序枚举),确定一种方式之后第二行也就随之确定了,因为如果第一行处理后没有翻回白色的点:(i,j),必须在第二行(i+1,j)翻回,否则将无法返回。反之第二行其他的点都处理为不翻转,要不然上一行的点会翻回黑色而无法改变。第二行ok后同理解决第三行,以此类推。处理到最后一行如果不是全白就输出IMPOSSIBLE。否则更新结果。

#include<stdio.h>
#include<string.h>
int t[30][30], tem[30][30], m[30][30];
int M,N,dir[5][2] = { 0,0,1,0,0,1,-1,0,0,-1 };
int get(int x, int y)//获得x,y点的颜色
{
	int c = t[x][y];
	for (int i = 0; i < 5; i++)
	{
		int x1 = x + dir[i][0], y1 = y + dir[i][1];
		c += tem[x1][y1];
	}
	return c % 2;
}
int cal()//计算2行及之后的,有解返回翻点数,无解返回-1
{
	for (int i = 2; i <= M; i++)
		for (int j = 1; j <= N; j++)
			if (get(i - 1, j) == 1)
				tem[i][j] = 1;
	for (int i = 1; i <= N; i++)
		if (get(M, i))return -1;
	int res = 0;
	for (int i = 1; i <= M; i++)
		for (int j = 1; j <= N; j++)
			res += tem[i][j];
	return res;
}
int main()
{
	int min = -1;//次数>0可以这样初始化
	scanf("%d%d", &M, &N);
	for (int i = 1; i <= M; i++)
		for (int j = 1; j <= N; j++)
			scanf("%d", &t[i][j]);
	for (int i = 0; i < (1 << N); i++)//枚举第一行
	{
		memset(tem, 0, sizeof(tem));
		for (int j = 1; j <= N; j++)
			tem[1][j] = (i >> (j - 1)) & 1;
		int num = cal();
		if (num >= 0 && (min<0 || min>num))
		{
			min = num;
			memcpy(m, tem, sizeof(tem));
		}
	}
	if (min == -1)printf("IMPOSSIBLE\n");
	else
	{
		for (int i = 1; i <= M; i++)
			for (int j = 1; j <= N; j++)
				printf("%d%c", m[i][j], j == N ? '\n' : ' ');
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值