D - Fliptile

本文介绍了一种解决网格翻转问题的算法,旨在通过最少次数的操作使所有方格颜色一致。采用深度优先搜索策略,从第一行开始探索所有可能的状态,确保找到达到目标状态所需的最小翻转次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

英文的意思:自行百度在线翻译。

题解:因为直接dfs会超时,所以我们dfs第一行的所有情况,因为如果第一行确定了,那么下边的每一行也能确定,因为题目要求翻得最少,翻转的位置要最少,其实翻转的个数最少就能确定。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int h[20][20],i[20][20],j[20][20],k[20][20],flag,tt,gg;
char xx[400],zz[400];

void dffs(int n,int end,int st)
{
    int a,b,c,d,e,g;
    if(n==end+1)
    {
        for(a=1;a<=st;a++)
        {
            for(b=1;b<=end;b++)
            {
                i[a][b]=h[a][b];
            }
        }
        e=-1;
        for(b=1;b<=end;b++)
        {
            xx[++e]='0'+j[1][b];
        }
        for(a=2;a<=st;a++)
        {
            for(b=1;b<=end;b++)
            {
                if(i[a-1][b]==1)
                {
                    j[a][b]=1;
                    xx[++e]='0'+j[a][b];
                    i[a-1][b]=0;
                    i[a][b]=abs(i[a][b]-1);
                    if(b!=1)
                    {
                        i[a][b-1]=abs(i[a][b-1]-1);
                    }
                    if(b!=end)
                    {
                        i[a][b+1]=abs(i[a][b+1]-1);
                    }
                    if(a!=st)
                    i[a+1][b]=abs(i[a+1][b]-1);
                    j[a][b]=0;
                }
                else xx[++e]='0';
            }
        }
        c=0;
        g=0;
        for(d=1;d<=end;d++)
        {
            if(i[st][d]==1)
                c++;
        }
        if(c==0)
        {
            tt++;
            flag++;
            for(a=0;a<e;a++)
            {
                if(xx[a]=='1')
                g++;

            }
            if(flag==0)
            {
                strcpy(zz,xx);
                gg=g;
            }
            else if(flag!=0)
            {
                if(gg>g)
                {
                    gg=g;
                    strcpy(zz,xx);
                }
                else if(gg==g)
                {
                if(strcmp(zz,xx)>0)
                {
                    strcpy(zz,xx);
                }
                }
            }
        }
        return ;
    }
    h[1][n]=abs(h[1][n]-1);
    j[1][n]=1;
    if(n!=1)
    {
        h[1][n-1]=abs(h[1][n-1]-1);
    }
    if(n!=end)
    {
        h[1][n+1]=abs(h[1][n+1]-1);
    }
    h[2][n]=abs(h[2][n]-1);
    dffs(n+1,end,st);
    h[1][n]=abs(h[1][n]-1);
    j[1][n]=0;
    if(n!=1)
    {
        h[1][n-1]=abs(h[1][n-1]-1);
    }
    if(n!=end)
    {
        h[1][n+1]=abs(h[1][n+1]-1);
    }
    h[2][n]=abs(h[2][n]-1);
    dffs(n+1,end,st);
}

int main()
{
    int a,b,c,d,e;
    scanf("%d %d",&a,&b);
    for(c=1;c<=a;c++)
    {
        for(d=1;d<=b;d++)
        {
            scanf("%d",&h[c][d]);
            i[c][d]=h[c][d];
            k[c][d]=h[c][d];
        }
    }
    flag=-1;
    tt=0;
    memset(j,0,sizeof(j));
    dffs(1,b,a);
    e=-1;
    if(tt!=0)
    {for(c=1;c<=a;c++)
    {
        for(d=1;d<=b;d++)
        {
            if(d==b)
                printf("%c\n",zz[++e]);
            else printf("%c ",zz[++e]);
        }
    }
    }
    else printf("IMPOSSIBLE\n");
    return 0;
}

 

内容概要:本文介绍了多种开发者工具及其对开发效率的提升作用。首先,介绍了两款集成开发环境(IDE):IntelliJ IDEA 以其智能代码补全、强大的调试工具和项目管理功能适用于Java开发者;VS Code 则凭借轻量级和多种编程语言的插件支持成为前端开发者的常用工具。其次,提到了基于 GPT-4 的智能代码生成工具 Cursor,它通过对话式编程显著提高了开发效率。接着,阐述了版本控制系统 Git 的重要性,包括记录代码修改、分支管理和协作功能。然后,介绍了 Postman 作为 API 全生命周期管理工具,可创建、测试和文档化 API,缩短前后端联调时间。再者,提到 SonarQube 这款代码质量管理工具,能自动扫描代码并检测潜在的质量问题。还介绍了 Docker 容器化工具,通过定义应用的运行环境和依赖,确保环境一致性。最后,提及了线上诊断工具 Arthas 和性能调优工具 JProfiler,分别用于生产环境排障和性能优化。 适合人群:所有希望提高开发效率的程序员,尤其是有一定开发经验的软件工程师和技术团队。 使用场景及目标:①选择合适的 IDE 提升编码速度和代码质量;②利用 AI 编程助手加快开发进程;③通过 Git 实现高效的版本控制和团队协作;④使用 Postman 管理 API 的全生命周期;⑤借助 SonarQube 提高代码质量;⑥采用 Docker 实现环境一致性;⑦运用 Arthas 和 JProfiler 进行线上诊断和性能调优。 阅读建议:根据个人或团队的需求选择适合的工具,深入理解每种工具的功能特点,并在实际开发中不断实践和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值