Fliptile(枚举+DFS)

简介: Problem Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk.

Problem 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.

SampleInput

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

SampleOutput

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

这题也是kuangbin搜索专题里面的一题,题意就是一个n*m的矩阵,0代表灯关,1代表灯开,按其中一个按钮,自身以及上下左右都会变成相反状态,就是和我们玩的关灯游戏一样,问你最少按几次就能全部关掉,多种情况的话输出字典序最小的。
这题目可以用递推的思路,从最上面开始操作,下一行的操作都会由上一行得出,最后我们判断最后一行是否都为0就行了。

而对于第一行来说,最坏一共有2^m种情况,我们只需要枚举每一种情况即可,然后若是有多组,输出字典序小值就好。
其他的话就没什么解释的啦,代码里面我加了注释,直接看代码吧=7=
代码:
  1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <sstream>
  6 #include <iomanip>
  7 #include <map>
  8 #include <stack>
  9 #include <deque>
 10 #include <queue>
 11 #include <vector>
 12 #include <set>
 13 #include <list>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <algorithm>
 17 #include <iterator>
 18 #include <cmath>
 19 #include <bitset>
 20 #include <ctime>
 21 #include <fstream>
 22 #include <limits.h>
 23 #include <numeric>
 24 
 25 using namespace std;
 26 
 27 #define F first
 28 #define S second
 29 #define mian main
 30 #define ture true
 31 
 32 #define MAXN 1000000+5
 33 #define MOD 1000000007
 34 #define PI (acos(-1.0))
 35 #define EPS 1e-6
 36 #define MMT(s) memset(s, 0, sizeof s)
 37 typedef unsigned long long ull;
 38 typedef long long ll;
 39 typedef double db;
 40 typedef long double ldb;
 41 typedef stringstream sstm;
 42 const int INF = 0x3f3f3f3f;
 43 
 44 int mp[20][20],tp[20][20],s[20][20];
 45 int n,m;
 46 int fx[5][2] = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
 47 
 48 int fun(int x,int y){    //判断x、y旁边的5个位置的颜色得出x、y位置的颜色
 49     int temp = mp[x][y];
 50     for(int i = 0; i < 5; i++){
 51         int next_x = x+fx[i][0];
 52         int next_y = y+fx[i][1];
 53 
 54         if(next_x < 1 || next_x > n || next_y < 1 || next_y > m)
 55             continue;
 56         temp += tp[next_x][next_y];
 57     }
 58     return temp%2;
 59 }
 60 
 61 int dfs(){
 62     for(int i = 2; i <= n; i++)
 63         for(int j = 1; j <= m; j++)
 64             if(fun(i-1,j))    //上一行位置灯开状态,此位置就必须开灯使上一行熄灭
 65                 tp[i][j] = 1;
 66 
 67     for(int i = 1; i <= m; i++)  //最后一行全部为0,直接结束
 68         if(fun(n,i))
 69             return -1;
 70 
 71     int res = 0;
 72     for(int i = 1; i <= n; i++)
 73         for(int j = 1; j <= m; j++)  //得出大小,因为后面会比较字典序
 74             res += tp[i][j];
 75     return res;
 76 }
 77 
 78 int main(){
 79     ios_base::sync_with_stdio(false);
 80     cin.tie(0);
 81     cout.tie(0);
 82     while(cin>>n>>m){
 83         for(int i = 1; i <= n; i++)
 84             for(int j = 1; j <= m; j++)
 85                 cin>>mp[i][j];
 86 
 87         int flag = 0;
 88         int ans = INF;
 89         for(int i = 0; i < 1<<m; i++){    //枚举第一行的所有状态
 90             memset(tp,0,sizeof(tp));
 91 
 92             for(int j = 1; j <= m; j++)
 93                 tp[1][m-j+1] = i>>(j-1) & 1;
 94             int cnt = dfs();
 95             if(cnt >= 0 && cnt < ans){  //得出字典序最小的
 96                 flag = 1;
 97                 ans = cnt;
 98                 memcpy(s,tp,sizeof(tp));
 99             }
100         }
101         if(!flag)
102             cout<<"IMPOSSIBLE"<<endl;
103         else{
104             for(int i = 1;i <= n;i ++){
105                 for(int j = 1;j <= m;j ++){
106                     if(j != 1)
107                         cout<<" ";
108                     cout<<s[i][j];
109                 }
110                 cout<<endl;
111             }
112         }
113     }
114     return 0;
115 }

 

目录
相关文章
|
2天前
|
云安全 数据采集 人工智能
古茗联名引爆全网,阿里云三层防护助力对抗黑产
阿里云三层校验+风险识别,为古茗每一杯奶茶保驾护航!
古茗联名引爆全网,阿里云三层防护助力对抗黑产
|
6天前
|
人工智能 中间件 API
AutoGen for .NET - 架构学习指南
《AutoGen for .NET 架构学习指南》系统解析微软多智能体框架,涵盖新旧双架构、核心设计、技术栈与实战路径,助你从入门到精通,构建分布式AI协同系统。
305 142
|
2天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
404 0
|
3天前
|
传感器 人工智能 算法
数字孪生智慧水务系统,三维立体平台,沃思智能
智慧水务系统融合物联网、数字孪生与AI技术,实现供水全流程智能监测、预测性维护与动态优化。通过实时数据采集与三维建模,提升漏损控制、节能降耗与应急响应能力,推动水务管理从经验驱动迈向数据驱动,助力城市水资源精细化、可持续化管理。
267 142
|
2天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践
本文介绍RAG(检索增强生成)技术,结合Spring AI与本地及云知识库实现学术分析AI应用,利用阿里云Qwen-Plus模型提升回答准确性与可信度。
204 90
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践
|
17天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
2天前
|
机器学习/深度学习 人工智能 运维
智能照明稳压节能控制器,路灯节能稳压系统,沃思智能
智能照明调控柜集电力分配、远程控制与能耗管理于一体,支持自动调光、场景切换与云平台运维,广泛应用于市政、商业及工业领域,显著节能降耗,助力智慧城市建设。
184 137
kde
|
2天前
|
人工智能 关系型数据库 PostgreSQL
n8n Docker 部署手册
n8n是一款开源工作流自动化平台,支持低代码与可编程模式,集成400+服务节点,原生支持AI与API连接,可自托管部署,助力团队构建安全高效的自动化流程。
kde
263 3

热门文章

最新文章