欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)消消樂(lè)游戲

 更新時(shí)間:2022年05月09日 16:01:19   作者:stevewongbuaa  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)消消樂(lè)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)消消樂(lè)游戲的具體代碼,供大家參考,具體內(nèi)容如下

問(wèn)題描述

給定一個(gè)矩陣, 判斷移動(dòng)哪一個(gè)格子,可以實(shí)現(xiàn)消除。(定義連續(xù)三個(gè)即可消除)

據(jù)說(shuō)是華為的筆試題。

分析

先寫(xiě)一個(gè)函數(shù),判斷包含(i, j)的格子是否可能實(shí)現(xiàn)消除。

然后就是向右向下交換,然后調(diào)用上面寫(xiě)好的函數(shù)判斷
被交換的兩個(gè)格子是否實(shí)現(xiàn)消除。

重點(diǎn)是:

1、只需要向右向下交換,因?yàn)楸闅v的時(shí)候,后面的交換會(huì)重復(fù)。前一個(gè)判斷了向右交換是否消除,后一個(gè)遍歷就不需要再判斷向左交換是否重復(fù)了。
2、一定要對(duì)被交換的兩個(gè)格子都判斷是否能消除,才能實(shí)現(xiàn)全面的判斷。

代碼

//
// ?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)為中心的點(diǎn), 看是否可以消除
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不越界, 應(yīng)該對(duì)被swap的兩個(gè)點(diǎn)都做縱向和橫向的檢查
{
? ? int map[LEN][LEN];
? ? for (int ii = 0; ii < LEN; ++ii)
? ? {
? ? ? ? for (int jj = 0; jj < LEN; ++jj)
? ? ? ? {
? ? ? ? ? ? map[ii][jj] = m[ii][jj];
? ? ? ? }
? ? }
? ? // 原來(lái)就可以消除
? ? if (check(map, i, j))
? ? {
? ? ? ? printf("no need to swap at (%d, %d)\n", i, j);
? ? ? ? return true;
? ? }
? ? // 只需要向下?lián)Q和向右換
? ? // 向下?lián)Q
? ? 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]);// 換回來(lái)
? ? }

? ? // 向右換
? ? 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]);// 換回來(lái)
? ? }

? ? 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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論