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.
思路
状态压缩+枚举。这题是不能直接推演出如何踩砖块步骤数最少。只能通过枚举和检查来不断维护最小步骤数。可以写成dfs的形式。
这题没有必要再写一个函数来判断字典序大小。因为第一行的个数的1
是增加的,之后可能出现步骤数相等的情况,但是显然第一次的成功的字典序小于后者。
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <vector>
typedef long double ld;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define mt make_tuple
#define eb emplace_back
#define pob pop_back
#define rz resize
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define debug(a) cout<<#a<<"="<<a<<endl;
#define xx first
#define yy second
#define qwe(i,a,b) for(int i=a;i<=b;i++)
#define ewq(i,a,b) for(int i=a;i>=b;i--)
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll INF=0x3f3f3f3f,inf=0x3f3f3f3f3f3f3f;
const ll mod[2]={int(1e9 + 7), 10007};
const int base[2]={29,31};
const int maxn=1e2+6;
ll qpow(ll x,ll n){ll ans=1;while(n>0){if(n&1)ans=ans*x%mod[1];x=x*x%mod[1];n>>=1;}return ans;}
int n,m;
int a[maxn][maxn],flip[maxn][maxn],cnt; // 原图 当前翻转情况 当前步骤数
int ans[maxn][maxn],st=INF; // 答案 最少步骤数
int vec[][2]={{-1,0},{1,0},{0,0},{0,-1},{0,1}}; // 方向
inline bool in_area(int x,int y) {
return x>=0 && x<n && y>=0 && y<m;
}
int white_or_black(int x,int y) {
int p=a[x][y];
for(int i=0;i<5;i++) {
int tx=x+vec[i][0],ty=y+vec[i][1];
if(in_area(tx,ty)) p+=flip[tx][ty];
}
return p&1; // 1为black,下面的一行的j位必须翻转
}
int find_steps() {
for(int i=1;i<n;i++) {
for(int j=0;j<m;j++) {
if(white_or_black(i-1,j)) {
flip[i][j]++;
cnt++;
}
}
}
for(int j=0;j<m;j++) {
if(white_or_black(n-1,j)) return INF; // 有位置为1,代表没有完全翻转
}
return cnt;
}
int main(int argc, char const *argv[]) {
n=rr(),m=rr();
qwe(i,0,n-1)
qwe(j,0,m-1)
a[i][j]=rr();
int upper=1<<m;
for(int i=0;i<upper;i++) {
int t=INF;
mem(flip,0),cnt=0;
for(int j=0;j<m;j++)
flip[0][j]=i>>j&1,cnt+=flip[0][j]; // 枚举第一行的情况。
t=find_steps();
if(st>t) {
st=t;
memcpy(ans,flip,sizeof flip);
}
}
if(st==INF) std::cout << "IMPOSSIBLE" << '\n';
else {
qwe(i,0,n-1) {
qwe(j,0,m-1)
printf("%d ",ans[i][j]);
printf("\n");
}
}
return 0;
}