【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\n",step);
        }

        return 0;

}

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

时间: 2024-10-05 20:41:09

【POJ 1084】 Square Destroyer的相关文章

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an

【POJ 3254】 Corn Fields(状压DP)

[POJ 3254] Corn Fields(状压DP) Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10891   Accepted: 5705 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parce

【POJ 3440】 Coin Toss(概率公式)

[POJ 3440] Coin Toss(概率公式) Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3591   Accepted: 957 Description In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes ar

【POJ 1739】Tony&#39;s Tour

Tony's Tour Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3545   Accepted: 1653 Description A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked.

【POJ 1195】 Mobile phones (树状数组)

[POJ 1195] Mobile phones (树状数组) Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16761   Accepted: 7713 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The ar

【POJ 2195】 Going Home(KM算法求最小权匹配)

[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20303   Accepted: 10297 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit ste

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

2292: 【POJ Challenge 】永远挑战

2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][Status][Discuss] Description lqp18_31和1tthinking经常出题来虐ftiasch.有一天, lqp18_31搞了一个有向图,每条边的长度都是1. 他想让ftiasch求出点1到点 N 的最短路."水题啊.", ftiasch这么说道. 所以1tth