zoukankan      html  css  js  c++  java
  • POJ3279 搜索/思维

    Fliptile
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10457   Accepted: 3866

    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

     
     
    只要枚举第一行的格子的翻转情况,然后下面的行数翻转使得上面的格子变为白色,枚举完之后前(n-1)行一定全白,只要检查最后一行就ok
    看的题解的思路自己没想到这样写哎
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define inf 0x3f3f3f3f
    int N,M;
    int e1[20][20];
    int e2[20][20];
    int tim[20][20];
    int an[20][20];
    bool e3[20];
    int ans=inf;
    int fx[4][2]={1,0,-1,0,0,1,0,-1};
    void turn(int x,int y)
    {
        e2[x][y]=(e2[x][y]==0?1:0);
        for(int i=0;i<4;++i){
            int dx=x+fx[i][0];
            int dy=y+fx[i][1];
             if(dx<1||dy<1||dx>N||dy>M) continue;
                     e2[dx][dy]=(e2[dx][dy]==0?1:0);
        }
    }
    void make()
    {
      int i,j;
      for(i=2;i<=N;++i){
        for(j=1;j<=M;++j){
            int up=e2[i-1][j];
            if(up==0) continue;
            else{
               turn(i,j);
               tim[i][j]++;
            }
        }
      }int s=0;
      for(i=1;i<=M;++i) if(e2[N][i]==0) s++;

      if(s==M){
        s=0;

        for(i=1;i<=N;++i){
            for(j=1;j<=M;++j) {
                    s+=tim[i][j];
           // cout<<tim[i][j];
           // if(j!=M) cout<<" ";
            }
            //cout<<endl;
        }

        if(s<ans){
            ans=s;
            memcpy(an,tim,sizeof(int)*400);
        }
      }
    }
    void solve()
    {
       int i,j,k;
       memcpy(e2,e1,sizeof(int)*400);
       memset(tim,0,sizeof(tim));
       for(i=1;i<=M;++i){
        if(e3[i]){
            turn(1,i);
            tim[1][i]++;
        }
       }
       make();
    }
    void dfs(int x)
    {
      if(x==M+1){
        solve();
        return;
      }
      for(int i=0;i<2;++i){
        e3[x]=i;
        dfs(x+1);
      }
    }
    int main()
    {
        int i,j,k;
        while(cin>>N>>M){ans=inf;
            for(i=1;i<=N;++i)
                for(j=1;j<=M;++j) {
                        cin>>e1[i][j];
                }
            dfs(1);
            if(ans==inf) puts("IMPOSSIBLE");
            else{
                for(i=1;i<=N;++i){
                    for(j=1;j<=M;++j){
                        if(j!=1) cout<<" ";
                        cout<<an[i][j];
                    }
                    cout<<endl;
                }
            }
        }
        return 0;
    }


  • 相关阅读:
    angularjs事件通信$on,$emit,$broadcast详解
    es6入门7--Set Map数据结构
    ES6函数参数默认值作用域的模拟原理实现与个人的一些推测
    JS判断数组是否包含某元素
    es6入门6--数组拓展运算符,Array.from()基本用法
    js new一个对象的过程,实现一个简单的new方法
    js中的NaN,isNaN与Number.isNaN的区别,如何判断一个值严格等于NaN
    详解 JDK8 新增的日期时间类
    详解 枚举
    详解 动态代理
  • 原文地址:https://www.cnblogs.com/zzqc/p/7112806.html
Copyright © 2011-2022 走看看