我们玩过的连连看游戏,通过选定两个图形相同的元素,判定其是否可在三次转弯内连接起来,若能,则消去,若不能,则不可消去,直至最后全部消除。
本算法中不包括关于死锁状态判定。
// 连连看.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
struct Point
{
int x;
int y;
};
int pushonelinespot(Point a,Point b,int c[6][6], queue &g) //将a点可直线到达的点压入队列g中
{
int i;
Point temp;
for(i=1;;i++) //向下检测
{ if((a.x+i)<=5&&c[a.x+i][a.y]==0)
{
temp.x=a.x+i;temp.y=a.y;
g.push(temp);}
else if(c[a.x+i][a.y]!=0&&(a.x+i)<=5)
{if((a.x+i)==b.x&&a.y==b.y) return 1;
else break;}
else break;
}
for(i=-1;;i--) //向上检测
{ if(c[a.x+i][a.y]==0&&(a.x+i)>=0)
{ temp.x=a.x+i;temp.y=a.y;
g.push(temp);}
else if(c[a.x+i][a.y]!=0&&(a.x+i)>=0)
{if((a.x+i)==b.x&&a.y==b.y) return 1;
else break;}
else break;
}
for(i=1;;i++) //向右检测
{ if(c[a.x][a.y+i]==0&&(a.y+i)<=5)
{ temp.x=a.x;temp.y=a.y+i;
g.push(temp);}
else if(c[a.x][a.y+i]!=0&&(a.y+i)<=5)
{if(a.x==b.x&&(a.y+i)==b.y) return 1;
else break;}
else break;
}
for(i=-1;;i--) //向左检测
{ if(c[a.x][a.y+i]==0&&(a.y+i)>=0)
{ temp.x=a.x;temp.y=a.y+i;
g.push(temp);}
else if(c[a.x][a.y+i]!=0&&(a.y+i)>=0)
{if(a.x==b.x&&(a.y+i)==b.y) return 1;
else break;}
else break;
}
return 0;
}
bool shortestline(Point a,Point b,int c[6][6])
{
if(c[a.x][a.y]==c[b.x][b.y])
{
Point temp;
queue X,Y,Z;
int i;
i=pushonelinespot(a,b,c,X);
if(i==1) return 1;
while(!X.empty())
{ temp=X.front();X.pop();
i=pushonelinespot(temp,b,c,Y);
if(i==1) return 1; } //第一次转弯
while(!Y.empty())
{ temp=Y.front();Y.pop();
i=pushonelinespot(temp,b,c,Z);
if(i==1) return 1;} //第二次转弯
cout<
return 0;
}
else if(c[a.x][a.y]==0||c[b.x][b.y]==0) {cout<
else { cout<
}
int main()
{
int v,i,j;
int c[6][6]={{0,0,0,0,0,0},{0,1,3,3,4,0},{0,0,6,4,0,0},{0,4,0,2,1,0},{0,6,0,4,2,0},{0,0,0,0,0,0}};
/* for(i=0;i<6;i++)
{for(j=0;j<6;j++)
c[i][j]=0;} //初始化二维数组
cout<
while(cin>>value) //输入控制
{
for(i=0;i<6;i++)
{for(j=0;j<6;j++)
c[i][j]=value;}
} //初始化二维数组;//while*/
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
cout<
};
Point a,b;
for(i=0;;i++)
{
int sum=0;
cout<
cin>>a.x>>a.y>>b.x>>b.y;
if(a.x>0&&a.x<5&&a.y>0&&a.y<5&&b.x>0&&b.x<5&&b.y>0&&b.y<5)
{
if(a.x==b.x&&a.y==b.y) {cout<
v=shortestline(a,b,c);
if(v==1)
{
c[a.x][a.y]=0;c[b.x][b.y]=0;
cout<
}
}
else cout<
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
sum+=c[i][j];
};
if(sum==0) break;
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
cout<
};
};
cout<
getchar();
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。