在看了一篇关于连连看通路检测算发的启发下篇写的,列出代码如下:
// 连连看通路检测算法
//#include <iostream>
//using namespace std;
#include <stdio.h>
int main ()
{
// 地图设置
short mapX = 6, mapY = 5;
// (要改进)横纵参数应该可以设置
short map[5][6] = {0, 0, 0, 0, 0, 0,
0, 1, 3, 2, 3, 0,
0, 2, 2, 1, 2, 0,
0, 1, 3, 1, 3, 0,
0, 0, 0, 0, 0, 0};
// 设置选中坐标
// (要改进)要检测位置是否相同,值是否相同
short Set1X = 4, Set1Y = 1, Set2X = 2, Set2Y = 3;
// 处理过程
// 纵向处理
// 缓存初始
// (要改进)缓存长度应该可以设置
short Set1BufY[6][2] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
short Set2BufY[6][2] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// 基点放入缓存
Set1BufY[0][0] = Set1X;
Set1BufY[0][1] = Set1Y;
Set2BufY[0][0] = Set2X;
Set2BufY[0][1] = Set2Y;
// 纵向检测:基点1以上
short nBufY = 1;
for (short TmpY = Set1Y - 1; TmpY >= 0; --TmpY)
{
if (map[TmpY][Set1X] == 0)
{
Set1BufY[nBufY][0] = Set1X;
Set1BufY[nBufY][1] = TmpY;
++nBufY;
}
else
{
break;
}
}
// 纵向检测:基点1以下
for (short TmpY = Set1Y + 1; TmpY < mapY; ++TmpY)
{
if (map[TmpY][Set1X] == 0)
{
Set1BufY[nBufY][0] = Set1X;
Set1BufY[nBufY][1] = TmpY;
++nBufY;
}
else
{
break;
}
}
Set1BufY[nBufY][0] = 0;
Set1BufY[nBufY][1] = 0;
// 纵向检测:基点2以上
nBufY = 1;
for (short TmpY = Set2Y - 1; TmpY >= 0; --TmpY)
{
if (map[TmpY][Set2X] == 0)
{
Set2BufY[nBufY][0] = Set2X;
Set2BufY[nBufY][1] = TmpY;
++nBufY;
}
else
{
break;
}
}
// 纵向检测:基点2以下
for (short TmpY = Set2Y + 1; TmpY < mapY; ++TmpY)
{
if (map[TmpY][Set2X] == 0)
{
Set2BufY[nBufY][0] = Set2X;
Set2BufY[nBufY][1] = TmpY;
++nBufY;
}
else
{
break;
}
}
Set2BufY[nBufY][0] = 0;
Set2BufY[nBufY][1] = 0;
// 纵向检测:横向通路检测
// 连通标志
bool bChkOK = false;
for (short nChkLink1 = 0; !(Set1BufY[nChkLink1][0] == 0 && Set1BufY[nChkLink1][1] == 0); ++nChkLink1)
{
// 设置同轴对比加速检查标志
bool bShortPass = false;
// debug
//cout << "enter X for 1" << endl;
for (short nChkLink2 = 0; !(Set2BufY[nChkLink2][0] == 0 && Set2BufY[nChkLink2][1] == 0); ++nChkLink2)
{
// debug
//cout << "enter X for 2" << endl;
// 检测是否在同一横轴
if (Set1BufY[nChkLink1][1] == Set2BufY[nChkLink2][1])
{
// 因为已经检查到同轴点,所以设置同轴对比加速检查标志为真
bShortPass = true;
// debug
//cout << "enter X for 2-if" << endl;
// 计算对比点间距离
short nStep = Set1BufY[nChkLink1][0] - Set2BufY[nChkLink2][0];
// 偏移量
short OpNum;
if (nStep > 0)
{
OpNum = -1;
--nStep;
}
else
{
OpNum = 1;
++nStep;
}
bChkOK = true;
short tmpvy = Set1BufY[nChkLink1][1];
for (short mStep = nStep; mStep != 0; mStep += OpNum)
{
// debug
//cout << "enter X for 3" << endl;
if (map[tmpvy][Set1BufY[nChkLink1][0] - mStep] == 0)
{
// 继续检测下一点
continue;
}
else
{
// 通路有障碍无法连通
bChkOK = false;
break;
}
}
// 如果通路连通则显示连通信息
if (bChkOK == true)
{
//cout << "Linked." << endl;
printf("%s", "Linked\n");
return 0;
}
}
else
{
// 判断同轴对比加速检查标志,以实行检查加速
if (bShortPass == true)
{
// debug
//cout << "enter shortpass" << endl;
break;
}
}
}
}
// 纵向处理完毕
// 横向处理
// 缓存初始
// (要改进)缓存长度应该可以设置
short Set1BufX[7][2] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
short Set2BufX[7][2] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// 基点放入缓存
Set1BufX[0][0] = Set1X;
Set1BufX[0][1] = Set1Y;
Set2BufX[0][0] = Set2X;
Set2BufX[0][1] = Set2Y;
// 横向检测:基点1以左
short nBufX = 1;
for (short TmpX = Set1X - 1; TmpX >= 0; --TmpX)
{
if (map[Set1Y][TmpX] == 0)
{
Set1BufX[nBufX][0] = TmpX;
Set1BufX[nBufX][1] = Set1Y;
++nBufX;
}
else
{
break;
}
}
// 横向检测:基点1以右
for (short TmpX = Set1X + 1; TmpX < mapX; ++TmpX)
{
if (map[Set1Y][TmpX] == 0)
{
Set1BufX[nBufX][0] = TmpX;
Set1BufX[nBufX][1] = Set1Y;
++nBufX;
}
else
{
break;
}
}
Set1BufX[nBufX][0] = 0;
Set1BufX[nBufX][1] = 0;
// 横向检测:基点2以左
nBufX = 1;
for (short TmpX = Set2X - 1; TmpX >= 0; --TmpX)
{
if (map[Set2Y][TmpX] == 0)
{
Set2BufX[nBufX][0] = TmpX;
Set2BufX[nBufX][1] = Set2Y;
++nBufX;
}
else
{
break;
}
}
// 横向检测:基点2以右
for (short TmpX = Set2X + 1; TmpX < mapX; ++TmpX)
{
if (map[Set2Y][TmpX] == 0)
{
Set2BufX[nBufX][0] = TmpX;
Set2BufX[nBufX][1] = Set2Y;
++nBufX;
}
else
{
break;
}
}
Set2BufX[nBufX][0] = 0;
Set2BufX[nBufX][1] = 0;
// 横向检测:纵向通路检测
// 连通标志
bChkOK = false;
for (short nChkLink1 = 0; !(Set1BufX[nChkLink1][0] == 0 && Set1BufX[nChkLink1][1] == 0); ++nChkLink1)
{
// 设置同轴对比加速检查标志
bool bShortPass = false;
// debug
//cout << "enter X for 1" << endl;
for (short nChkLink2 = 0; !(Set2BufX[nChkLink2][0] == 0 && Set2BufX[nChkLink2][1] == 0); ++nChkLink2)
{
// debug
//cout << "enter X for 2" << endl;
// 检测是否在同一纵轴
if (Set1BufX[nChkLink1][0] == Set2BufX[nChkLink2][0])
{
// 因为已经检查到同轴点,所以设置同轴对比加速检查标志为真
bShortPass = true;
// debug
//cout << "enter X for 2-if" << endl;
// 计算对比点间距离
short nStep = Set1BufX[nChkLink1][1] - Set2BufX[nChkLink2][1];
// 偏移量
short OpNum;
if (nStep > 0)
{
OpNum = -1;
--nStep;
}
else
{
OpNum = 1;
++nStep;
}
bChkOK = true;
short tmpvx = Set1BufX[nChkLink1][0];
for (short mStep = nStep; mStep != 0; mStep += OpNum)
{
// debug
//cout << "enter X for 3" << endl;
if (map[Set1BufX[nChkLink1][1] - mStep][tmpvx] == 0)
{
// 继续检测下一点
continue;
}
else
{
// 通路有障碍无法连通
bChkOK = false;
break;
}
}
// 如果通路连通则显示连通信息
if (bChkOK == true)
{
//cout << "Linked." << endl;
printf("Linked\n");
return 0;
}
}
else
{
// 判断同轴对比加速检查标志,以实行检查加速
if (bShortPass == true)
{
// debug
//cout << "enter shortpass" << endl;
break;
}
}
}
}
// 横向处理完毕
// 处理过程完毕
// 如果通路不能连通则显示不能连通信息
if (bChkOK == false)
{
//cout << "Unlinked." << endl;
printf("%s", "Unlinked\n");
}
return 0;
}