UVa 1603 破坏正方形
https://vjudge.net/problem/UVA-1603
题意:有一个火柴棍组成的正方形网格,计算至少要拿走多少根火柴才能破坏所有正方形。
思路:从边长为1的正方形开始遍历,将正方形的边长和它的实际火柴数保存起来。之后dfs搜索。
#include<iostream>
#include<cstring>
using namespace std; const int maxn = ; int n, m,best,s;
int map[maxn],size[maxn],fullsize[maxn];
int contins[maxn][maxn]; int row_match(int x, int y) //行
{
return ( * n + )*x + y;
} int col_match(int x, int y) //列
{
return ( * n + )*x + n + y;
} int edges() //计算火柴根数
{
return *n*(n + );
} void init()
{
int ans;
cin >> n >> m;
//初始化网格
for (int i = ; i < edges(); i++)
map[i] = ;
while (m--)
{
cin >> ans;
map[ans - ] = ;
}
s = ; //s用来为正方形编号
memset(contins, , sizeof(contins));
for (int i = ; i <= n;i++) //正方形的边长,从1开始到n
for (int x = ; x <= n - i;x++) //正方形左上角的x坐标
for (int y = ; y <= n - i; y++) //正方形左上角的y坐标
{
size[s] = ;
fullsize[s] = * i; //第s个正方形的边长
//cout << fullsize[s] << "*" << endl;
for (int j = ; j < i; j++)
{
int a = row_match(x, y + j);
int b = row_match(x + i, y + j);
int c = col_match(x + j, y);
int d = col_match(x + j, y + i);
contins[s][a] = ; //储存正方形的边
contins[s][b] = ;
contins[s][c] = ;
contins[s][d] = ;
size[s] += map[a] + map[b] + map[c] + map[d];
}
s++;
}
} int find_sqware()
{
for (int i = ; i < s;i++)
if (fullsize[i] == size[i]) return i; //找到未被破坏的正方形
return -;
} void dfs(int dep)
{
if (dep >= best) return;
int k = find_sqware();
if (k == -) //每个正方形都已经被破坏
{
best = dep;
return;
}
for (int i = ; i < edges(); i++)
{
if (contins[k][i])
{
for (int j = ; j < s;j++)
if (contins[j][i]) size[j]--; //破坏,边的数量减少
dfs(dep + );
for (int j = ; j < s;j++) //回溯
if (contins[j][i]) size[j]++;
}
}
}
int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int t;
cin >> t;
while (t--)
{
init();
best = n*n;
dfs();
cout << best << endl;
}
return ;
}
UVa 1603 破坏正方形的更多相关文章
- UVA - 1603 Square Destroyer (DLX可重复覆盖+IDA*)
题目链接 给你一个n*n的由火柴组成的正方形网格,从中预先拿掉一些火柴,问至少还需要拿掉多少火柴才能破坏掉所有的正方形. 看到这道题,我第一反应就是——把每根火柴和它能破坏掉的正方形连边,不就是个裸的 ...
- 破坏正方形UVA1603
题目大意 有一个由火柴棍组成的边长为n的正方形网格,每条边有n根火柴,共2n(n+1)根火柴.从上至下,从左到右给每个火柴编号,现在拿走一些火柴,问在剩下的后拆当中ongoing,至少还要拿走多少根火 ...
- UVA 11520 填充正方形
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1603 Square Destroyer
题意: 给定一个火柴棒拼成的方格阵,然后去掉一些火柴棒,问至少再去掉几根火柴棒能够让图中一个正方形都没有. 思路: 1. 由于题目中给定了 n 的范围,2 * n * (n + 1) <= 60 ...
- 7-15 Square Destroyer 破坏正方形 uva1603
先是处理所有的正方形 从边长为1开始 将其边存好 满边存好 然后不断扫描正方形 并且进行拆除 直到拆完或者 步数小于等于9(启发方程 因为n小于等于5 九次足以将所有的拆完) 代码实施有很多细 ...
- IDA*
模拟退火 基本思路(Main Thoughts): IDA*是一种优秀的搜索法,在一般的实际问题中,它比普通的搜索更快. 通过迭代加深和估价函数剪枝来搜索. 通常处理没有层数上界或上界很多大的搜索. ...
- UVA - 12113 Overlapping Squares(重叠的正方形)
题意:给定一个4*4的棋盘和棋盘上所呈现出来的纸张边缘,问用不超过6张2*2的纸能否摆出指定的形状. 分析:2*2的纸在4*4的棋盘上总共有9种放置位置,枚举所有的放置位置即可.枚举情况总共种. #p ...
- UVA - 1643 Angle and Squares (角度和正方形)(几何)
题意:第一象限里有一个角,把n(n <= 10)个给定边长的正方形摆在这个角里(角度任意),使得阴影部分面积尽量大. 分析:当n个正方形的对角线在一条直线上时,阴影部分面积最大. 1.通过给定的 ...
- UVa 11520 Fill the Square 填充正方形
在一个 n * n 网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如果有多重填法,则要求按照从上到下,从左到右的顺序把所有格子连 ...
随机推荐
- Git Gui 查看分支历史的时候中文显示乱码
如图所示 解决方案1 在Git Gui工具栏上选择-编辑-选项: 选择:Default File Contents Encoding, change为UTF-8 成功: 解决方案2 C:\Users ...
- Ubuntu16.04安裝最新Nvidia驱动
在安装完Ubuntu之后,可能通过自带驱动无法更新,一直处于无法下载状态,那么就需要通过到Nvidia官网下载驱动,手动安装了 方法/步骤 通过度娘,打开NVIDIA官网,然后在下载驱动那里找到自己的 ...
- pycharm进行调试[转载]
转自:https://blog.csdn.net/william_hehe/article/details/80898031 1.首先设置断点. 2.Step into(F7):进入 若函数A内存在子 ...
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- test examples/test scripts
//https://www.getpostman.com/docs/v6/postman/scripts/test_examples //Setting an environment variable ...
- [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- js 参数声明用var和不用var的区别
var 声明的变量,作用域是当前 function 没有声明的变量,直接赋值的话, 会自动创建变量 ,但作用域是全局的. //----------------- function doSth() { ...
- Vue.js学习笔记——表单控件实践
最近项目中使用了vue替代繁琐的jquery处理dom的数据更新,个人非常喜欢,所以就上官网小小地实践了一把. 以下为表单控件的实践,代码敬上,直接新建html文件,粘贴复制即可看到效果~ <! ...
- mysql主从延迟(摘自http://www.linuxidc.com/Linux/2012-02/53995.htm)
http://www.linuxidc.com/Linux/2012-02/53995.htm