C - 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".
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
4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1Sample Output
0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0
1 // 2 // Created by w on 2020/11/2. 3 // 4 #include <iostream> 5 #include <cstring> 6 using namespace std; 7 int dr[]={1,0,-1,0,0}; 8 int dc[]={0,-1,0,1,0}; 9 const int maxn=20; 10 int n,m; 11 int tile[maxn][maxn]; 12 int file[maxn][maxn],opt[maxn][maxn];//分别保存中间结果和最优解 13 bool inside(int x,int y) 14 { 15 return x>=0&&x<m&&y>=0&&y<n; 16 } 17 int get(int x,int y)//查询(x,y)的颜色 18 { 19 int c=tile[x][y]; 20 for(int i=0;i<5;i++)//看它周围和自己是否翻转过 21 { 22 int dx=x+dr[i],dy=y+dc[i]; 23 if(inside(dx,dy)) 24 { 25 c+=file[dx][dy]; 26 } 27 } 28 return c%2; 29 } 30 //求出第一行确定情况下的最小操作数,不存在解的话返回-1 31 int calc() 32 {//求出从第2行开始的翻转方法 33 for(int i=1;i<m;i++) 34 { 35 for(int j=0;j<n;j++) 36 { 37 //(i-1,j)是黑色的话,则必须翻转这个格子 38 if(get(i-1,j)!=0) 39 { 40 file[i][j]=1; 41 } 42 } 43 } 44 //判断最后一行是否全白 45 for(int j=0;j<n;j++) 46 {//无解 47 if(get(m-1,j)!=0)return -1; 48 } 49 //统计翻转的次数 50 int res=0; 51 for(int i=0;i<m;i++) 52 { 53 for(int j=0;j<n;j++) 54 { 55 res+=file[i][j]; 56 } 57 } 58 return res; 59 } 60 void solve() 61 { 62 int res=0x3f3f3f3f; 63 //按照字典序尝试第一行的所有可能 64 for(int i=0;i<(1<<n);i++)//二进制枚举子集 65 { 66 memset(file,0,sizeof(file)); 67 for(int j=0;j<n;j++) 68 { 69 file[0][n-j-1]=i>>j&1;//因为要让字典序最小 70 } 71 int num=calc(); 72 if(num>=0&&num<res) 73 { 74 res=num; 75 memcpy(opt,file,sizeof(file));//复制数组 76 } 77 } 78 if(res==0x3f3f3f3f) 79 { 80 cout<<"IMPOSSIBLE"<<endl; 81 } 82 else 83 { 84 for(int i=0;i<m;i++) 85 { 86 for(int j=0;j<n;j++) 87 { 88 if(j==0)cout<<opt[i][j]; 89 else cout<<" "<<opt[i][j]; 90 } 91 cout<<endl; 92 } 93 } 94 } 95 int main() 96 { 97 while (cin>>m>>n) 98 { 99 for(int i=0;i<m;i++) 100 { 101 for(int j=0;j<n;j++) 102 cin>>tile[i][j]; 103 } 104 solve(); 105 } 106 return 0; 107 }

浙公网安备 33010602011771号