Fliptile (列舉搜尋)
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
遇到的坑: 1>m和n弄混了 2>dfs不能對每一層的公共變數直接修改,應該弄一個副本,修改後傳到下一層去 3>搜尋是有策略的,像這道題,每一行的1可以有翻轉下一行來改變,所以不是暴力翻轉每一個位置,a[i - 1][j]是1,則可以翻轉i,j這個位置,使得a[i - 1][j] = 0;
思路:暴力列舉第一行的所有情況,從0開始找的話,就是字典序從小到大,利用二進位制思想列舉第一行,然後dfs下面每一行,採取a[i - 1][j]是1,則可以翻轉i,j這個位置的策略,最後一行翻轉完了,看最後一行是否全為0,如果全為0,則存在一個方案; 取所有方案裡翻轉數量最小的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
int ans[16][16],sta[16][16],tmp[16][16],mp[16][16];
int m,n;
int col[5] = {0,1,-1,0,0};
int con[5] = {0,0,0,1,-1};
bool flag = false;
bool judge(int i,int j)
{
if(i < 0 || j < 0)
return false;
if(i >= m || j >= n)
return false;
return true;
}
void Reverse(int a[16][16],int x,int y)
{
for(int i = 0;i < 5;++i)
{
int x1 = x + col[i];
int y1 = y + con[i];
if(judge(x1,y1)){
a[x1][y1] = !a[x1][y1];
}
}
}
//注意dfs時,每一層中共用的變數不能改變
void dfs(int a[16][16],int num)
{
if(flag) return;
if(num == m){
int sum = 0;
for(int i = 0;i < n;++i)
sum += a[m - 1][i];
if(sum == 0){
flag = true;
}
return ;
}
for(int i = 0;i < n;++i)
if(a[num - 1][i] == 1){
sta[num][i] = 1;
Reverse(a,num,i);
}
dfs(a,num + 1);
}
int main()
{
while(~scanf("%d %d",&m,&n))
{
for(int i = 0;i < m;++i)
for(int j = 0;j < n;++j)
scanf("%d",&ans[i][j]);
// Reverse(ans,1,1);
// for(int i = 0;i < m;++i)
// {
// for(int j = 0;j < n;++j){
// cout << ans[i][j] << " ";
// }
// cout << endl;
// }
int MIN = inf;
for(int i = 0;i <= ((1 << n) - 1);++i)
{
for(int x = 0;x < m;++x)
for(int y = 0;y < n;++y){
tmp[x][y] = ans[x][y];
sta[x][y] = 0;
}
for(int j = 0;j < n;++j)
{
if((1 << j) & i){
Reverse(tmp,0,n - j - 1);
sta[0][n - j - 1] = 1;
}
}
flag = false;
dfs(tmp,1);
if(flag){
int res = 0;
for(int i = 0;i < m;++i)
{
for(int j = 0;j < n;++j){
if(sta[i][j] == 1) res++;
}
}
if(res < MIN){
MIN = res;
memcpy(mp,sta,sizeof(sta));
}
}
}
if(MIN == inf){
printf("IMPOSSIBLE\n");
}else{
for(int i = 0;i < m;++i)
{
for(int j = 0;j < n;++j){
if(j == n - 1) printf("%d\n",mp[i][j]);
else printf("%d ",mp[i][j]);
}
}
}
}
return 0;
}