POJ - 1753 Flip Game (IDA*)

Posted SomnusMistletoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于POJ - 1753 Flip Game (IDA*)的主要内容,如果未能解决你的问题,请参考以下文章

POJ_1753——Flip Game(枚举)

POJ 1753 Flip Game

POJ1753 Flip Game(bfs枚举)

Flip Game / POJ 1753

POJ1753Flip Game(DFS + 枚举)

POJ 1753 Flip Game(状态压缩+BFS)