码迷,mamicode.com
首页 > 其他好文 > 详细

【POJ 4007】 Flood-it!

时间:2018-07-08 23:15:50      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:std   clu   while   题目   tac   http   turn   fun   char   

【题目链接】

            http://poj.org/problem?id=4007

【算法】

          IDA*

【代码】

          

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};

int i,j,n,step;
int v[10][10],a[10][10];

inline bool valid(int x,int y)
{
        return x > 0 && x <= n && y > 0 && y <= n;
}
inline void dfs(int x,int y,int c)
{
        int i,tx,ty;
        v[x][y] = 1;
        for (i = 0; i < 4; i++)
        {
                tx = x + dx[i];
                ty = y + dy[i];
                if (valid(tx,ty) && v[tx][ty] != 1)
                {
                        if (a[tx][ty] == c) dfs(tx,ty,c);
                        else v[tx][ty] = 2;
                }
        }
}
inline int f()
{
        int i,j,s = 0;
        bool h[10];
        memset(h,0,sizeof(h));
        for (i = 1; i <= n; i++)
        {
                for (j = 1; j <= n; j++)
                {
                        if (!h[a[i][j]] && v[i][j] != 1)
                        {
                                h[a[i][j]] = true;
                                s++;
                        }
                }
        }
        return s;
}
inline bool fill(int c)
{
        int i,j,s = 0;
        for (i = 1; i <= n; i++)
        {
                for (j = 1; j <= n; j++)
                {
                        if (a[i][j] == c && v[i][j] == 2)
                        {
                                s++;
                                dfs(i,j,c);
                        }
                }
        }
        return s != 0;
}
inline bool IDDFS(int dep)
{
        int i,t;
        int tmp[10][10];
        t = f();
        if (dep + t > step) return false;
        if (!t) return true;
        for (i = 0; i <= 5; i++)
        {
                memcpy(tmp,v,sizeof(v));
                if (fill(i) && IDDFS(dep+1))
                        return true;
                memcpy(v,tmp,sizeof(v));
        }
        return false;
}
int main() 
{
        
        while (scanf("%d",&n) != EOF && n)
        {
                memset(v,0,sizeof(v));
                for (i = 1; i <= n; i++)
                {
                        for (j = 1; j <= n; j++)
                        {
                                scanf("%d",&a[i][j]);    
                        }    
                }        
                dfs(1,1,a[1][1]);
                for (i = 0; i <= n * n; i++)
                {
                        step = i;
                        if (IDDFS(0))
                                break;
                }
                printf("%d\n",step);
        }
        
        return 0;
    
}

 

【POJ 4007】 Flood-it!

标签:std   clu   while   题目   tac   http   turn   fun   char   

原文地址:https://www.cnblogs.com/evenbao/p/9281561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!