zoukankan      html  css  js  c++  java
  • POJ 1753 Flip Game (IDA*)

    题意:4*4的棋盘摆满棋子,有黑有白,翻转一个棋子的同时也将翻转其上下左右的棋子(翻转后黑变白,白变黑),问使棋盘上所有棋子颜色相同,最少翻转的棋子数。

    分析:

    1、每个棋子至多翻转1次。翻转偶数次与不翻转结果相同,翻转奇数次与翻转1次结果相同。

    2、每个棋子翻转或不翻转,共有216种情况。

    3、IDA*搜索。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 10000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    char pic[5][5];
    char tmp[5][5];
    int maxn;
    bool ok;
    bool judge_equal(){
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                if(tmp[i][j] != tmp[0][0]) return false;
            }
        }
        return true;
    }
    bool judge(int x, int y){
        return x >= 0 && x < 4 && y >= 0 && y < 4;
    }
    void flip(int x, int y){
        for(int i = 0; i < 5; ++i){
            int tmpx = x + dr[i];
            int tmpy = y + dc[i];
            if(judge(tmpx, tmpy)){
                if(tmp[tmpx][tmpy] == 'b') tmp[tmpx][tmpy] = 'w';
                else tmp[tmpx][tmpy] = 'b';
            }
        }
    }
    void dfs(int x, int y, int cur){
        if(cur == maxn){
            ok = judge_equal();
            return;
        }
        if(ok || x == 4) return;
        flip(x, y);
        if(y < 3) dfs(x, y + 1, cur + 1);
        else dfs(x + 1, 0, cur + 1);
        flip(x, y);
        if(y < 3) dfs(x, y + 1, cur);
        else dfs(x + 1, 0, cur);
        return;
    }
    int main(){
        for(int i = 0; i < 4; ++i){
            scanf("%s", pic[i]);
        }
        ok = false;
        for(maxn = 0; maxn <= 16; ++maxn){
            memcpy(tmp, pic, sizeof pic);
            dfs(0, 0, 0);
            if(ok) break;
        }
        if(!ok) printf("Impossible\n");
        else printf("%d\n", maxn);
        return 0;
    }
    

      

  • 相关阅读:
    tomcat下的web.xml和项目中的web.xml
    tomcat日志详解
    tomcat日志及logback相关日志框架
    关于程序中使用servlet-api.jar和jsp-api.jar与服务器lib包jar包冲突的问题
    windows server数据库备份
    idea为tomcat设置虚拟地址
    GitHub
    MarkDown的用法
    SVN添加用户权限
    第一篇小记
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6413556.html
Copyright © 2011-2022 走看看