http://cuirunxing.googlepages.com/
连连看封装算法
LLKan封装的算法
public class LLKan
{
private int[,] lianLianKan;
private int mylength,myheight;
private int circletime;
public LLKan(int length,int height)
{
mylength=length+2;
myheight=height+2;
this.lianLianKan=new int[mylength,myheight];
for(int x=0;x<mylength;x++)
{
this.lianLianKan[x,0]=100;
this.lianLianKan[x,myheight-1]=100;
}
for(int y=0;y<myheight;y++)
{
this.lianLianKan[0,y]=100;
this.lianLianKan[mylength-1,y]=100;
}
}
public void Assignment(int x,int y,int myvalue)
{
this.lianLianKan[x,y]=myvalue;
}
public bool JudgeRoute(int startx,int starty,int endx,int endy)
{
//两次点击的是同一个图片的话,路径错误。
if(endx==startx&&endy==starty)
{
return false;
}
//两次点击的图片类型不同的话,路径错误。
if(lianLianKan[endx,endy]!=lianLianKan[startx,starty])
{
return false;
}
this.circletime=0;
if(this.RightLeft(startx,starty,endx,endy))
{
this.lianLianKan[startx,starty]=100;
this.lianLianKan[endx,endy]=100;
return true;
}
this.circletime=0;
if(this.UpDown(startx,starty,endx,endy))
{
this.lianLianKan[startx,starty]=100;
this.lianLianKan[endx,endy]=100;
return true;
}
return false;
}
private bool RightLeft(int x,int y,int endx,int endy)
{
this.circletime++;
//路径转折了3次以上,此路经寻找失败
if(circletime>3)
{
this.circletime–;
return false;
}
for(int hx=x-1;hx>=0;hx–)
{
//目标点在起始点水平向左的方向
if(hx==endx&&y==endy)
{
return true;
}
//从起始点开始水平向左的路不通
if(lianLianKan[hx,y]!=100)
break;
//寻找上下方向的路。
if(this.UpDown(hx,y,endx,endy))
{
return true;
}
}
for(int hx=x+1;hx<this.mylength;hx++)
{
//目标点在起始点水平向右的方向
if(hx==endx&&y==endy)
{
return true;
}
//从起始点开始水平向右的路不通
if(lianLianKan[hx,y]!=100)
break;
//寻找上下方向的路。
if(this.UpDown(hx,y,endx,endy))
{
return true;
}
}
this.circletime–;
return false;
}
private bool UpDown(int x,int y,int endx,int endy)
{
this.circletime++;
//路径转折了3次以上,此路经寻找失败
if(circletime>3)
{
this.circletime–;
return false;
}
for(int hy=y-1;hy>=0;hy–)
{
//目标点在起始点垂直向上的方向
if(x==endx&&hy==endy)
{
return true;
}
//从起始点开始垂直向上的路不通
if(lianLianKan[x,hy]!=100)
break;
//寻找左右方向的路。
if(this.RightLeft(x,hy,endx,endy))
{
return true;
}
}
for(int [...]