POJ 3279 —— 开关问题

Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2860 Accepted: 1095

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矩阵,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-8
const 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值