POJ 3279 —— 开关问题
来源:互联网 发布:国内查找数据的网站 编辑:程序博客网 时间:2024/05/18 00:45
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
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
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
Source
题意是给你一个m*n的01矩阵,0代表白色,1代表黑色,你可以翻转某一块格子使之白变黑,黑变白,与此同时,他的上下左右的格子也会翻转,现在你要用最小的步数把全部格子翻成白色,输出记录每个位置翻转了几次的矩阵。
思路:首先一个格子翻转奇数次等于翻转一次,翻转偶数次相当于没翻转,我们只要统计该位置翻转了几次就能得到最终状态;因此,每个格子的最终状态为(最初颜色+上下左右中翻转的次数 )% 2,这也就是getcolor函数的作用。
由于如果我们知道了第一排的翻转方法,我们可以推出整个图的翻转方法,因此,我们枚举第一行的所有情况,然后通过一个calc函数就能得到答案。
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>#include <cassert>using namespace std;///#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid , rt << 1#define rson mid + 1 , r , rt << 1 | 1#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 15 + 5;const int MAXS = 10000 + 50;const int sigma_size = 26;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;const int inf = 1 << 30;#define eps 1e-8const long long MOD = 1000000000 + 7;const int mod = 100000;typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pii;#define Bug(s) cout << "s = " << s << endl;///#pragma comment(linker, "/STACK:102400000,102400000")int m , n ;int gra[MAXN][MAXN];int ans[MAXN][MAXN];int filp[MAXN][MAXN];const int dx[5] = {-1 ,0 , 0 , 0 , 1};const int dy[5] = {0 , -1 , 0 , 1 , 0};int getcolor(int x , int y){ int c = gra[x][y]; FOR(d , 0 , 5) { int xx = x + dx[d] ,yy = y + dy[d]; if(0 <= xx && xx < m && 0 <= yy && yy < n)c += filp[xx][yy]; } return c % 2;}int calc(){ FOR(i , 1 , m)FOR(j ,0 , n)if(getcolor(i - 1, j))filp[i][j] = 1; FOR(j , 0 , n)if(getcolor(m - 1 , j))return -1; int res = 0; FOR(i ,0, m)FOR(j , 0, n)res += filp[i][j]; return res;}void solve(){ int res = -1; FOR(i ,0 , 1 << n) { clr(filp , 0); FOR(j , 0 , n)filp[0][n -j - 1] = i >> j & 1;///判断i的第j位是否存在,存在为1,否则为0; int num = calc(); if(num >= 0 && (res < 0 || res > num)) { res = num; memcpy(ans , filp , sizeof(filp)); } } if(res < 0)printf("IMPOSSIBLE\n"); else { FOR(i , 0 , m)FOR(j , 0, n) { printf("%d%c" , ans[i][j] , j == n - 1 ? '\n' : ' '); } }}int main(){ scanf("%d%d" , &m ,&n); FOR(i , 0 , m)FOR(j , 0 ,n) { scanf("%d" , &gra[i][j]); } solve(); return 0;}
- POJ 3279 —— 开关问题
- POJ 3279 Fliptile——开关问题
- POJ 3276 —— 开关问题
- POJ 3279 Fliptile 开关问题
- Fliptile poj 3279 开关问题
- POJ 3279 Fliptile 开关问题
- poj 3279 Fliptile 开关问题
- POJ 3279 Fliptile 开关问题
- poj 3279 Fliptile(开关问题)
- POJ 3279 Fliptile 【开关问题】
- 开关问题2(poj 3279)
- POJ 3279 Fliptile(开关问题)
- POJ 3276 Face The Right Way——开关问题
- poj 3276Face The Right Way—反转(开关问题)
- poj 3279 Fliptile—二进制暴力的运用(开关问题)
- Poj 3279 & 1222(开关问题)
- POJ - 3279 Fliptile(开关问题)
- Fliptile POJ 3279(开关问题)
- 21-activity跳转第三步获取SecondActivity的返回参数值
- StringTokenizer类的使用
- 数据结构排序算法汇总
- Android的GridView和Gallery结合Demo
- ID3&C4.5的区别
- POJ 3279 —— 开关问题
- 算法学习之邻域标记边缘提取
- java l垃圾收集相关
- Leetcode: Wildcard Matching
- cin.get()和cin.getline()区别
- 资料整理
- cocos2d学习笔记(六)CCParallaxNode和Tile Map
- 老码农:关于需求分析的几点体会
- JavaScript的继承的封装