D - Fliptile——POJ

来源:互联网 发布:5g网络哪些手机支持 编辑:程序博客网 时间:2024/06/06 17:20

D - Fliptile
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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.

Sample Input

4 41 0 0 10 1 1 00 1 1 01 0 0 1

Sample Output

0 0 0 01 0 0 11 0 0 10 0 0 0




自写:

#include<stdio.h>#include<string.h>int n,m;int inf=999999,res;int flag[20][20];int mid[20][20];int end[20][20];int dir[5][2]={{0,0},{1,0},{-1,0},{0,1},{0,-1}};int get(int x,int y);int cal();int main(){int i,j;while(scanf("%d %d",&m,&n)!=EOF){for(i=1;i<=m;i++)//录入 {for(j=1;j<=n;j++){scanf("%d",&flag[i][j]);}}res=inf;f<span style="font-family: Arial, Helvetica, sans-serif;">or(i=0;i<(1<<n);i++)</span><pre name="code" class="cpp">{memset(mid,0,sizeof(mid));for(j=0;j<n;j++)//字典序,先确定第一个 {mid[1][n-j]=i>>j&1;}int num=cal();//从第二行开始向下遍历 if(num>0&&num<res)//更新最小值 {res=num;memcpy(end,mid,sizeof(mid));}}

if(res==inf)printf("IMPOSSIBLE\n");else{for(i=1;i<=m;i++){for(j=1;j<=n;j++){if(j-1) printf(" ");printf("%d",end[i][j]);}printf("\n");}}}return 0;}int get(int x,int y)//判断其目前状态,是正还是负 {int i,xx,yy,sum=flag[x][y];for(i=0;i<5;i++){xx=x+dir[i][0];yy=y+dir[i][1];if(xx>=0&&xx<=m&&yy>=0&&yy<=n){sum+=mid[xx][yy];}}return sum%2;}int cal(){int i,j,sum=0;for(i=2;i<=m;i++)for(j=1;j<=n;j++){if(get(i-1,j)!=0)//跟据同一列的上一个行的数,来判断其是否 要反转 mid[i][j]=1;}for(j=1;j<=n;j++)if(get(m,j)!=0)//判断最后一行,若最后一行还需要反转,则其无法反转 return -1;for(i=1;i<=m;i++)for(j=1;j<=n;j++){sum+=mid[i][j];}return sum;}


知识点:

利用二进制数字典序:

<span style="white-space:pre"></span>for(i=0;i<(1<<n);i++){for(j=0;j<n;j++)//字典序,先确定第一个 {mid[1][n-j]=i>>j&1;}}


知识点2:

从头到尾,覆盖!



参考:


#include<iostream>#include<cstring>#include<cstdio>#include<climits>using namespace std;const int int_max = INT_MAX;const int dirx[5] = { 1, -1, 0, 0, 0 };const int diry[5] = { 0, 0, 0, 1, -1 };int tile[20][20];   //黑白标志 int flip[20][20];//int opt[20][20];//int n, m;//??(x,y)???int get(int x, int y){int c = tile[x][y];for (int i = 0; i < 5; i++){int xx = x + dirx[i],yy = y + diry[i];if (xx >= 0 && xx < m && yy >= 0 && yy < n)c += flip[xx][yy];}return c % 2;}//???????????????,????-1int calc(){//???????????for (int i = 1; i < m;i++)//第二行 for (int j = 0; j < n; j++){if (get(i - 1, j) != 0)flip[i][j] = 1;}//判断最后是否为全白 for (int j = 0; j < n;j++)if (get(m - 1, j) != 0)return -1;//统计反转次数 int ret = 0;for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)ret += flip[i][j];return ret;}int main(){while (scanf("%d%d", &m, &n) != EOF){for (int i = 0; i < m;i++)for (int j = 0; j < n; j++)scanf("%d", &tile[i][j]);int res = int_max;//字典序 for (int i = 0; i < (1 << n); i++){memset(flip, 0, sizeof(flip));for(int j = 0; j < n; j++)flip[0][n - j - 1] = i >> j & 1;int num = calc();if (num > 0 && num < res){res = num;memcpy(opt,flip,sizeof(flip));}}if (res == int_max) printf("IMPOSSIBLE\n");else{for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){if (j) printf(" ");printf("%d", opt[i][j]);}printf("\n");}}}}


0 0
原创粉丝点击