zoukankan      html  css  js  c++  java
  • POJ 3279

    Fliptile
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 3062   Accepted: 1178

    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 <algorithm>
     5 
     6 using namespace std;
     7 
     8 const int MAX = 20;
     9 const int dx[] = {-1,0,0,0,1};
    10 const int dy[] = {0,-1,0,1,0};
    11 int N,M;
    12 int tile[MAX][MAX];
    13 int flip[MAX][MAX];
    14 int opt[MAX][MAX];
    15 
    16 int get(int x,int y) {
    17         int c = tile[x][y];
    18         for(int d = 0; d < 5; ++d) {
    19             int x1 = x + dx[d],y1 = y + dy[d];
    20             if(x1 >= 1 && x1 <= N && y1 >= 0 && y1 < M) {
    21                 c += flip[x1][y1];
    22             }
    23         }
    24         return c % 2;
    25 
    26 }
    27 int cal() {
    28         int res = 0;
    29         int t[MAX * MAX + 5];
    30         for(int i = 2; i <= N; ++i) {
    31                 for(int j = 0; j < M; ++j) {
    32                         if(get(i - 1,j)) {
    33                                 flip[i][j] = 1;
    34                         }
    35                 }
    36         }
    37 
    38         for(int i = 0; i < M; ++i) {
    39                 if(get(N,i)) return -1;
    40         }
    41 
    42         for(int i = 1; i <= N; ++i) {
    43                 for(int j = 0; j < M; ++j) {
    44                         res += flip[i][j];
    45                 }
    46         }
    47 
    48         return res;
    49 
    50 }
    51 
    52 void solve() {
    53         int res = -1;
    54         for(int s = 0; s < (1 << M); ++s) {
    55                 memset(flip,0,sizeof(flip));
    56                 for(int i = 0; i < M; ++i) {
    57                         if(s & (1 << i)) flip[1][i] = 1;
    58                 }
    59                 int num = cal();
    60                 //printf("num = %d
    ",num);
    61                 if(num >= 0 && (res < 0 || res > num)) {
    62                         res = num;
    63                         memcpy(opt,flip,sizeof(flip));
    64                 }
    65 
    66         }
    67 
    68         if(res < 0) {
    69                 printf("IMPOSSIBLE
    ");
    70         } else {
    71                 for(int i = 1; i <= N; ++i) {
    72                         for(int j = 0; j < M; ++j) {
    73                                 printf("%d",opt[i][j]);
    74                                 if(j != M - 1) printf(" ");
    75 
    76                         }
    77                         printf("
    ");
    78                 }
    79         }
    80 }
    81 
    82 int main()
    83 {
    84    // freopen("sw.in","r",stdin);
    85     scanf("%d%d",&N,&M);
    86     for(int i = 1; i <= N; ++i) {
    87             for(int j = 0; j < M; ++j) {
    88                     scanf("%d",&tile[i][j]);
    89                     //printf("%d ",tile[i][j]);
    90             }
    91             //printf("
    ");
    92     }
    93 
    94     solve();
    95     //cout << "Hello world!" << endl;
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    git配置config记住密码
    C#调用c++类的导出函数
    经典算法之直接插入排序
    经典算法之冒泡排序
    经典算法之二分查找
    shell实战之Linux主机系统监控
    shell高级特性-4
    shell实战之tomcat看门狗
    shell函数-3
    shell运算符与流程控制-2
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3632483.html
Copyright © 2011-2022 走看看