*搜索 HOJ 1320 Square Destroyer

Square Destroyer

My Tags  (Edit)
Source : ACM ICPC Taejon(S.Korea) Regional Contest 2001
Time limit : 10 secMemory limit : 32 M

Submitted : 27, Accepted : 13

The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The lengths of all matchsticks are one. You can find many squares of different sizes in the grid. The size of a square is the length of its side. In the grid shown in the left figure, there are 9 squares of size one, 4 squares of size two, and 1 square of size three. 

Each matchstick of the complete grid is identified with a unique number which is assigned from left to right and from top to bottom as shown in the left figure. If you take some matchsticks out from the complete grid, then some squares in the grid will be destroyed, which results in an incomplete 3*3 grid. The right figure illustrates an incomplete 3*3 grid after removing three matchsticks numbered with 12, 17 and 23. This removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. Consequently, the incomplete grid does not have squares of size three, but still has 4 squares of size one and 1 square of size two. 

*搜索 HOJ 1320 Square Destroyer - 恶魔仁 - 恶魔仁 """

As input, you are given a (complete or incomplete) n*n grid made with no more than 2n(n+1) matchsticks for a natural number n<=5 . Your task is to compute the minimum number of matchsticks taken out to destroy all the squares existing in the input n*n grid.

Input 

The input consists of T test cases. The number of test cases (T) is given in the first line of the input. Each test case consists of two lines: The first line contains a natural number n , not greater than 5, which implies you are given a (complete or incomplete) n*n grid as input, and the second line begins with a nonnegative integer k , the number of matchsticks that are missing from the complete n*n grid, followed by k numbers specifying the matchsticks. Note that if k is equal to zero, then the input grid is a complete n*n grid; otherwise, the input grid is an incomplete n*n grid such that the specified k matchsticks are missing from the complete n*n grid. 


Output 

Print exactly one line for each test case. The line should contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid. 



Sample Input

2
2
0
3
3 12 17 23
Sample Output
3
3


题意:给出n*n的正方形方格,要你删除尽可能少的边使得这个图形里面没有正方形。

思路:我自己没想到什么好的办法,基本就是穷举。看了别人的题解后就明白了。首先,题目的n不超过5,那么这个图形里面最多只有60条边,那么我们用一个long long 来表示边的选取情况。这样我们就能用long long来表示一个正方形了,如果某条边在这个正方形里面,这个位置的数字就是1,否则为0,这样我们就能用位操作来判断了。我们用迭代加深的深搜,其中可以剪枝的地方就是,当有正方形的时候,我们把它所有的边都去掉,但是我们对这个操作的消耗只记为1,如果去掉所有正方形之后,总消耗加上目前的深度还是超过了当前迭代的深度的话,我们就可以之间返回了。还有就是在发现有一个正方形需要被去掉的时候,这个正方形一定会有一条边需要我们去掉,所以不要略过去了。避免递归到后面,又再一次回来去掉这个正方形。

思路:
#include <cstdio>
#include <cstdlib>
using namespace std;
#define LL long long


int n , k , T , sqr_cnt , edge_cnt , maxdepth;
inline LL place(int x) { return (LL) 1 << (x - 1); }
inline int hor(int r,int c) { return r * (2 * n + 1) + c + 1; } 
inline int ver(int r,int c) { return r * (2 * n + 1) + c + 1 + n; }

LL sqr[61] , one_sqr[10][10];

void init()
{
scanf("%d",&n);
edge_cnt = 2 * n * (n + 1);
sqr_cnt = 0;
for (int i = 0 ; i < n ; ++i)
{
for (int j = 0 ; j < n ; ++j)
{
sqr[sqr_cnt] = place(hor(i,j)) | place(hor(i+1,j)) | place(ver(i,j)) | place(ver(i,j+1));
one_sqr[i][j] = sqr[sqr_cnt++];
}
}
for (int sz = 2 ; sz <= n ; ++sz)
{
for (int i = 0 ; i + sz <= n ; ++i)
for (int j = 0 ; j + sz <= n ; ++j)
{
sqr[sqr_cnt] = 0;
for (int k = 0 ; k < sz ; ++k)
for (int l = 0 ; l < sz ; ++l)
sqr[sqr_cnt] ^= one_sqr[i+k][j+l];
++sqr_cnt;
}
}
}

bool dfs(LL s , int step)
{
LL t = s , sel = -1;
int min_dep = 0;
for (int i = 0 ; i < sqr_cnt ; ++i)
{
if ( (t & sqr[i]) == sqr[i])
{
++min_dep , t ^= sqr[i];
if (sel==-1) sel = sqr[i];
}
}
if (sel==-1) return true;
if (step + min_dep > maxdepth) return false;
for (int i = 1 ; i <= edge_cnt ; ++i)
{
if (sel & place(i))
if (dfs(s ^ place(i),step+1)) return true;
}
return false;
}

void solve()
{
LL s = ( (LL)1 << edge_cnt) - 1;
scanf("%d",&k);
while (k--)
{
int x;
scanf("%d",&x);
s ^= place(x);
}
for (maxdepth = 0 ; ; ++maxdepth)
if (dfs(s,0)) break;
printf("%d\n",maxdepth);
}

int main()
{
scanf("%d",&T);
while (T--)
{
init();
solve();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值