• Fliptile(枚举+DFS)


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

    SampleInput

    4 4
    1 0 0 1
    0 1 1 0
    0 1 1 0
    1 0 0 1

    SampleOutput

    0 0 0 0
    1 0 0 1
    1 0 0 1
    0 0 0 0

    这题也是kuangbin搜索专题里面的一题,题意就是一个n*m的矩阵,0代表灯关,1代表灯开,按其中一个按钮,自身以及上下左右都会变成相反状态,就是和我们玩的关灯游戏一样,问你最少按几次就能全部关掉,多种情况的话输出字典序最小的。
    这题目可以用递推的思路,从最上面开始操作,下一行的操作都会由上一行得出,最后我们判断最后一行是否都为0就行了。

    而对于第一行来说,最坏一共有2^m种情况,我们只需要枚举每一种情况即可,然后若是有多组,输出字典序小值就好。
    其他的话就没什么解释的啦,代码里面我加了注释,直接看代码吧=7=
    代码:
      1 #include <iostream>
      2 #include <string>
      3 #include <cstdio>
      4 #include <cstdlib>
      5 #include <sstream>
      6 #include <iomanip>
      7 #include <map>
      8 #include <stack>
      9 #include <deque>
     10 #include <queue>
     11 #include <vector>
     12 #include <set>
     13 #include <list>
     14 #include <cstring>
     15 #include <cctype>
     16 #include <algorithm>
     17 #include <iterator>
     18 #include <cmath>
     19 #include <bitset>
     20 #include <ctime>
     21 #include <fstream>
     22 #include <limits.h>
     23 #include <numeric>
     24 
     25 using namespace std;
     26 
     27 #define F first
     28 #define S second
     29 #define mian main
     30 #define ture true
     31 
     32 #define MAXN 1000000+5
     33 #define MOD 1000000007
     34 #define PI (acos(-1.0))
     35 #define EPS 1e-6
     36 #define MMT(s) memset(s, 0, sizeof s)
     37 typedef unsigned long long ull;
     38 typedef long long ll;
     39 typedef double db;
     40 typedef long double ldb;
     41 typedef stringstream sstm;
     42 const int INF = 0x3f3f3f3f;
     43 
     44 int mp[20][20],tp[20][20],s[20][20];
     45 int n,m;
     46 int fx[5][2] = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
     47 
     48 int fun(int x,int y){    //判断x、y旁边的5个位置的颜色得出x、y位置的颜色
     49     int temp = mp[x][y];
     50     for(int i = 0; i < 5; i++){
     51         int next_x = x+fx[i][0];
     52         int next_y = y+fx[i][1];
     53 
     54         if(next_x < 1 || next_x > n || next_y < 1 || next_y > m)
     55             continue;
     56         temp += tp[next_x][next_y];
     57     }
     58     return temp%2;
     59 }
     60 
     61 int dfs(){
     62     for(int i = 2; i <= n; i++)
     63         for(int j = 1; j <= m; j++)
     64             if(fun(i-1,j))    //上一行位置灯开状态,此位置就必须开灯使上一行熄灭
     65                 tp[i][j] = 1;
     66 
     67     for(int i = 1; i <= m; i++)  //最后一行全部为0,直接结束
     68         if(fun(n,i))
     69             return -1;
     70 
     71     int res = 0;
     72     for(int i = 1; i <= n; i++)
     73         for(int j = 1; j <= m; j++)  //得出大小,因为后面会比较字典序
     74             res += tp[i][j];
     75     return res;
     76 }
     77 
     78 int main(){
     79     ios_base::sync_with_stdio(false);
     80     cin.tie(0);
     81     cout.tie(0);
     82     while(cin>>n>>m){
     83         for(int i = 1; i <= n; i++)
     84             for(int j = 1; j <= m; j++)
     85                 cin>>mp[i][j];
     86 
     87         int flag = 0;
     88         int ans = INF;
     89         for(int i = 0; i < 1<<m; i++){    //枚举第一行的所有状态
     90             memset(tp,0,sizeof(tp));
     91 
     92             for(int j = 1; j <= m; j++)
     93                 tp[1][m-j+1] = i>>(j-1) & 1;
     94             int cnt = dfs();
     95             if(cnt >= 0 && cnt < ans){  //得出字典序最小的
     96                 flag = 1;
     97                 ans = cnt;
     98                 memcpy(s,tp,sizeof(tp));
     99             }
    100         }
    101         if(!flag)
    102             cout<<"IMPOSSIBLE"<<endl;
    103         else{
    104             for(int i = 1;i <= n;i ++){
    105                 for(int j = 1;j <= m;j ++){
    106                     if(j != 1)
    107                         cout<<" ";
    108                     cout<<s[i][j];
    109                 }
    110                 cout<<endl;
    111             }
    112         }
    113     }
    114     return 0;
    115 }
  • 相关阅读:
    洛谷 P1593 因子和
    洛谷 P1167 刷题
    洛谷 P1613 跑路
    洛谷 P1629 邮递员送信
    洛谷 P1654 OSU!
    洛谷 P1967 货车运输
    FPGA开平方的实现
    FPGA设计思想之串并转换
    verilog乘法器的设计
    FPGA浮点数定点数的处理
  • 原文地址:https://www.cnblogs.com/xenny/p/9384239.html
Copyright © 2020-2023  润新知