Lianliankan Game Assistant

Lianliankan Game Assistant

Topic information

"Lianliankan" is a puzzle game.
"Lianliankan" can be eliminated by connecting the same two cards with three straight lines. The rules are simple and easy to use.
This question is written to simulate a person playing the game of Lianliankan, and the process of a computer simulating person playing Lianliankan is as follows:
1. Analyze the game of this game and transform the game into a two-digit array. 0 indicates a blank area, and the number indicates the type of game card that appears in the game.
2. Use the breadth first search algorithm to determine whether the two game cards can be eliminated.
3. The program simulator clicks a pair of game cards that can be eliminated to eliminate.
Convert a game into a two-dimensional array as follows:
0 0 0 0 0
0 1 2 0 0
0 0 3 4 0
0 0 0 1 0

enter

The rows and columns of the two-digit array, the two-digit array, the coordinates of the start element, and the coordinates of the end element, determine whether the start game card and the end game card can be eliminated.

Output

TRUE OR FALSE

Test sample

Test example 1

4 5
0 0 0 0 0 0 1 2 0 0 0 0 3 4 0 0 0 0 1 0
1 1
3 3
TRUE

Test example 2

5 6
0 0 0 0 0 0 0 1 2 3 0 0 3 0 0 0 0 3 0 2 3 4 0 2 0 0 0 2 1 4
1 1
4 4
TRUE

Test example 3

5 6
0 0 0 0 0 0 0 1 2 3 0 0 3 0 0 0 0 3 0 2 3 4 0 2 0 0 0 2 1 4
3 3
4 5
FALSE

answer

#include<iostream>
#include<queue>

using namespace std;

int Map[1000][1000] = {
    
    0};
int vir[1000][1000] = {
    
    0};
int m, n, StartX, StartY, EndX, EndY;
int trend[4][2] = {
    
    {
    
    0,  1},
                   {
    
    0,  -1},
                   {
    
    1,  0},
                   {
    
    -1, 0}};//0-下,1-上,2-右,3-左
struct Node
{
    
    
    int x, y;//坐标
    int dir; //方向
    int turn;//转弯次数
};

void BFS()
{
    
    
    queue<Node> q;
    Node k0, k1, t0;
    bool flag = false;
    for (int i = 0; i < 4; i++)
    {
    
    //初始化四个方向的点
        t0.x = StartX + trend[i][0];
        t0.y = StartY + trend[i][1];
        t0.dir = i;
        t0.turn = 1;
        vir[t0.x][t0.y] = t0.turn;
        q.push(t0);
    }
    while (!q.empty())
    {
    
    //在队列中开始广度优先搜索
        k0 = q.front();
        q.pop();
        if (k0.x == EndX && k0.y == EndY)
        {
    
    //找到了终点
            flag = true;
            cout << "TRUE" << endl;
            break;
        }
        for (int i = 0; i < 4; i++)
        {
    
    //对当前点遍历四个方向
            k1.x = k0.x + trend[i][0];
            k1.y = k0.y + trend[i][1];
            k1.dir = i;
            if (k1.dir != k0.dir)
            {
    
    //方向发生改变则相当于转弯了
                k1.turn = k0.turn + 1;
            }
            else
            {
    
    
                k1.turn = k0.turn;
            }

            if (k1.turn > 3 || k1.x < 0 || k1.y < 0 || k1.x > m - 1 || k1.y > n - 1)
            {
    
    //如果边树大于三条边了或者是超出地图范围了就不考虑了直接下一个
                continue;
            }
            if (Map[k1.x][k1.y] != 0 && Map[k1.x][k1.y] != Map[EndX][EndY])
            {
    
    //如果在地图上这个点不是0且不是终点,相当于走不通,也不考虑了直接下一个
                continue;
            }
            if (vir[k1.x][k1.y] == 0 || vir[k1.x][k1.y] >= k1.turn)
            {
    
    //如果是个未访问对点,或者是转向角度低于图中的点。
                //因为图中转向角度大的点相当于已经访问过了,甚至是已经到3不符合要求的点
                vir[k1.x][k1.y] = k1.turn;//更新我们刚算的转向值
                q.push(k1);//并压入队列等待下一轮判定
            }
        }
    }
    if (!flag)
    {
    
    
        cout << "FALSE" << endl;
    }
}

int main()
{
    
    
    //freopen("/Users/zhj/Downloads/test.txt", "r", stdin);
    cin >> m >> n;
    for (int i = 0; i < m; i++)
    {
    
    
        for (int j = 0; j < n; j++)
        {
    
    
            cin >> Map[i][j];
        }
    }
    cin >> StartX >> StartY >> EndX >> EndY;
    BFS();
    return 0;
}

Guess you like

Origin blog.csdn.net/zhj12399/article/details/110002379