Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19730   Accepted: 7118

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 思路:
因为求解的是最小按键次数,如果将所有都遍历的话,时间复杂度为 2^(N*M)太大了,因为开关按下,上下左右和它自己都会变化,所以,让第一行出现2^m次不同的情况,用二进制表示

也就是for(int i=0;i< 1<<m; i++)种不同情况, 然后将i转化为二进制。这样就可以代表具体开关的地方,第二行以及以后就可以根据第一行进行遍历了。因为第一行已经确定了所以,第二行就专门去寻找第一行还没有关的灯,比如:map[i][j] = 1,就按下a[i+1][j]处的开关保证i+1行开关按后,第i行的灯全部关闭为0 最后判断这种情况是否正确,然后判断是否最小,记录下来输出。

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int map[][],mm[][];
int a[][],A[][];
int d[][] = {,,,,-,,,,,-}; int get_s(int x, int y){
if(map[x][y]==) return ;
else return ;
}
int main(){
int n,m;
cin>>n>>m;
memset(map,,sizeof(map));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>map[i][j];
}
}
memcpy(mm,map,sizeof(map)); int minn = 1e9;
for( int i = ; i < <<m; i++ ) {
int count=,flag = ;
memset(a,,sizeof(a));
memcpy(map,mm,sizeof(mm));
//第一行解决 将 i 转化为 二进制
for(int j=;j<=m;j++){
a[][j] = (i>>(j-))&;
if( a[][j] == ){
count++;
map[][j] = get_s(,j);
map[][j-] = get_s(,j-);
map[][j+] = get_s(,j+);
map[][j] = get_s(,j);
}
} for( int j = ; j < n; j++ ) {
for( int k = ; k <= m; k++ ) {
if( map[j][k] == ) {
a[j+][k] = ;
int dx = j+, dy = k, tx, ty;
count++;
for( int z = ; z < ; z++ ) {
tx = dx + d[z][];
ty = dy + d[z][];
// cout<<tx<<" "<<ty<<endl;
map[tx][ty] = get_s(tx,ty);
}
// cout<<map[j][k]<<" "<<" j = "<<j<<" k = "<<k<<endl;
} }
}
for( int j = ; j <= m; j++ ) {
if(map[n][j]==) flag = ;
} if( flag ) continue;
else {
if( count < minn ) {
minn = count;
memcpy(A,a,sizeof(a));
}
} }
if(minn == 1e9) printf("IMPOSSIBLE\n");
else{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
printf("%d%c",A[i][j],j==m?'\n':' ');
}
return ;
}

最新文章

  1. 性能优化方法(Z)
  2. MVC中处理表单提交的方式(使用html扩展方法+juqery插件)
  3. mysql分组查询取分组后各分组中的最新一条记录
  4. Linux(Centos)下jdbc连接oracle速度超慢的问题
  5. iOS 开发--Objective-C 反射机制
  6. Excel操作--使用NPOI导入导出Excel为DataTable
  7. 一句话绑定父函数的作用域this
  8. golang的并发不等于并行
  9. git删除本地分支
  10. OO第一次作业总结
  11. 引擎设计跟踪(九.14.2j) TableView工具填坑以及多国语言
  12. Zabbix Server 监控Web页面
  13. Sql server—— for xml path简单用法(可以按照分组把相同组的列中的不同的值,像字符串一样拼接在一起显示在分组之后的列中。)
  14. 在html中做表格以及给表格设置高宽字体居中和表格线的粗细
  15. 三种通用应用层协议protobuf、thrift、avro对比,完爆xml,json,http
  16. shell脚本中比较两个小数的办法
  17. 20135202闫佳歆--week7 可执行程序的装载--学习笔记
  18. asp.net MVC中防止跨站请求攻击(CSRF)的ajax用法
  19. python开发环境搭建(python2.7.5+pyCharm2.7.3)【原创】
  20. STM32.定时器

热门文章

  1. MySQL半同步主从.md
  2. Jinja2 简明使用手册
  3. ## 20145203盖泽双 《Java程序设计》第二周学习总结
  4. vagrant之道(即其工作流程)
  5. Selenium启动最新的火狐浏览器异常排查
  6. selenium测试报告(含通过率统计图和失败截图)
  7. Python2图像文本识别
  8. UITextView 和 UITextField限制字符数和表情符号
  9. 利用java代码生成keyStore
  10. CSS+JQuery实现Tabs效果,点击更改背景色(不含图片)