连连看自动查找核心算法

#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <list> 

using namespace std;

int iup = 1;
int ileft = 2;
int idown = 3;
int iright = 4;

int findy = 0;
int findx = 0;

unsigned char g_array[11][19] = {
0x00,  0x16,  0x00,  0x00,  0x00,  0x00,  0x00,  0x0c,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x17,  0x02,  0x00,  0x00,  0x00,  
0x07,  0x13,  0x06,  0x00,  0x00,  0x14,  0x08,  0x0b,  0x11,  0x0c,  0x00,  0x00,  0x07,  0x00,  0x00,  0x08,  0x00,  0x16,  0x00,  
0x00,  0xf0,  0x00,  0x00,  0x11,  0x0c,  0x0f,  0x00,  0x04,  0x15,  0x00,  0x03,  0x12,  0x0e,  0x00,  0x12,  0x00,  0x00,  0x00,  
0x00,  0x00,  0x00,  0x0d,  0x0a,  0x02,  0x00,  0x00,  0x00,  0x11,  0x00,  0x00,  0x0e,  0x00,  0x00,  0x16,  0x04,  0x0a,  0x0b,  
0x0b,  0xf7,  0x10,  0x0c,  0x10,  0x00,  0x00,  0x05,  0x00,  0x00,  0x0b,  0x00,  0x00,  0x00,  0xf4,  0x13,  0x00,  0x00,  0x00,  
0x12,  0x00,  0x00,  0x00,  0x12,  0x00,  0x16,  0x07,  0x0a,  0x00,  0x18,  0x0e,  0x00,  0x0d,  0xf2,  0x00,  0x00,  0x0d,  0x00,  
0x00,  0x00,  0x04,  0x00,  0x00,  0x00,  0x00,  0x14,  0x00,  0x00,  0x07,  0x0f,  0xf2,  0x09,  0x00,  0x00,  0x02,  0xf0,  0x14,  
0x00,  0x03,  0x15,  0x13,  0x00,  0x14,  0x00,  0x00,  0x00,  0xf5,  0x00,  0x00,  0x00,  0x03,  0x10,  0x00,  0x00,  0x03,  0x00,  
0x00,  0x00,  0x06,  0x00,  0x00,  0x17,  0x11,  0x17,  0x06,  0x00,  0x00,  0x09,  0x00,  0x00,  0x18,  0x05,  0x00,  0x00,  0x00,  
0x13,  0x00,  0x00,  0x00,  0x02,  0x0a,  0x06,  0x0f,  0x00,  0x00,  0x15,  0x17,  0xf4,  0x00,  0x0d,  0x10,  0x05,  0x0f,  0xf7,  
0x04,  0x0e,  0x00,  0x15,  0xf5,  0x09,  0x09,  0x00,  0x00,  0x00,  0x00,  0x08,  0x00,  0x00,  0x00,  0x08,  0x00,  0x00,  0x05
};

bool find(int type, int bucking, unsigned char value, int y, int x)
{
    bool b_up, b_down, b_left, b_right;
    if(bucking > 2) {
        return false;
    }
    if(x < 0 || x == 19 || y < 0 || y == 11) {
        return false;
    }
    if(g_array[y][x] != 0) {
        if(g_array[y][x] == value) {
            findy = y;
            findx = x;
            return true;
        } else {
            return false;
        }
    } else {
        if(type == iup)
        {
            b_up = find(iup, bucking, value, y-1, x);
            b_left = find(ileft, bucking+1, value, y, x-1);
            b_right = find(iright, bucking+1, value, y, x+1);
            return b_up | b_left | b_right;
        } 
        else if(type == ileft)
        {
            b_up = find(iup, bucking+1, value, y-1, x);
            b_left = find(ileft, bucking, value, y, x-1);
            b_down = find(idown, bucking+1, value, y+1, x);
            return b_up | b_left | b_down;
        } 
        else if(type == idown)
        {
            b_down = find(idown, bucking, value, y+1, x);
            b_left = find(ileft, bucking+1, value, y, x-1);
            b_right = find(iright, bucking+1, value, y, x+1);
            return b_down | b_left | b_right;
        } 
        else 
        {
            b_up = find(iup, bucking+1, value, y-1, x);
            b_right = find(iright, bucking, value, y, x+1);
            b_down = find(idown, bucking+1, value, y+1, x);
            return b_up | b_right | b_down;
        }
    }
}


void oneStep()
{
    bool b_up, b_down, b_left, b_right;
    for(int y = 0; y < 11; y++)
    {
        for(int x = 0; x < 19; x++)
        {
            // 向四个方向辐射查找
            b_up = find(iup, 0, g_array[y][x], y-1, x);
            b_left = find(ileft, 0, g_array[y][x], y, x-1);
            b_down = find(idown, 0, g_array[y][x], y+1, x);
            b_right = find(iright, 0, g_array[y][x], y, x+1);

            if(b_up | b_left | b_down | b_right) {

                printf("%d %d %d %d\n", y, x, findy, findx);
                return;
            }
        }
    }
}

int main()
{
    oneStep();

    getchar();

    return 0;
}
点赞