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. 如果当 ...
随机推荐
- 编译MapWinGis
其实在github下载的MapWinGIS代码,在support文件夹下的build文件夹下的HowToBuild说明已经写的很清楚了, * How to build MapWinGIS.ocx** ...
- 在Windows上远程运行Linux程序
1.在Windows主机上安装X Server软件,如Cygwin带的XWin Server 2.在Windows主机上启动X服务器,并将Linux主机设为允许访问该Windows主机上的X服务器. ...
- 关系数据标准语言SQL之数据查询
数据查询是数据库的核心操作.SQL提供了SELECT语句进行数据查询,该语句具有灵活的使用方式和丰富的功能. 其一般格式为 select [all | distinct]<目标表达式>[, ...
- 腾讯AlloyTeam正式发布pasition - 制作酷炫Path过渡动画
pasition Pasition - Path Transition with little JS code, render to anywhere - 超小尺寸的Path过渡动画类库 Github ...
- oracle学习笔记(2)-基本术语
oracle基本术语 先上图. 相当粗糙的一个图,可能有些地方不够精细,大致结构基本是对的. 逻辑结构上从大到小的依次为文件(file)->表空间(tablespace)->段(segme ...
- JSONArray用法jquery循环list<Map>对象
controoler中 List<Map<String,Object>> resList =(List<Map<String,Object>>)resM ...
- docker 初识之二(简单发布ASP.NET Core 网站)
在发布ASP.NET Core网站以前,先介绍一下DaoCloud 一个免费的docker云容器服务平台.登陆官方网站,创建一台docker主机,这台主机有120分钟的使用时间,对于鄙人学习使用正好合 ...
- python 导入informixdb模块
最近碰到Linux平台使用python连接informixdb数据库的问题.整理如下: 1.安装 informixdb 下载InformixDB-2.5.tar.gz 解压之后,在README文档下看 ...
- sublime 设置字体
通过菜单Preferences/Settings - User,添加下面这行配置就可以修改字体: "font_face": "Courier New", &qu ...
- linux下安装apache最常见的报错解决
报错如下: Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, ...