if (p1.x == p2.x || p1.y == p2.y)
return nullPoint;
_point c1,c2;
c1.x=p1.x;
c1.y=p2.y;
c2.x=p2.x;
c2.y=p1.y;
if (map[c1.x][c1.y] == empty){
bool b1=havePathCorner0(p1,c1);
bool b2=havePathCorner0(c1,p2);
if (b1 && b2)
return c1;
}
if (map[c2.x][c2.y] == empty){
bool b1=havePathCorner0(p1,c2);
bool b2=havePathCorner0(c2,p2);
if (b1 && b2)
return c2;
}
return nullPoint;
}
//检查两折连接,返回两个点,
//返回点坐标为负表示不存在两折连接
//其中使用了4个方向的循环查找
_point result[2];
_point *havePathCorner2(_point p1,_point p2){
int i;
_point *r=result;
//search direction 1
for(i=p1.y+1;i<_height;i++){
if (map[p1.x][i] == empty){
_point c1;
c1.x=p1.x;
c1.y=i;
_point d1=havePathCorner1(c1,p2);
if (d1.x != -1){
r[0].x=c1.x;
r[0].y=c1.y;
r[1].x=d1.x;
r[1].y=d1.y;
return r;
}
} else
break;
}
//search direction 2
for(i=p1.y-1;i>-1;i--){
if (map[p1.x][i] == empty){
_point c1;
c1.x=p1.x;
c1.y=i;
_point d1=havePathCorner1(c1,p2);
if (d1.x != -1){
r[0].x=c1.x;
r[0].y=c1.y;
r[1].x=d1.x;
r[1].y=d1.y;
return r;
}
} else
break;
}
//search direction 3
for(i=p1.x+1;i<_width;i++){
if (map[i][p1.y] == empty){
_point c1;
c1.x=i;
c1.y=p1.y;
_point d1=havePathCorner1(c1,p2);
if (d1.x != -1){
r[0].x=c1.x;
r[0].y=c1.y;
r[1].x=d1.x;
r[1].y=d1.y;
return r;
}
} else
break;
}
//search direction 4
for(i=p1.x-1;i>-1;i--){
if (map[i][p1.y] == empty){
_point c1;
c1.x=i;
c1.y=p1.y;
_point d1=havePathCorner1(c1,p2);
if (d1.x != -1){
r[0].x=c1.x;
r[0].y=c1.y;
r[1].x=d1.x;
r[1].y=d1.y;
return r;
}
} else
break;
}
r[1].x=r[0].x=r[0].y=r[1].y=-1;
return r;
}
//汇总上面的3种情况,查找两个点之间是否存在合法连接
bool havePath(_point p1,_point p2){
if (havePathCorner0(p1,p2)){
printf("[%d,%d] to [%d,%d] have a direct path.\n",p1.x,p1.y,p2.x,p2.y);
return TRUE;
}
_point r=havePathCorner1(p1,p2);
if (r.x != -1){
printf("[%d,%d] to [%d,%d] have a 1 cornor path throught [%d,%d].\n",
p1.x,p1.y,p2.x,p2.y,r.x,r.y);
return TRUE;
}
_point *c=havePathCorner2(p1,p2);
if (c[0].x != -1){
printf("[%d,%d] to [%d,%d] have a 2 cornor path throught [%d,%d] and [%d,%d].\n",
p1.x,p1.y,p2.x,p2.y,c[0].x,c[0].y,c[1].x,c[1].y);
return TRUE;
}
return FALSE;
}
//对于给定的起始点,查找在整个图板中,起始点之后的所有点,
//是否存在相同图片,并且两张图片之间可以合法连线
bool searchMap(_point p1){
int ix,iy;
bool inner1=TRUE;
《连连看》算法C语言演示(自动连连看)(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://www.heiqu.com/56e4f54cdb339007498ae69a67cbcac4.html