【Luogu】P1312Mayan游戏(暴搜)
由于是暴搜题,所以这篇博客只讲怎么优化剪枝,以及一些细节。
模拟消除思路:因为消除可以拆分成小的横条或竖条,而这些条的长度至少为三,所以一块可消除的区域至少会有一个中心点。这里的中心点可以不在正中间,只需要不是条上的第一个或者最后一个。
于是枚举中间点,搜索它为中心最多向四个方向能扩展多远。如果搜索出来的横向满足长度要求,或竖向满足长度要求,就给他们打上一个标记。
注意,这里只是打上标记,不能直接清零,很可能另一个方块的结算还得用到这个方块。
等到枚举所有的中间点并给所有可消除的方块打上标记之后,可以把所有标记上的方块清空。然后检查有没有地方可以落下去。
注意所有方块落下去之后还有可能接着消消乐,所以在落完之后还要再循环回去检查有没有可消除的方块。这里使用一个递归的代码来实现。
int count(int x){
bool vis[][],flag=;
int cnt=;
memset(vis,,sizeof(vis));
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j){
int col=Map[x][i][j];
if(!col) continue;
int u=i,d=i,l=j,r=j;
while(u>&&Map[x][u-][j]==col) u--;
while(d<n&&Map[x][d+][j]==col) d++;
while(l>&&Map[x][i][l-]==col) l--;
while(r<m&&Map[x][i][r+]==col) r++;
if(d-u>=)
for(int k=u;k<=d;++k) vis[k][j]=;
if(r-l>=)
for(int k=l;k<=r;++k) vis[i][k]=;
}
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j)
if(vis[i][j]){
Map[x][i][j]=; cnt++;
}
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j)
if(!Map[x][i][j]){
int s=i;
while(s<n&&!Map[x][s][j]) s++;
if(!Map[x][s][j]) continue;
flag=;
Map[x][i][j]=Map[x][s][j];
Map[x][s][j]=;
}
if(flag) cnt+=count(x);
return cnt;
}
注意最后倒数第三行。flag表示的是有没有方块落下,因为有落下方块就有新一轮消除的可能性,所以可以递归这个函数,直到没有任何方块落下为止。此时递归终止,开始回溯计算答案。
再说说剪枝的几个小技巧。
1、优先考虑坐标字典序小的向右移动,这样一旦搜到答案就是字典序最小的解。
2、只有当左边没有方块的时候才向左移动,否则右面方块向左移动等价于左面方块向右移动,而这个状态已经搜过了。
3、不交换两个颜色相同的方块。这个没什么好说的。
最后给出代码。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cstring>
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int num;
int n=,m=;
int Max;
int Map[][][];
int posx[],posy[],move[]; inline void copy(int x){
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j) Map[x+][i][j]=Map[x][i][j];
} int count(int x){
bool vis[][],flag=;
int cnt=;
memset(vis,,sizeof(vis));
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j){
int col=Map[x][i][j];
if(!col) continue;
int u=i,d=i,l=j,r=j;
while(u>&&Map[x][u-][j]==col) u--;
while(d<n&&Map[x][d+][j]==col) d++;
while(l>&&Map[x][i][l-]==col) l--;
while(r<m&&Map[x][i][r+]==col) r++;
if(d-u>=)
for(int k=u;k<=d;++k) vis[k][j]=;
if(r-l>=)
for(int k=l;k<=r;++k) vis[i][k]=;
}
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j)
if(vis[i][j]){
Map[x][i][j]=; cnt++;
}
for(register int i=;i<=n;++i)
for(register int j=;j<=m;++j)
if(!Map[x][i][j]){
int s=i;
while(s<n&&!Map[x][s][j]) s++;
if(!Map[x][s][j]) continue;
flag=;
Map[x][i][j]=Map[x][s][j];
Map[x][s][j]=;
}
if(flag) cnt+=count(x);
return cnt;
} void dfs(int use,int deep){
if(use!=&&deep==Max) return;
if(use==){
if(deep==Max){
for(int i=;i<=Max;++i) printf("%d %d %d\n",posy[i]-,posx[i]-,move[i]);
exit();
}
return;
}
copy(deep);
for(register int j=;j<=m;++j)
for(register int i=;i<=n;++i){
if(!Map[deep][i][j]) continue;
if(Map[deep][i][j]!=Map[deep][i][j+]&&j<m){
int a=Map[deep][i][j],b=Map[deep][i][j+];
Map[deep+][i][j]=b;Map[deep+][i][j+]=a;
posx[deep+]=i; posy[deep+]=j; move[deep+]=;
int q=count(deep+);
dfs(use-q,deep+);
copy(deep);
}
if(!Map[deep][i][j-]&&j>){
int a=Map[deep][i][j],b=Map[deep][i][j-];
Map[deep+][i][j]=b;Map[deep+][i][j-]=a;
posx[deep+]=i; posy[deep+]=j; move[deep+]=-;
int s=i;
while(s>&&Map[deep+][s-][j-]==){
Map[deep+][s--][j-]=;
Map[deep+][s][j-]=a;
}
int q=count(deep+);
dfs(use-q,deep+);
copy(deep);
}
}
} int main(){
Max=read();
int start=;
for(int i=;i<=m;++i)
for(int j=;;j++){
Map[][j][i]=read();
if(Map[][j][i]==) break;
start++;
}
dfs(start,);
printf("-1");
return ;
}
【Luogu】P1312Mayan游戏(暴搜)的更多相关文章
- 洛谷 1312 Mayan游戏——暴搜+剪枝
题目:https://www.luogu.org/problemnew/show/P1312 自己写了很久.又T又WA的. 发现对题理解有误.改完后应该只有T了,但还是T的. 自己写了许多剪枝,很鸡肋 ...
- NOIP 2011 Mayan游戏 大暴搜
题目链接:https://www.luogu.org/problemnew/show/P1312 我的第一篇题解!! 当然感谢ZAGER 的提示,他的链接https://www.cnblogs.com ...
- 【BZOJ-3033】太鼓达人 欧拉图 + 暴搜
3033: 太鼓达人 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 204 Solved: 154[Submit][Status][Discuss] ...
- c++20701除法(刘汝佳1、2册第七章,暴搜解决)
20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入正整数n,按从小到大的顺序输出所有 ...
- Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- Sicily1317-Sudoku-位运算暴搜
最终代码地址:https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1317.c 这题博主刷了1天,不是为了做出来,AC ...
- codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Weights Xenia has a set of weig ...
- Usaco 2.3 Zero Sums(回溯DFS)--暴搜
Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...
随机推荐
- 海康威视采集卡结合opencv使用(两种方法)-转
(注:第一种方法是我的原创 ^_^. 第二种方法是从网上学习的.) 第一种方法:利用 板卡的API: GetJpegImage 得到 Jpeg 格式的图像数据,然后用opencv里的一个函数进行解码 ...
- 在每天黄金时刻将数据库中数据获取包装成Excel表
过程: 1.由Timer对象实现安排指定的任务在指定的时间进行重复的固定的延迟操作 a.设定时间间隔24小时:PERIOD_DAY = 24 * 60 * 60 * 100; b.指定每天执行操作的时 ...
- 利用基于@AspectJ的AOP实现权限控制
一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...
- jquery 不支持$.browser
if (!$.browser) { $.browser = { mozilla : /firefox/.test(navigator.userAgent.toLowerCase()), webkit ...
- Codeforces Round #316 (Div. 2) D Tree Requests
官方题解是离线询问,dfs树形转线性,然后二分找区间. 还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案. 代码是学 ...
- URAL 2047 Maths (数学)
对于一个数来说,它的除数是确定的,那么它的前驱也是确定的,而起点只能是1或2,所以只要类似筛法先预处理出每个数的除数个数 ,然后递推出每个数往前的延伸的链长,更新最大长度,记录对应数字.找到maxn以 ...
- 机器学习之 PCA (二)
参考 http://www.cnblogs.com/frombeijingwithlove/p/5931872.html
- mysql 外键关联
mysql 外键关联 什么是外键:外键是一个特殊的索引,用于关联两个表,只能是指定内容. 如我将新建一个daka的表,然后将此表的class_id 与另外一个class的表的cid字段关联 class ...
- (七)VMware Harbor 问题:Get https://192.168.3.135:8088/v2/: http:server gave HTTP response to HTTPS client
(一)问题描述 登陆时,报错 docker Get https://192.168.3.135:8088/v2/: http:server gave HTTP response to HTTPS cl ...
- WPF中实现两个窗口之间传值
在使用WPF的时候,我们经常会用到窗体之间传值,下面示例主窗口传值到子窗口,子窗口传值到主窗口的方法. 一.主窗口向子窗口传值 主窗口向子窗口传值主要方法就是在子窗口建立一个接收主窗口值的变量,然后实 ...