Home  >  Article  >  Backend Development  >  【C++ Fun Program】Happy Xiaoxiaole

【C++ Fun Program】Happy Xiaoxiaole

little bottle
little bottleforward
2019-04-09 15:44:393828browse

Are you like the editor, you always like to pick up your mobile phone and open mini games to play in your spare time. This article is about the production process of the C version of the popular game Happy Xiaoxiaole. Friends who are interested can learn about it!

Problem Description

Given a matrix, determine which grid to move to achieve elimination. (define three in a row to eliminate)

It is said to be Huawei's written test question.

Analysis

First write a function to determine whether the grid containing (i, j) can be eliminated.

Then <span style='font-family: 微软雅黑, "Microsoft YaHei";'>swap right and down</span>, and then call the function written above to judge
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>Whether the two exchanged grids</span> are eliminated.

The key point is:

  1. You only need to swap right and downward, because when traversing, subsequent swaps will be repeated. The former one determines whether the right exchange is eliminated, and the latter traversal does not need to determine whether the left exchange is repeated.
  2. It is necessary to judge whether the two exchanged grids can be eliminated in order to achieve a comprehensive judgment.

Code

//
//  main.cpp
//  huawei
//
//  Created by SteveWong on 11/10/2016.
//  Copyright © 2016 SteveWong. All rights reserved.
//

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
//#include <cstdlib>
using namespace std;


const int LEN = 8;

void pmap(int map[][LEN])
{
    for (int i = 0; i < LEN; ++i)
    {
        for (int j = 0; j < LEN; ++j)
        {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
}



// 检查以(i,j)为中心的点, 看是否可以消除
bool check(int map[][LEN], int i, int j)// 保证i、j不越界,
{
    if (
        (i-1>=0 && i+1<LEN && map[i-1][j]==map[i][j]&&map[i][j]==map[i+1][j])
        || (j-1>=0 && j+1<LEN && map[i][j-1]==map[i][j]&&map[i][j]==map[i][j+1])
        || (i-2>=0 && map[i-2][j]==map[i-1][j]&&map[i-1][j]==map[i][j])
        || (j-2>=0 && map[i][j-2]==map[i][j-1]&&map[i][j-1]==map[i][j])
        || (i+2<LEN && map[i+2][j]==map[i+1][j]&&map[i+1][j]==map[i][j])
        || (j+2<LEN && map[i][j+2]==map[i][j+1]&&map[i][j+1]==map[i][j])
        )
    {
        return true;
    }
    return false;
}


bool swapAndJudge(int m[][LEN], int i, int j)// 保证i、j不越界, 应该对被swap的两个点都做纵向和横向的检查
{
    int map[LEN][LEN];
    for (int ii = 0; ii < LEN; ++ii)
    {
        for (int jj = 0; jj < LEN; ++jj)
        {
            map[ii][jj] = m[ii][jj];
        }
    }
    // 原来就可以消除
    if (check(map, i, j))
    {
        printf("no need to swap at (%d, %d)\n", i, j);
        return true;
    }
    // 只需要向下换和向右换
    // 向下换
    if (i + 1 < LEN)
    {
        swap(map[i+1][j], map[i][j]);

        if (check(map, i, j))
        {
            printf("# swap and sweap! (%d, %d)\n", i, j);
            return true;
        }
        if (check(map, i+1, j))
        {
            printf("# swap and sweap! (%d, %d)\n", i+1, j);
            return true;
        }

        swap(map[i+1][j], map[i][j]);// 换回来
    }

    // 向右换
    if (j + 1 < LEN)
    {
        swap(map[i][j+1], map[i][j]);

        if (check(map, i, j))
        {
            printf("# swap and sweap! (%d, %d)\n", i, j);
            return true;
        }
        if (check(map, i, j+1))
        {
            printf("# swap and sweap! (%d, %d)\n", i, j+1);
            return true;
        }

        swap(map[i][j+1], map[i][j]);// 换回来
    }

    return false;

}


void findMinSwap(int map[][LEN])
{
    for (int i = 0; i < LEN; ++i)
    {
        for (int j = 0; j < LEN; ++j)
        {
            if (swapAndJudge(map, i, j))
            {
                printf("gotcha! (%d, %d)\n", i, j);
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
//    std::cout << "Hello, World!\n";
    srand(unsigned(time(0)));
    for (int i = 0; i < LEN; ++i)
    {
        for (int j = 0; j < LEN; ++j)
        {
            map[i][j] = rand() % 5;
        }
    }
    cout << "xiaoxiaole!\n";
    findMinSwap(map);
    pmap(map);
    return 0;
}

[Recommended course: C Video Tutorial]

The above is the detailed content of 【C++ Fun Program】Happy Xiaoxiaole. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete