- Mayan游戏
- 2016-10-11 21:45:28 @
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node{
int x,y,fa;
}path[10];
int n,map[5][7]={0},book[5][7]={0};
bool finish(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<7;j++){
if(map[i][j]!=0) return false;
}
}
return true;
}
void swap(int *a,int *b){
int *c;
c=a;a=b;b=c;
}
void fall(int i){
int j;
for(j=0;j<7;j++){
if(map[i][j]==0){
int k=j;
while(k<7){
if(map[i][k]!=0){
swap(map[i][k],map[i][j]);
break;
}
k++;
}
}
}
}
bool check(){
int i,j,flag=0;
for(i=0;i<5;i++){
for(j=0;j<5&&map[i][j];j++){
if(map[i][j]==map[i][j+1]&&map[i][j+1]==map[i][j+2]){
book[i][j]=1;book[i][j+1]=1;book[i][j+2]=1; flag=1;
}
}
}
for(j=0;j<7;j++){
for(i=0;i<3;i++){
if(!map[i][j]) continue;
if(map[i][j]==map[i+1][j]&&map[i+1][j]==map[i+2][j]){
book[i][j]=1;book[i+1][j]=1;book[i+2][j]=1;flag=1;
}
}
}
return flag;
}
void clear(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<7;j++){
if(book[i][j]){
map[i][j]=0;book[i][j]=0;
}
}
}
for(i=0;i<5;i++) fall(i);
}
void dfs(int step){
int graph[5][7];
memcpy(graph,map,sizeof(map));
int i,j,k;
if(finish()){
for(i=1;i<=n;i++){
cout<<path[i].x<<" "<<path[i].y<<" "<<path[i].fa<<endl;
}
exit(0);
}
if(step>n) return;
for(i=0;i<5;i++){
for(j=0;j<7&&map[i][j];j++){
for(k=1;k>=-1;k-=2){
int newx=i+k;
if((newx>=5)||(newx<0)) continue;
if(k==-1&&map[newx][j]!=0) continue;
if(map[i][j]==map[newx][j]) continue;
swap(map[newx][j],map[i][j]);
if(map[i][j]==0){
fall(i);fall(newx);
}//问题在这里,可我不知道为什么,直接fall(i);fall(newx);就会过
while(check()){
clear();
}
path[step].x=i;path[step].y=j;path[step].fa=k;
dfs(step+1);
memcpy(map,graph,sizeof(graph));
}
}
}
}
int main(){
//freopen("mayan.txt","r",stdin);
int i,j,tmp;
cin>>n;
for(i=0;i<5;i++){
for(j=0;j<7;j++){
cin>>tmp;
if(tmp==0) break;
map[i][j]=tmp;
}
}
dfs(1);
cout<<"-1"<<endl;
return 0;
}
1 条评论
-
wuwangmao LV 8 @ 2016-10-12 14:37:13
是读入有问题!!!!!!!
- 1