Fliptile

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
就是一个01矩阵,翻一个位置,那么周围四个相邻格子以及自身都会变为相反的数(即0变1,1变0),问最少步数的方案。第一下拿到这个题目是一点思路都没有,之后想的最暴力方法是每个枚举一遍,我疯了……但是发现,其实枚举某一行或者一列,那么其他的就可以定了。比如把最后一行情况列出来,如果这行还有1,那就只能通过上一行来改变它的值,再看题目说要求字典序最小的,那就是枚举第一行了,而且是先搜不翻的情况,再搜翻的情况,这样字典序才会最小。
PS:讨论区有人说数据有问题。
官方数据:
7 8
0 0 0 0 1 0 1 0
0 0 0 0 0 0 1 0
1 0 0 0 1 0 0 1
0 1 0 1 1 0 0 1
0 0 0 1 1 0 1 1
0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 0
它的答案是:
1 1 1 0 1 1 0 0
0 1 0 0 1 0 0 0
0 0 0 1 0 0 1 0
1 1 1 1 1 1 1 0
0 0 1 1 0 1 1 0
1 0 1 0 0 1 0 0
1 0 0 1 1 0 0 0

但其是如果我们理解成从左到右的话解为:
0 0 1 1 0 1 1 1
0 1 0 0 1 0 0 0
1 1 0 0 1 0 0 1
1 1 1 1 1 1 1 0
1 1 1 0 1 1 0 1
1 0 1 0 0 1 0 0
0 1 0 0 0 0 1 1
但是官方数据只用了26步,他们的解答要30步,题目要的是最少的步数,最小步数中最小字典序才是题目需要的,所以这个误导人可能。
还有,一直默认n为行,m为列,今天坑死我了……

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int m, n, pre;
int a[17][17], b[17][17], time, moban[17][17], moban2[17][17];
int max(int a, int b)
{
    return a > b ? a : b;
}
inline void ex_check(int x)
{
    int i, j, leap;
    leap = 0;
    for (i = 1; i <= m; i++)
    for (j = 1; j <= n; j++)
    {
        if (b[i][j] == 0)continue;
        else
        {
            leap = 1; break;
        }
    }
    if (!leap)
    {
        if (x < time)
        {
            time = x;
            for (i = 1; i <= m;i++)
            for (j = 1; j <= n; j++)
                moban2[i][j] = moban[i][j];
        }
    }
}
void check(int x)
{
    int i, j, sum, c[17][17];
    for (i = 1; i <= m;i++)
    for (j = 1; j <= n; j++)
        c[i][j] = moban[i][j];
    sum = x;
    if (x == 5){
        sum = x;
    }
    for (i = 2; i <= m; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (b[i - 1][j] == 1)
            {
                moban[i][j] = 1;
                b[i][j] = 1 - b[i][j];
                b[i - 1][j] = 0;
                if (j >= 2)
                    b[i][j - 1] = 1 - b[i][j - 1];
                if (j + 1 <= n)
                    b[i][j + 1] = 1 - b[i][j + 1];
                if (i + 1 <= m)
                    b[i + 1][j] = 1 - b[i + 1][j];
                sum++;
            }
        }
    }
    ex_check(sum);
    for (i = 1; i <= m; i++)
    for (j = 1; j <= n; j++)
        moban[i][j] = c[i][j];
}
void dfs(int x)
{
    int i, j, c[17][17], d[17][17];
    if (x == n + 1)
    {
        check(pre);
    }
    else if(x<=n)
    {
        for (i = 1; i <= m; i++)
        for (j = 1; j <= n; j++)
        {
            c[i][j] = b[i][j];
            d[i][j] = moban[i][j];
        }
        dfs(x + 1);
        for (i = 1; i <= m; i++)
        for (j = 1; j <= n; j++)
            b[i][j] = c[i][j];
        b[1][x] = 1 - b[1][x];
        moban[1][x] = 1;
        if (x - 1 >= 1)
            b[1][x - 1] = 1 - b[1][x - 1];
        if (x + 1 <= n)
            b[1][x + 1] = 1 - b[1][x + 1];
        if (m >= 2)
            b[2][x] = 1 - b[2][x];
        pre++;
        dfs(x + 1);
        pre--;
        for (i = 1; i <= m;i++)
        for (j = 1; j <= n; j++)
        {
            b[i][j] = c[i][j];
            moban[i][j] = d[i][j];
        }
    }
}
int main()
{
    int i, j;
    cin >> m >> n;
    for (i = 1; i <= m; i++)
    {
        for (j = 1; j <= n; j++)
        {
            scanf("%d", &a[i][j]);
            b[i][j] = a[i][j];
        }
    }
    pre = 0; time = 500;
    for (i = 1; i <= m;i++)
    for (j = 1; j <= n; j++)
        moban[i][j] = 0;
    dfs(1);
    if (time == 500)
    {
        cout << "IMPOSSIBLE" << endl;
    }
    else
    {
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= n; j++)
                cout << moban2[i][j] << " ";
            cout << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值