上次看了编程之美,里面讲到的连连看,用广度优先很有启发性.索性昨晚下了一个连连看的代码,重新写了那个匹配算法.

#include <iostream>
#include <cmath>
using namespace std;

#define MAXX 10
#define MAXY 10


typedef struct
{
        int x;
        int y;
}Structure;

Structure Next[MAXX*MAXY];
Structure Parent[MAXX][MAXY];
int minValue[MAXX][MAXY];
int disValue[MAXX][MAXY];

bool Reach(int startI, int startJ, int directionX, int directionY, int *tail, int endI, int endJ)
{
        int destiX = startI;
        int destiY = startJ;
        while (true)
        {
                destiX += directionX;
                destiY += directionY;

                if ( directionX == 0)
                {
                        if ( destiX<0 || destiX>max)
                                // over the boundrate
                                return break;
                }
                else
                {
                        if ( destiY<0 || destiY>max)
                                // over the boundrate
                                return break;
                }
                int dist = abs(directionX) + abs(directionY);
                if ( (minValue[startI][startJ]+1<minValue[destiX][destiY])||
                                (minValue[startI][startJ]+1==minValue[destiX][destiY] &&
                                 disValue[startI][startJ]+dist<disValue[destiX][destiY]))
                {
                        minValue[destiX][destiY] = minValue[startI][startJ] + 1;
                        disValue[destiX][destiY] = disValue[startI][startJ] + dist;
                        // recode the parent node
                        Parent[destiX][destiY].x = startI;
                        Parent[destiX][destiY].y = startJ;
                        (*tail)++;
                        Next[*tail].x = destiX;
                        Next[*tail].y = destiY;

                }
                if (map[destiX][destiY]==0)
                {
                        directionX += directionX;
                        directionY += directionY;
                        continue;
                }
                break;
        }// end while
        if ( minValue[endI][endJ]!=7 )
                return true;
        else
                return false;
}

bool callReach(int startI, int startJ, int endI, int endJ )
{
        // 广度优先解决
        int top = -1;
        int tail = 0;
        Next[tail].x = startI;
        Next[tail].y = startJ;
        for (int i=0; i<MAXX; i++)
                for (int j=0; j<MAXY; j++)
                {
                        //init
                        minValue[i][j] = 7;
                        disValue[i][j] = MAXX+MAXY+2;
                }
        minValue[startI][startJ] = -1;
        disValue[startI][startJ] = 0;
        while (top<tail)
        {
                top++;
                // top
                if(Reach(Next[top].x,Next[top].y,0,-1,&tail,endI,endJ))
         return true;
                // down
                if(Reach(Next[top].x,Next[top].y,0,1,&tail,endI,endJ))
         return true;
                // left
                if(Reach(Next[top].x,Next[top].y,-1,0,&tail,endI,endJ))
         return true;
                // right
                if(Reach(Next[top].x,Next[top].y,1,0,&tail,endI,endJ))
         return true;
        }
        return false;
}

int main(int argc, char *argv[])
{

        return 0;
}