*搜索 HOJ 1320 Square Destroyer
来源:互联网 发布:radan软件 编辑:程序博客网 时间:2024/05/16 07:54
Square Destroyer
My Tags (Edit)Source : ACM ICPC Taejon(S.Korea) Regional Contest 2001Time limit : 10 secMemory limit : 32 MSubmitted : 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.
"""
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
22033 12 17 23
Sample Output33
题意:给出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
- 搜索 HOJ 1320 Square Destroyer
- *搜索 HOJ 1320 Square Destroyer
- ZOJ 1031 Square Destroyer
- ZOJ1031 Square Destroyer
- 1603 - Square Destroyer
- UVa1603 - Square Destroyer
- 1603 - Square Destroyer
- POJ 1084 Square Destroyer
- POJ 1084 Square Destroyer
- Square Destroyer UVA
- UVA 1603 Square Destroyer
- POJ1084-Square Destroyer
- POJ 1084 Square Destroyer 笔记
- 搜索 HOJ 1281 Lagrange's Four-Square Theorem
- 搜索 HOJ 1281 Lagrange\'s Four-Square Theorem
- JOJ 1052: Square Destroyer解题报告
- 【IDA*+位运算】PKU-1084-Square Destroyer
- [DLX重复覆盖] poj 1084 Square Destroyer
- *数论 HOJ 3110 Remoteland
- 博弈论 HOJ 1122 Number Game
- 字符串(最小表示法) HOJ 1223 Hidden Password
- 搜索 HOJ 1266 Phone Home
- 搜索 HOJ 1281 Lagrange\'s Four-Square Theorem
- *搜索 HOJ 1320 Square Destroyer
- 概率dp ZOJ 3640 Help Me Escape
- 并查集 HOJ 2064 Journey to Tibet
- (简单)树形dp HOJ 2514 Perfect Service
- *(中等)动态规划 HOJ 2508 Pitcher Rotation
- linux rm 命令详解
- (简单) 树形dp HOJ 2500 Party at Hali-Bula
- (中等) 状态压缩dp HOJ 2480 The best travel design
- (中等) 状态压缩dp HOJ 2193 Time to Graduate
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
雄县游玩
雄县刻章
雄县医院
雄县气泡膜
天津到雄县
雄县水晶棺
雄县中医院
中国雄县
雄县瓷砖
雄县硅芯管
雄县属于哪
雄县日租房
雄县 塑料袋
雄县中学
雄县塑胶厂
雄县缠绕膜
雄县租房子
雄县教育信息网
雄县外卖电话号码
雄县今天招工信息
雄县阳光信息招聘网
雄县人才招聘网
雄县阳光信息网招聘
雄县房产信息
雄县租房信息网
雄县阳光信息网
雄县邮编是多少
保定雄县邮编
雄县隐蔽保健
保定雄县房价
雄县出租车电话
雄县塑料袋生产厂家
河北保定雄县
雄县缠绕膜厂家
雄县编织袋厂
雄县色母粒厂家
雄县旺泰塑胶制品厂
雄县永强塑料制品厂
雄县永塑塑胶制品厂
河北省雄县塑料袋厂
雄县华兴塑料制品厂
程序博客网,程序员的互联网技术博客家园。csdn论坛精品 msdn技术资料都在这里
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.
"""
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
22033 12 17 23Sample Output
33
- 搜索 HOJ 1320 Square Destroyer
- *搜索 HOJ 1320 Square Destroyer
- ZOJ 1031 Square Destroyer
- ZOJ1031 Square Destroyer
- 1603 - Square Destroyer
- UVa1603 - Square Destroyer
- 1603 - Square Destroyer
- POJ 1084 Square Destroyer
- POJ 1084 Square Destroyer
- Square Destroyer UVA
- UVA 1603 Square Destroyer
- POJ1084-Square Destroyer
- POJ 1084 Square Destroyer 笔记
- 搜索 HOJ 1281 Lagrange's Four-Square Theorem
- 搜索 HOJ 1281 Lagrange\'s Four-Square Theorem
- JOJ 1052: Square Destroyer解题报告
- 【IDA*+位运算】PKU-1084-Square Destroyer
- [DLX重复覆盖] poj 1084 Square Destroyer
- *数论 HOJ 3110 Remoteland
- 博弈论 HOJ 1122 Number Game
- 字符串(最小表示法) HOJ 1223 Hidden Password
- 搜索 HOJ 1266 Phone Home
- 搜索 HOJ 1281 Lagrange\'s Four-Square Theorem
- *搜索 HOJ 1320 Square Destroyer
- 概率dp ZOJ 3640 Help Me Escape
- 并查集 HOJ 2064 Journey to Tibet
- (简单)树形dp HOJ 2514 Perfect Service
- *(中等)动态规划 HOJ 2508 Pitcher Rotation
- linux rm 命令详解
- (简单) 树形dp HOJ 2500 Party at Hali-Bula
- (中等) 状态压缩dp HOJ 2480 The best travel design
- (中等) 状态压缩dp HOJ 2193 Time to Graduate
程序博客网,程序员的互联网技术博客家园。csdn论坛精品 msdn技术资料都在这里