大模拟+搜索
写了一个下午 这么弱也是没谁了。
题目描述
Mayan puzzle是最近流行起来的一个游戏。游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上。游戏通关是指在规定的步数内消除所有的方块,消除方块的规则如下:
1 、每步移动可以且仅可以沿横向(即向左或向右)拖动某一方块一格:当拖动这一方块时,如果拖动后到达的位置(以下称目标位置)也有方块,那么这两个方块将交换位置(参见输入输出样例说明中的图6 到图7 );如果目标位置上没有方块,那么被拖动的方块将从原来的竖列中抽出,并从目标位置上掉落(直到不悬空,参见下面图1 和图2);
2 、任一时刻,如果在一横行或者竖列上有连续三个或者三个以上相同颜色的方块,则它们将立即被消除(参见图1 到图3)。
注意:
a) 如果同时有多组方块满足消除条件,几组方块会同时被消除(例如下面图4 ,三个颜色为1 的方块和三个颜色为 2 的方块会同时被消除,最后剩下一个颜色为 2 的方块)。
b) 当出现行和列都满足消除条件且行列共享某个方块时,行和列上满足消除条件的所有方块会被同时消除(例如下面图5 所示的情形,5 个方块会同时被消除)。
3 、方块消除之后,消除位置之上的方块将掉落,掉落后可能会引起新的方块消除。注意:掉落的过程中将不会有方块的消除。
上面图1 到图 3 给出了在棋盘上移动一块方块之后棋盘的变化。棋盘的左下角方块的坐标为(0, 0 ),将位于(3, 3 )的方块向左移动之后,游戏界面从图 1 变成图 2 所示的状态,此时在一竖列上有连续三块颜色为4 的方块,满足消除条件,消除连续3 块颜色为4 的方块后,上方的颜色为3 的方块掉落,形成图 3 所示的局面。
说明
【输入输出样例说明】
按箭头方向的顺序分别为图6 到图11
样例输入的游戏局面如上面第一个图片所示,依次移动的三步是:(2 ,1 )处的方格向右移动,(3,1 )处的方格向右移动,(3 ,0)处的方格向右移动,最后可以将棋盘上所有方块消除。
【数据范围】
对于30% 的数据,初始棋盘上的方块都在棋盘的最下面一行;
对于100%的数据,0 < n≤5 。
注意剪枝:
1.向左面换的话只有左面是空才换(要不然等价于左面那块向右,而且解不比那样优)
2.剩下不足三个剪掉
3.走了n+1步没搞完剪掉
4.同样色块没必要换
注意处理消除:
1.超过三个都能消掉
2.注意共用一块,最好解决方案是复制下来判断
3.注意消完之后要处理下落,还有可能能继续消,开一个flag判断。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
#include<cstdio> #include<iostream> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<iterator> #include<utility> using namespace std; int n=0,ansstep[10][4]={0}; short mapx[8][6]={0}; void processfall(short mapc[8][6]) { for (int j=1;j<=5;j++) for (int i=6;i>=1;i--) if (mapc[i][j] && !mapc[i+1][j]) { int temp=i; while(!mapc[temp+1][j] && temp<=6) { mapc[temp+1][j]=mapc[temp][j]; mapc[temp][j]=0; temp++; } } } bool processmeta(short mapc[8][6]) { short mapt[8][6]={0}; bool flag=false; memcpy(mapt,mapc,sizeof(mapt)); for (int j=1;j<=5;j++) for (int i=2;i<=6;i++) if (mapc[i][j] && mapc[i][j]==mapc[i+1][j] && mapc[i][j]==mapc[i-1][j]) { int posp=i+2; while (posp<=7 && mapc[posp][j]==mapc[i][j]) { mapt[posp][j]=0; posp++; } mapt[i][j]=mapt[i+1][j]=mapt[i-1][j]=0; if (!flag) flag=true; } for (int i=1;i<=7;i++) for (int j=2;j<=4;j++) if (mapc[i][j] && mapc[i][j]==mapc[i][j-1] && mapc[i][j]==mapc[i][j+1]) { int posp=j+2; while (posp<=5 && mapc[i][posp]==mapc[i][j]) { mapt[i][posp]=0; posp++; } mapt[i][j]=mapt[i][j-1]=mapt[i][j+1]=0; if (!flag) flag=true; } memcpy(mapc,mapt,sizeof(mapt)); if (flag) { processfall(mapc); processmeta(mapc); return true; } return false; } void dfs(short mapi[8][6],int step) { //pruning if (step>n+1) return; bool flag=false; register int cnt=0; //count for (int i=1;i<=7;i++) for (int j=1;j<=5;j++) if (mapi[i][j]) { flag=true; cnt++;} //pruning if (flag && step==n+1) return; if (cnt==1 || cnt==2) return; //getans if (!flag) { for (int i=1;i<step;i++) printf("%d %d %d\n",ansstep[i][2]-1,7-ansstep[i][1],ansstep[i][3]); exit(0); } short mapc[8][6]={0}; memcpy(mapc,mapi,sizeof(mapc)); for (int j=1;j<=5;j++) for (int i=7;i>=1;i--) if (mapi[i][j]) { if (j+1<=5 && mapi[i][j]!=mapi[i][j+1]) { swap(mapc[i][j],mapc[i][j+1]); processfall(mapc); processmeta(mapc); ansstep[step][1]=i; ansstep[step][2]=j; ansstep[step][3]=1; dfs(mapc,step+1); memcpy(mapc,mapi,sizeof(mapc)); } if (j-1>=1 && !mapc[i][j-1]) { swap(mapc[i][j],mapc[i][j-1]); processfall(mapc); processmeta(mapc); ansstep[step][1]=i; ansstep[step][2]=j; ansstep[step][3]=-1; dfs(mapc,step+1); memcpy(mapc,mapi,sizeof(mapc)); } } } int main() { scanf("%d",&n); for (int j=1;j<=5;j++) { int temp=1,pos=7; while (temp) { scanf("%d",&temp); mapx[pos][j]=temp; pos--; } } dfs(mapx,1); printf("-1\n"); return 0; } |