NOIP2011玛雅游戏
闲的没事干,出来写一下早两天刷的一道搜索题NOIP2011玛雅游戏,其实这道题还是比较水的,虽然看起来可能有点复杂。
方法很简单粗暴,直接根据规则模拟就行。
话不多说直接上代码(关键操作在注释中有提到):
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define ll long long
#define il inline
#define RG register
#define inf (1<<30)
using namespace std;
il int gi(){
RG int x=0,q=1; RG char ch=getchar();
while( ( ch<'0' || ch>'9' ) && ch!='-' ) ch=getchar();
if( ch=='-' ) q=-1,ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
return q*x;
}
int n,a,b;
int map[ 6 ][ 8 ],step[ 10 ][ 3 ];
bool broken[ 6 ][ 8 ],appear[ 11 ];
int color,sum[ 11 ];
il bool check(){
memset( sum,0,sizeof( sum ) );
for( RG int i=1;i<=5;i++ )
for( RG int j=1;j<=7;j++ )
sum[ map[ i ][ j ] ]++;
for( RG int i=1;i<=color;i++ )
if( sum[i] && sum[i]<=2 )
return false;
//若块数小于3则无法消去
return true;
}
il int count(){
memset( broken,0,sizeof( broken ) );
RG int left,right,top,bottom;
RG int ans=0,drop=0;
for( RG int i=1;i<=5;i++ )
for( RG int j=1;j<=7;j++ )
if( map[ i ][ j ] )
if( j==1 || ( j>1 && map[ i ][ j-1 ] ) ){//找到一个没有悬空的块
left=right=i;
top=bottom=j;
while(left>1 && map[left-1][j]==map[i][j])
left--;
while(right<4 && map[right+1][j]==map[i][j])
right++;
while(top<7 && map[ i ][top+1]==map[i][j])
top++;
while(bottom>1 && map[i][bottom-1]==map[i][j])
bottom--;
//统计可以消除的块
if( right-left+1>=3 )
for( RG int k=left;k<=right;k++ )
broken[ k ][ j ]=1;
if( top-bottom+1>=3 )
for( RG int k=top;k>=bottom;k-- )
broken[ i ][ k ]=1;
}
for( RG int i=1;i<=5;i++ )
for( RG int j=1;j<=7;j++ )
if( broken[ i ][ j ] )
ans++,map[ i ][ j ]=0;
RG int i,j,down,up;
for( i=1;i<=5;i++ ){
j=1;
while( j<=7&&map[ i ][ j ] ) j++;
down=j; //找到该列第一个空块
while( j<=7&&!map[ i ][ j ] )j++;
up=j-1;////找到该列最后一个空块
if( up==7 ) continue;//若道顶端都没有悬空的块就跳过该列
RG int k=0;
for( j=down;j<=7;j++ ){//下落操作
k++;
if( map[i][j] || !map[i][up+k] ) break;
map[i][j]=map[i][up+k];
map[i][up+k]=0;
drop=1;
}
}
//**下落的块还会造成其他块消除,所以再次统计**
if( drop ) ans+=count();
return ans;
}
il void dfs( int ste,int cnt ){//分别表示已走步数和剩余块数
if( !check() )return ;
if( ste>n ){
if( !cnt ){
for( RG int i=1;i<=n;i++ )
printf("%d %d %d \n",step[i][0]-1,step[i][1]-1,step[i][2]);
exit(0);
}
return ;
}
int mapp[ 6 ][ 8 ],if_change,x;
for( RG int i=1;i<=5;i++ )
for( RG int j=1;j<=7;j++ )
mapp[i][j]=map[i][j];
for( RG int i=1;i<=5;i++ )
for( RG int j=1;j<=7;j++ ){
if( map[i][j] ){//找到可以移动的块
if_change=1;
if( i<5 && map[i][j]!=map[i+1][j] ){
//考虑到结果的优先原则,先进行右移操作
swap( map[i][j],map[i+1][j] );
step[ste][0]=i;
step[ste][1]=j;
step[ste][2]=1;
x=cnt-count();
dfs( ste+1,x );
if_change=0;
}
if( !if_change ){//回溯
for( RG int g=1;g<=5;g++ )
for( RG int s=1;s<=7;s++ )
map[g][s]=mapp[g][s];
if_change=1;
}
if( i>1 && !map[i-1][j] ){
//只有左边没有块的时候才交换,否则就会和前面往右
//移的步骤重复
swap( map[i][j],map[i-1][j] );
step[ste][0]=i;
step[ste][1]=j;
step[ste][2]=-1;
x=cnt-count();
dfs( ste+1,x );
if_change=0;
}
if( !if_change )
for( RG int g=1;g<=5;g++ )
for( RG int s=1;s<=7;s++ )
map[ g ][ s ]=mapp[ g ][ s ];
}
}
}
il void init(){
n=gi();
for( RG int i=1;i<=5;i++ ){
RG int j=0;
a=gi();
while( a ){
map[ i ][ ++j ]=a;
if( !appear[ a ] ) appear[ a ]=1,color++;
a=gi();
b++;
}
}
}
int main(){init();dfs( 1,b );printf( "-1\n" );return 0;}
NOIP2011玛雅游戏的更多相关文章
- [NOIP2011]玛雅游戏
闲的没事干,出来写一下早两天刷的一道搜索题NOIP2011玛雅游戏,其实这道题还是比较水的,虽然看起来可能有点复杂. 方法很简单粗暴,直接根据规则模拟就行. 话不多说直接上代码(关键操作在注释中有提到 ...
- noip2011 玛雅游戏 大模拟
深搜+模拟 需要剪枝:同一移动向右移了就不需要向左移了 #include<cstdio> #include<cstring> #include<iostream> ...
- [COGS 622] [NOIP2011] 玛雅游戏 模拟
整个模拟的关键除了打出来就是一个剪枝:对于两个左右相邻的块你不用再走←,因为走→是等效的 #include<cstdio> #include<cstring> #include ...
- Luogu 1312 【NOIP2011】玛雅游戏 (搜索)
Luogu 1312 [NOIP2011]玛雅游戏 (搜索) Description Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空 ...
- 玛雅游戏[NOIP2011]
题目描述 Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...
- [NOIP2011] mayan游戏(搜索+剪枝)
题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...
- [NOIP2011]Mayan游戏 题解
题目大意: 有一个5*7的方格,上面有几种颜色的方块,如果在一横行或者竖列上有连续三个或者三个以上相同颜色的方块,则它们将立即被消除,方块消除之后,消除位置之上的方块将掉落.每步移动可以且仅可以沿横向 ...
- NOIP2011 Mayan游戏
3 Mayan游戏 题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上 ...
- Noip2011 Mayan游戏 搜索 + 模拟 + 剪枝
写了一下午,终于AC了. 由于n<=5, 所以不需要太多的剪枝和技巧也能过.可以将操作后的消方块和下落和剪枝函数写到一个结构体中,这样会减少调试难度,更加简洁. 可以采用如下剪枝: 1. 如果当 ...
随机推荐
- PHP 防恶意刷新实现代码
本质还是采用session方式进行时间比较,在单位时间内允许访问或者访问次数,如果有使用反向代理的话,也可以采用nginx配置 <?phpsession_start(); $k=$_GET[' ...
- top的用法
top命令可以用来方便地观察当前系统中运行的进程的信息,并可以在运行过程中执行改变进程的优先级.更改排序规则.导出状态信息等操作,非常方便. 1.主要选项 -d:后接秒数,状态更新的秒数,默认5秒-b ...
- 一个比较完善的httpWebRequest 封装,适合网络爬取及暴力破解
大家在模拟http请求的时候,对保持长连接及cookies,http头部信息等了解的不是那么深入.在各种网络请求过程中,发送N种问题. 可能问题如下: 1)登录成功后session保持 2)保证所有c ...
- jqueryui autocomplete的使用与angular配合的小坑
刚开始在做搜索联想功能时,使用了jquery.autocomplete.js插件,当并不理想,首先插件老旧,也只适合老版的jquery.其次在数组中只能联想到首字母一样的数据,比如[12,23,222 ...
- Strom序列化机制
Storm 中的 tuple可以包含任何类型的对象.由于Storm 是一个分布式系统,所以在不同的任务之间传递消息时Storm必须知道怎样序列化.反序列化消息对象. Storm 使用 Kryo库对对象 ...
- Xcode9新特性介绍-中文篇
背景: Xcode 9 新特性介绍: 1.官方原文介绍链接 2.Xcode9 be ta 2 官方下载链接 本文为官方介绍翻译而来,布局排版等都是按照官方布局来的. 与原文相比,排版上基本还是熟悉的配 ...
- select change事件给其它元素赋值,本select的value或tex
select change事件给其它元素赋值,本select的value或textonchange='$("#areaname").val($("option:selec ...
- [leetcode-513-Find Bottom Left Tree Value]
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- 【Android Developers Training】 17. 停止和重启一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- git的使用[转]
本节内容 github介绍 安装 仓库创建& 提交代码 代码回滚 工作区和暂存区 撤销修改 删除操作 远程仓库 分支管理 多人协作 github使用 忽略特殊文件.gitignore 为什么要 ...