码迷,mamicode.com
首页 > 其他好文 > 详细

Fliptile 开关问题 poj 3279

时间:2015-03-29 23:39:51      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4031   Accepted: 1539

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 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

Source

 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <string>
  7 #include <vector>
  8 #include <set>
  9 #include <map>
 10 #include <queue>
 11 #include <stack>
 12 #include <sstream>
 13 #include <iomanip>
 14 using namespace std;
 15 const int INF=0x4fffffff;
 16 const int EXP=1e-6;
 17 const int MS=16;
 18 
 19 int ans[MS][MS];
 20 int pic[MS][MS];
 21 int flag[MS][MS];
 22 int M,N;
 23 int dir[5][2]={{0,0},{0,1},{1,0},{0,-1},{-1,0}};
 24 
 25 int color(int x,int y)
 26 {
 27       int sum=pic[x][y];
 28       for(int i=0;i<5;i++)
 29       {
 30             int r=x+dir[i][0];
 31             int c=y+dir[i][1];
 32             if(r>=0&&r<M&&c>=0&&c<N)
 33                   sum+=flag[r][c];
 34       }
 35       return sum&1;
 36 }
 37 
 38 int calc()
 39 {
 40       int res=0;
 41       for(int i=1;i<M;i++)
 42       {
 43             for(int j=0;j<N;j++)
 44             {
 45                   if(color(i-1,j))
 46                   {
 47                         flag[i][j]=1;
 48                   }
 49             }
 50       }
 51       for(int i=0;i<N;i++)
 52             if(color(M-1,i))
 53                   return -1;
 54       for(int i=0;i<M;i++)
 55             for(int j=0;j<N;j++)
 56                   res+=flag[i][j];
 57       return res;
 58 }
 59 
 60 void solve()
 61 {
 62       int res=-1;
 63       for(int i=0;i<1<<N;i++)
 64       {
 65             memset(flag,0,sizeof(flag));
 66             for(int j=0;j<N;j++)
 67                   flag[0][N-1-j]=(i>>j)&1;
 68             int cnt=calc();
 69             if(cnt>=0&&(res<0||res>cnt))
 70             {
 71                   res=cnt;
 72                   memcpy(ans,flag,sizeof(flag));
 73             }
 74       }
 75       if(res<0)
 76             printf("IMPOSSIBLE\n");
 77       else
 78       {
 79             for(int i=0;i<M;i++)
 80             {
 81                   for(int j=0;j<N;j++)
 82                   {
 83                         if(j)
 84                               printf(" ");
 85                         printf("%d",ans[i][j]);
 86                   }
 87                   printf("\n");
 88             }
 89       }
 90 }
 91 
 92 int main()
 93 {
 94       scanf("%d%d",&M,&N);
 95       for(int i=0;i<M;i++)
 96             for(int j=0;j<N;j++)
 97                   scanf("%d",&pic[i][j]);
 98       solve();
 99       return 0;
100 }

 

Fliptile 开关问题 poj 3279

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4376575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!