Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13631   Accepted: 5027

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

 

大意:一个m*n的01矩阵,每次点击(x,y),那么她的上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步。

想法:枚举第一行的所有可能,对于每种可能找出最优解。

需要干的活就是枚举第一行的所有情况然后对于每次枚举都计算验证是否符合要求以及反转所需的次数。其中,第一行的状态数量可以使用左移运算优化(效率比pow高),于是总共枚举的数量就有1<<col次。另外,因为这使得一行的状态是由一个数字保存的,所以依然使用位运算取得是否翻转的状态。

将枚举好的一次首行状态存好后就可以交给calc计算和验证了。验证就是通过上述翻转规则操作。其中get(x,y)为取得某个位置是否为1的状态的方法(过程)。

可以玩这个游戏找找规律http://s1.4399.com:8080/4399swf/upload_swf/ftp/20080527/1.swf

详见代码注释

 [][];
int a[][];
int q[][];
int n, m; bool get(int x, int y)
{
int i;
int s = a[x][y];//初始化为本身
for (i = ; i < ; i++)
{
int xx = x + d[i][];
int yy = y + d[i][];
if (xx <= || y <= || x > n || y > m) continue;
s +=p[xx][yy];
}
return (s+)%;
/*或者if (s & 1)return 0;
return 1;*/
} int cal()
{
int i, j;
int res =;
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
if (!get(i - , j))//通过翻转次数+原本的a[x][y]来判断要不要转
{
p[i][j] = ;//用下一排来控制上一排,让其恢复
}
}
}
for (j = ; j <= m; j++)
{
if (!get(n, j))
return -;
}
for (i = ; i <= n; i++)
for (j = ; j <= m; j++)
res += p[i][j];//计算转的总次数
return res;
} int main()
{
cin >> n >> m;
int ans = inf;
int i, j;
for (i = ; i <= n; i++)
for (j = ; j <= m; j++)
cin >> a[i][j];
for (i =; i <(<< m); i++)//二进制枚举,到2的m次方
{
memset(p,,sizeof(p));
for (j =; j <=m; j++)
p[][j] = i >>(j-)& ;//是右移,二进制枚举子集
int mm = cal();
if (mm != - && ans > mm)//更新
{
ans = mm;
memcpy(q, p, sizeof(p));//拷贝函数
}
}
if (ans == inf)
cout << "IMPOSSIBLE" << endl;
else
{
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
cout << q[i][j];
if (j == m) cout << "\n";
else cout <<" ";
}
}
}
return ;
}

POJ 3279 Fliptile(反转 +二进制枚举)的更多相关文章

  1. (简单) POJ 3279 Fliptile,集合枚举。

    Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...

  2. POJ 3279 Fliptile (二进制+搜索)

    [题目链接]click here~~ [题目大意]: 农夫约翰知道聪明的牛产奶多. 于是为了提高牛的智商他准备了例如以下游戏. 有一个M×N 的格子,每一个格子能够翻转正反面,它们一面是黑色,还有一面 ...

  3. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  4. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  5. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  6. POJ 3279 Fliptile (二进制枚举)

    <题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...

  7. poj 3279 Fliptile(二进制)

    http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...

  8. POJ - 3279 Fliptile(反转---开关问题)

    题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况.解不存在,输出IMPOSSIBLE. 分析: 1.枚举第一行 ...

  9. poj 3279 Fliptile(二进制搜索)

    Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He ha ...

随机推荐

  1. Jquery validation自定义验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 浅谈OSSemPost()和OSSemPend()

    http://blog.csdn.net/goodman_lqifei/article/details/53616174

  3. React之事件处理

    在react中,事件处理的写法和处理方式可能会和vue以及传统html有些不同. 一.事件名和默认行为阻止 事件名采用驼峰写法,并且方法名用大括号引入,而不是双引号: <button onCli ...

  4. 11.Linux启动过程详解

    目录: 本文转载自:http://blog.csdn.net/miss_acha/article/details/50004717 经过对Linux系统有了一定了解和熟悉后,想对其更深层次的东西做进一 ...

  5. libcurl 错误码总结

    下载出现这种错误(Requested range was not delivered by the server  ),说明是重复下载,删掉本地的再下载就不会出现了

  6. C#中PadLeft和PadRight小知识点

    当我们显示字符串数据时,有时候我们需要考虑数据的排列美观. 比如一些人名和一些编号,我们想让他们整齐对齐显示等. C# String类提供了2种操作方法: String.PadLeft(int tot ...

  7. springmvc+mybatis+redis实现查询插入操作

    最近在学习redis,虽然现在还不是很熟练.不过可以进行简单的框架整合开发. IDE:我使用的是IDEA.springmvc+spring+mybatis的整合这个我就不多说了,下面我们先进行这块的整 ...

  8. BZOJ2936 Codevs3634 POI1999 积水 【并查集】*

    BZOJ2936 Codevs3634 POI1999 积水 题目描述 有这样一块土地,它可以被划分成N×M个正方形小块,每块面积是一平方英寸,第i行第j列的小块可以表示成P(i,j).这块土地高低不 ...

  9. 使用python处理selenium中的xpath定位元素的模糊匹配问题

    # 用contains,寻找页面中style属性值包含有sp.gif这个关键字的所有div元素,其中@后面可以跟该元素任意的属性名. self.driver.find_element_by_xpath ...

  10. 字符串处理scanf("%d%*c",&n);

    "*"表示该输入项读入后不赋予任何变量,即跳过该输入值.这在减小内存开支上面还是有一点用处的,不需要的字符直接跳过,免得申请没用的变量空间 你的例子中的%*c的作用是读入'\n', ...