POJ3279 Fliptile【状态压缩+DFS】

Fliptile

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17511 Accepted: 6409

Description

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

Source

USACO 2007 Open Silver

问题链接:POJ3279 Fliptile

问题描述:给定M * N 个格子,每个格子可以翻转正反面,一面是黑色,另一面是白色。黑色翻转之后变成白色,白色翻转之后则变成黑色。现在要将所有的格子翻转为白色。不过每次翻转一个格子,与它上下左右相邻接的格子也会被翻转。求用最少的步骤完成任务时,每个格子的翻转次数。当有多个解时,输出字典序最小的一组;解不存在的话,则输出IMPOSSIBLE。

解题思路:一个格子反转偶数次和反转0次一样,反转奇数次和反转1次一样,因此最后的答案是一个01数组,即要么反转一次,要么不反转。如果穷举所有的结果会超时,现在考虑当第一行的状态固定时,格子(i,j)的状态只受格子(i+1,j)控制了,当格子(i,j)不为白色时,格子(i+1,j)就必须反转,当第二行的状态确定后,继续确定第三行,直到倒数第二行决定后,至此1-(m-1)行全为白色了,现在查看最后一行,如果最后一行都为白色,则是一种可行解。第一行共有2^n中状态,遍历各种状态求出最优解即可。

AC的C++程序:

#include<iostream>
#include<cstring>

using namespace std;
const int N=20;
int m,n;//行列

int g[N][N];//保存初始情况
int ans[N][N];//保存最优解
int flip[N][N];//保存中间解

//查询(x,y)位置的颜色 奇数为1,偶数为0 ,颜色有初始颜色g[x][y]和上下左右和自己是否反转共同决定 
//程序下标从1开始存储,因此可以不用进行下标越界查询 
int get(int x,int y)
{
	return (g[x][y]+flip[x][y]+flip[x-1][y]+flip[x+1][y]+flip[x][y-1]+flip[x][y+1])%2;
} 

//求出在第一行确定的情况下最少的操作次数,如果无解返回-1
int calc()
{
	//求出从第2行开始的反转方法
	int count=0;
	for(int i=2;i<=m;i++)
	  for(int j=1;j<=n;j++)
	    if(get(i-1,j))//如果(i-1,j)是黑色的话,则这个格子必须反转 
		  flip[i][j]=1;
	//判断最后一行是否为全为0
	for(int j=1;j<=n;j++) 
	  if(get(m,j))
	    return -1;
	//如果是可行解,返回反转的次数 
	for(int i=1;i<=m;i++)
	  for(int j=1;j<=n;j++)
	    count+=flip[i][j];
	return count; 
}

void solve()
{
	int res=-1;
	/*按照字典序尝试第一行的所有可能情况,使用状态压缩
	  第一行共n个格子,每个格子都有反转和不反转两种选择,因此共2^n种选择
	  状态压缩:1表示反转,0表示不反转
	  举例说明:当n=2时有两个格子,共4中选泽
	  00 01 10 11(低位表示序号小的格子)分别表示都不反转,反转第一个,反转第二个,两个都反转
	  四种状态的十进制数分别为0 1 2 3 。1<<(j-1)表示此状态下第j个格子是否被反转,即第j为是否为1	
	  那么((1<<(j-1))&i)==1表示第j个格子被反转了因此flip[1][j]=1 */ 
	for(int i=0;i<(1<<n);i++){
		memset(flip,0,sizeof(flip));
		for(int j=1;j<=n;j++) 
		  if((1<<(j-1))&i)
		    flip[1][j]=1;
		int count=calc();//统计这种情况下需要反转的次数
		if(count>=0&&(res<0||res>count)){//更新最优结果 
			res=count;
			memcpy(ans,flip,sizeof(flip));
		} 
	}
	if(res<0)//无解 
	  printf("IMPOSSIBLE\n"); 
	else{//输出结果 
		for(int i=1;i<=m;i++)
		  for(int j=1;j<=n;j++)
		    printf("%d%c",ans[i][j],j==n?'\n':' ');
	}
} 

int main()
{
	scanf("%d%d",&m,&n);
	for(int i=1;i<=m;i++)
	  for(int j=1;j<=n;j++)
	    scanf("%d",&g[i][j]);
	solve();
	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 1321 排兵布阵问题可以使用 DFS 算法求解。 题目要求在一个 n x n 的棋盘上,放置 k 个棋子,其中每行、每列都最多只能有一个棋子。我们可以使用 DFS 枚举每个棋子的位置,对于每个棋子,尝试将其放置在每一行中未被占用的位置上,直到放置了 k 个棋子。在 DFS 的过程中,需要记录每行和每列是否已经有棋子,以便在尝试放置下一个棋子时进行判断。 以下是基本的 DFS 模板代码: ```python def dfs(row, cnt): global ans if cnt == k: ans += 1 return for i in range(row, n): for j in range(n): if row_used[i] or col_used[j] or board[i][j] == '.': continue row_used[i] = col_used[j] = True dfs(i + 1, cnt + 1) row_used[i] = col_used[j] = False n, k = map(int, input().split()) board = [input() for _ in range(n)] row_used = [False] * n col_used = [False] * n ans = 0 dfs(0, 0) print(ans) ``` 其中,row 代表当前尝试放置棋子的行数,cnt 代表已经放置的棋子数量。row_used 和 col_used 分别表示每行和每列是否已经有棋子,board 则表示棋盘的状态。在尝试放置棋子时,需要排除掉无法放置的位置,即已经有棋子的行和列,以及棋盘上标记为 '.' 的位置。当放置了 k 个棋子时,即可计数一次方案数。注意,在回溯时需要将之前标记为已使用的行和列重新标记为未使用。 需要注意的是,在 Python 中,递归深度的默认限制为 1000,可能无法通过本题。可以通过以下代码来解除限制: ```python import sys sys.setrecursionlimit(100000) ``` 完整代码如下:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值