POJ 1084 Square Destroyer

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1084 Square Destroyer相关的知识,希望对你有一定的参考价值。

【题目链接】

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

【算法】

           迭代加深

【代码】

          

#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;

int i,n,T,step,k,x;
bool dest[100];

inline bool is_square(int x,int y,int len)
{
        int i,l,r;
        bool ret = true;
        l = (x - 1) * (2 * n + 1) + y;
        r = l + len - 1;
        for (i = l; i <= r; i++) ret &= (dest[i] ^ 1);
        l = (x + len - 1) * (2 * n + 1) + y;
        r = l + len - 1;
        for (i = l; i <= r; i++) ret &= (dest[i] ^ 1);
        l = n * x + (x - 1) * (n + 1) + y; 
        r = n * (x + len - 1) + (x + len - 2) * (n + 1) + y; 
        for (i = l; i <= r; i += 2 * n + 1) ret &= (dest[i] ^ 1);
        l = n * x + (x - 1) * (n + 1) + y + len;
        r = n * (x + len - 1) + (x + len - 2) * (n + 1) + y + len;
        for (i = l; i <= r; i += 2 * n + 1) ret &= (dest[i] ^ 1);
        return ret;
}
inline bool check()
{
        int i,j,k;
        for (k = 1; k <= n; k++)
        {
                for (i = 1; i <= n - k + 1; i++)
                {
                        for (j = 1; j <= n - k + 1; j++)
                        {
                                if (is_square(i,j,k))
                                        return false;
                        }
                }
        }
        return true;
}
inline bool IDDFS(int dep)
{
        int i,j,k,x,y,len,l,r;
        if (dep > step)
        {
                if (check())
                        return true;        
                else return false;
        }
        x = y = len = 0;    
      for (k = 1; k <= n; k++)
        {
                for (i = 1; i <= n - k + 1; i++)
                {
                        for (j = 1; j <= n - k + 1; j++)
                        {
                                if (is_square(i,j,k))
                                {
                                        x = i;
                                         y = j;
                                        len = k;
                                        break;
                                }
                        }
                        if (x) break;
                }
                if (x) break;
        }
        l = (x - 1) * (2 * n + 1) + y;
        r = l + len - 1;
        for (i = l; i <= r; i++)
        {
                dest[i] = true;
                if (IDDFS(dep+1))
                        return true;    
                dest[i] = false;    
        }
        l = (x + len - 1) * (2 * n + 1) + y;
        r = l + len - 1;
        for (i = l; i <= r; i++)
        {
                dest[i] = true;
                if (IDDFS(dep+1))
                        return true;
                dest[i] = false;
        }
        l = n * x + (x - 1) * (n + 1) + y; 
        r = n * (x + len - 1) + (x + len - 2) * (n + 1) + y; 
        for (i = l; i <= r; i += 2 * n + 1)
        {
                dest[i] = true;
                if (IDDFS(dep+1))
                        return true;
                dest[i] = false;
        }
        l = n * x + (x - 1) * (n + 1) + y + len;
        r = n * (x + len - 1) + (x + len - 2) * (n + 1) + y + len;
        for (i = l; i <= r; i += 2 * n + 1)
        {
                dest[i] = true;
                if (IDDFS(dep+1))
                        return true;
                dest[i] = false;
        }
        return false;
}

int main() 
{
        
        scanf("%d",&T);
        while (T--)
        {
                scanf("%d",&n);
                for (i = 1; i <= 2 * n * (n + 1); i++) dest[i] = false;
                scanf("%d",&k);
                for (i = 1; i <= k; i++)
                {
                        scanf("%d",&x);
                        dest[x] = true;        
                }        
                for (i = 0; i <= 2 * n * (n + 1); i++)
                {
                        step = i;
                        if (IDDFS(1))
                                break;
                }
                printf("%d
",step);
        }
        
        return 0;
    
}

 

以上是关于POJ 1084 Square Destroyer的主要内容,如果未能解决你的问题,请参考以下文章

[DLX反复覆盖] poj 1084 Square Destroyer

POJ 1084 Square Destroyer舞蹈链重复覆盖

UVA 1603 Square Destroyer

例7-15 UVA-1603Square Destroyer

POJ3251 Big Square水题

POJ 2362 Square 深度优先