【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 ... ...
随机推荐
- IE兼容rgba()透明度
一般浏览器的背景透明度可以直接设置 background:rgba(0,0,0,.5); -webkit-background:rgba(0,0,0,.5); -o-background:rgba(0 ...
- github入门之创建仓库--3
1.登陆到github,点击加号中的New repository 2.设置仓库信息 *注: ------Description:添加仓库说明,不是必填项 ------Public.Private:选择 ...
- 关于sqlserver帐号被禁用问题
若发现sqlsrver所有帐号不小心被禁用了,这个时候怎么办?用重装吗?不用,仔细看小白是怎么一步一步解开这个谜题的.首先需要Windows帐号设置里重新添加一个新帐号.并将其添加到管理员组里面,然后 ...
- 图像处理框架 Core Image 介绍
这篇文章会为初学者介绍一下 Core Image,一个 OS X 和 iOS 的图像处理框架. 如果你想跟着本文中的代码学习,你可以在 GitHub 上下载示例工程.示例工程是一个 iOS 应用程序, ...
- history 路由且带二级目录的Apache配置
有多个项目目录的时候 由于项目不知一个,所以不得不为每一个项目建一个专有的文件夹,这就导致了在配置nginx的时候会出现二级目录 - step1: 修改 vue.config.js 添加配置 ...
- 用vscode开发vue应用[转]
https://segmentfault.com/a/1190000019055976 现在用VSCode开发Vue.js应用几乎已经是前端的标配了,但很多时候我们看到的代码混乱不堪,作为一个前端工程 ...
- Python 输入输出 数据类型 变量
python输入输出 数据类型 变量 输入输出 print()在Python3中是函数 >>>print('hello world') #print注意print前面不要有任何空格 ...
- 引入了junit为什么还是用不了@Test注解
pom文件明明引入了unit,为什么还是用不了@Test? 配置如下: <dependency> <groupId>junit</groupId> <arti ...
- CPP-基础:非静态成员函数后面加const,以及mutable修饰成员变量
非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...
- Hibernate 多表查询 - Criteria添加子字段查询条件 - 出错问题解决
Criteria 查询条件如果是子对象中的非主键字段会报 could not resolve property private Criteria getCriteria(Favorite favori ...