zoukankan      html  css  js  c++  java
  • Fliptile 开关问题 poj 3279

    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
    ");
     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("
    ");
     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 }
  • 相关阅读:
    P1197 [JSOI2008]星球大战[并查集+图论]
    P1955 [NOI2015]程序自动分析[离散化+并查集]
    取模运算律[简单数学]
    P1462 通往奥格瑞玛的道路[最短路+二分+堆优化]
    P1330 封锁阳光大学[搜索+染色]
    P1168 中位数[堆 优先队列]
    P2661 信息传递[最小环+边带权并查集]
    P1080 【NOIP 2012】 国王游戏[贪心+高精度]
    P2085 最小函数值[优先队列]
    【转】priority_queue的用法
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4376575.html
Copyright © 2011-2022 走看看