zoukankan      html  css  js  c++  java
  • POJ 3279 枚举(思维)

    Fliptile
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10931   Accepted: 4029

    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.先枚举第一行的翻转情况,然后相邻的下一行的翻转情况就已经确定,因为这时只有下一行的翻转能改变当前行的状态,这样每个状态只由第一行确定。

    2.要保证字典序最小,只需使第一行的字典序最小。

    代码:

     1 #include "cstdio"
     2 #include "stdlib.h"
     3 #include "iostream"
     4 #include "algorithm"
     5 #include "string"
     6 #include "cstring"
     7 #include "queue"
     8 #include "cmath"
     9 #include "vector"
    10 #include "map"
    11 #include "set"
    12 #define mj
    13 #define db double
    14 #define ll long long
    15 using namespace std;
    16 const int inf = 0x3f3f3f3f;
    17 const int N = 17;
    18 const int dx[] = {-1, 0, 1, 0, 0};
    19 const int dy[] = { 0,-1, 0, 1, 0};
    20 
    21 int s[N][N], st[N][N], tmp[N][N], rec[N][N];
    22 int n, m, ans;
    23 void flip(int x, int y) {
    24     tmp[x][y] = 1;
    25     int nx, ny;
    26     for(int i = 0; i < 5; i++) {
    27         nx = x+dx[i];
    28         ny = y+dy[i];
    29         st[nx][ny] = !st[nx][ny];
    30     }
    31 }
    32 bool ok() {
    33     for(int j = 1; j <= m; j++) {
    34         if(st[n][j]) return 0;
    35     }
    36     return 1;
    37 }
    38 void f(int t) {
    39     memcpy(st, s, sizeof(s));
    40     memset(tmp, 0, sizeof(tmp));
    41     int cnt = 0;
    42     for(int j = 0; j < m; j++) {
    43         if((t>>j) & 1) {//从第一行的二进制第一位开始枚举翻转状态。
    44             flip(1, j+1);
    45             cnt++;
    46         }
    47     }
    48     for(int i = 2; i <= n; i++) {
    49         for(int j = 1; j <= m; j++) {
    50             if(st[i-1][j] == 1) {
    51                 flip(i, j);
    52                 cnt++;
    53             }
    54         }
    55     }
    56     if(ok() && cnt < ans) {
    57         ans = cnt;
    58         memcpy(rec, tmp, sizeof(tmp));
    59     }
    60 }
    61 
    62 int main() {
    63     while(scanf("%d%d", &n, &m) != EOF) {
    64         for(int i = 1; i <= n; i++) {
    65             for(int j = 1; j <= m; j++) {
    66                 scanf("%d", &s[i][j]);
    67             }
    68         }
    69         ans = inf;
    70         int end = 1 << m;
    71         for(int t = 0; t < end; t++)  f(t);
    72         if(ans == inf) puts("IMPOSSIBLE");
    73         else {
    74             for(int i = 1; i <= n; i++) {
    75                 printf("%d", rec[i][1]);
    76                 for(int j = 2; j <= m; j++)
    77                     printf(" %d", rec[i][j]);
    78                 puts("");
    79             }
    80         }
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    node 使用 ssh2-sftp-client 实现 FTP 的文件上传和下载功能
    gulp 使用 gulp.spritesmith 组件生成雪碧图
    webpack4 打包多页面应用
    javascript 两个变量值的互换
    微信公众号爬坑(react)
    文件上传(H5 + node)
    前后端分离 ajax 跨域 session 传值 (后端使用 node)
    webpack dev 日记
    npm报错 端口被占用 events.js:167 throw er; // Unhandled 'error' event Error: listen EADDRINUSE 127.0.0.1:9999
    多个 img 之间存在间隔
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/7200428.html
Copyright © 2011-2022 走看看