1. 程式人生 > >【POJ 1084】 Square Destroyer

【POJ 1084】 Square Destroyer

local exception else problem errno vector poj print its

【題目鏈接】

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

【POJ 1084】 Square Destroyer