zoukankan      html  css  js  c++  java
  • 【POJ 4007】 Flood-it!

    【题目链接】

                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
    ",step);
            }
            
            return 0;
        
    }
  • 相关阅读:
    函数指针与变长参数列表
    Finding intersection and union of two sets.
    依赖注入
    可达性分析算法
    java 虚拟机栈
    Java 堆
    java虚拟机>>程序计数器
    Java方法区(Method Area)
    Java 运行时常量池
    java引用
  • 原文地址:https://www.cnblogs.com/evenbao/p/9281561.html
Copyright © 2011-2022 走看看