博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 3279 FlipTile
阅读量:4217 次
发布时间:2019-05-26

本文共 3085 字,大约阅读时间需要 10 分钟。

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16502   Accepted: 6030

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 41 0 0 10 1 1 00 1 1 01 0 0 1

Sample Output

0 0 0 01 0 0 11 0 0 10 0 0 0

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int MAX_N = 17;/* 题目大意:有一个 M * N 的格子,每个格子可以翻转正反面,它们有一面是黑色,另一面是白色。黑色翻转之后变成白色,白色翻转之后则变成黑色。 游戏要做的是把所有的格子翻转为白色。不过因为牛蹄很大,所以每次翻转一个格子,与它上下左右相邻接的格子也会被翻转。 求用最小的步数完成时,每个格子的翻转次数。最小步数的解有多个时,输出字典序最小的一组;解不存在的话,则输出IMPOSSIBLE 题目样例:0表示白色,1表示黑色 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 题目思路:首先,同一个格子翻转两次就会恢复原状,所以多次翻转是多余的。此外,翻转的格子的集合相同的话,其次序是无关紧要的。 不妨先指定好最上面一行的翻转方法。此时,能翻转(1,1)的只剩下了(2,1),所以可以直接判断(2,1)是否需要翻转。类似的(2,1)~(2,N)都能这样判断, 如此反复下去就能确定所有格子的翻转方法,最后(M,1)~(M,N)并非全为白色,则意味着不存在可行的操作方法。 像这样,先确定第一行的翻转方式,然后可以很容易判断这样是否存在解以及解的最小步数是多少,这样将第一行的所有翻转方式都尝试一次就能求出整个问题的最小步数。这个算法中最上面 一行的翻转方式共有2^N种,复杂度为O(M * N * 2^N) */int opt[MAX_N][MAX_N];int flip[MAX_N][MAX_N]; // 记录瓷砖翻次数int a[MAX_N][MAX_N];int dir[5][2] = { {-1,0},{1,0},{0,1},{0,-1},{0,0}};int m,n;// 统计 位置(x,y) 周围(包括自身)翻转次数int get(int x, int y){ // 记录自身颜色 1 的话相当于 翻转了一次 int res = a[x][y]; for(int i = 0; i < 5; ++i){ int nx = x + dir[i][0]; int ny = y + dir[i][1]; if(nx>=0 && nx
=0 && ny
> j & 1; // 从右到左(先取低位) 依次把 首行翻转情况 赋值给flip } int num = calc(); if(num >= 0 && (res < 0 || res > num)){ res = num; // 最优解 memcpy(opt,flip,sizeof(flip)); } } if(res == -1){ cout << "IMPOSSIBLE" <
> m >> n){ memset(flip,0,sizeof(flip)); memset(a,0,sizeof(a)); for(int i = 0; i < m; ++i){ for(int j = 0; j < n; ++j) cin >> a[i][j]; } solve(); } return 0; }

 

转载地址:http://kwsmi.baihongyu.com/

你可能感兴趣的文章
Web前端学习笔记——JavaScript 之继承、函数进阶
查看>>
Web前端学习笔记——JavaScript之面向对象游戏案例:贪吃蛇
查看>>
Web前端学习笔记——JavaScript之面向对象编程
查看>>
上海控安成功举办普陀区科普创新专项智能网联车学术活动
查看>>
控安轩辕实验室:利用开源项目实现定位和时间欺骗(二)
查看>>
基于预测的自动驾驶全球导航卫星系统欺骗攻击检测
查看>>
上海工业互联网协会安全专委会成立,加快提升工业互联网安全保障能力
查看>>
普陀区委副书记顾军、区政府副区长魏静带队调研上海控安
查看>>
上海市水务工控系统安全联合研究实验室正式启用
查看>>
基于数字孪生的水务系统安全试验床正式上线
查看>>
上海控安成功入选“2020年度上海市工业互联网平台和专业服务商推荐目录”
查看>>
多传感器融合定位是否足够安全?(一)
查看>>
多传感器融合定位是否足够安全?(二)
查看>>
上海申通地铁集团院士专家工作站与上海控安达成战略合作
查看>>
多传感器融合定位是否足够安全?(三)
查看>>
光靠欺骗检测是不够的:对抗多目标跟踪的攻击
查看>>
上海控安自主研发汽车信息安全风险评估工具平台,建标准化检测能力
查看>>
汽车智能化啥时候能实现? 先问问“汽车传感器”!
查看>>
利用车对车通信定位欺骗攻击车载GPS
查看>>
不做单元测试?小心得不偿失!嵌入式系统单元测试工具,自动生成测试用例
查看>>