poj3279(状态压缩)

来源:互联网 发布:微商吸粉软件 编辑:程序博客网 时间:2024/05/20 07:37

http://poj.org/problem?id=3279

Fliptile
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10085 Accepted: 3740

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

题意:给定一个0 1矩阵,对一个点进行操作,那么上下左右以及本身从1变成0,0变成1,求将矩阵变成零矩阵所用最少的次数并打印方案

思路:枚举第一行的所有翻转情况 从0...00-1...11,0表示不反转,1表示翻转。如0011表示第0,1个翻转。

再扫下一行,如果该位置的上方为1,那么该位置就要翻转。然后看最后一行如果都是0那么该方案可行。

flip[i][j]:1表示该位置进行翻转操作,0表示不操作

ans[i][j]:维护最小次数的操作方案

color():返回某位置的状态


代码:

#include <iostream>#include <cstdio>#include <cstring>#define INT 500using namespace std;int map[20][20];int flip[20][20];int ans[20][20];int dicx[]={1,-1,0,0,0};int dicy[]={0,0,1,-1,0};int n,m;int num;//返回位置(x,y)的状态 int color(int x,int y){int cnt=map[x][y];for(int i=0;i<5;i++){int xx=x+dicx[i];int yy=y+dicy[i];if(xx>=0&&xx<n&&yy>=0&&yy<m)cnt+=flip[xx][yy];}return cnt&1;}int solve(){//从第二行开始枚举每一行 for(int i=1;i<n;i++)for(int j=0;j<m;j++)if(color(i-1,j))//如果该位置上方为1,那么该位置要翻转 flip[i][j]=1;//扫最后一行,如果存在1,那么该方案不通过 for(int i=0;i<m;i++)if(color(n-1,i))return INT;//统计翻转次数 int cnt=0;for(int i=0;i<n;i++)for(int j=0;j<m;j++)cnt+=flip[i][j];return cnt;}int main(){while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;i++)for(int j=0;j<m;j++)scanf("%d",&map[i][j]);//初始化 num=INT;memset(ans,0,sizeof(ans));//枚举第一行的所有可能的翻转情况 for(int i=0;i<(1<<m);i++){memset(flip,0,sizeof(flip));for(int j=0;j<m;j++)flip[0][j]=(i>>j)&1;int temp=solve();if(temp<num){num=temp;//保存最小操作次数的方案 for(int i=0;i<n;i++)for(int j=0;j<m;j++)ans[i][j]=flip[i][j];}}if(num==INT){printf("IMPOSSIBLE\n");continue;}for(int i=0;i<n;i++){for(int j=0;j<m;j++){printf("%d",ans[i][j]);if(j==m-1) printf("\n");else printf(" ");}}}return 0;} 


0 0
原创粉丝点击