C++實(shí)現(xiàn)消消樂(lè)游戲
本文實(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)文章
C/C++中的atan和atan2函數(shù)實(shí)例用法
在本篇文章里小編給大家分享的是一篇關(guān)于C/C++中的atan和atan2函數(shù)實(shí)例用法相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-02-02c++ 形狀類(lèi)Shape(派生出圓類(lèi)Circle和矩形類(lèi)Rectangle)
通過(guò)C++方式,建立一個(gè)形狀類(lèi)Shape作為基類(lèi),派生出圓類(lèi)Circle和矩形類(lèi)Rectangle 求出面積并獲取相關(guān)信息2020-11-11c語(yǔ)言根據(jù)用戶輸入的出生年份并計(jì)算出當(dāng)前年齡
這篇文章主要介紹了c語(yǔ)言根據(jù)用戶輸入的出生年份并計(jì)算出當(dāng)前年齡,需要的朋友可以參考下2023-03-03淺談C語(yǔ)言編程中程序的一些基本的編寫(xiě)優(yōu)化技巧
這篇文章主要介紹了C語(yǔ)言編程中程序的一些基本的編寫(xiě)優(yōu)化技巧,文中涉及到了基礎(chǔ)的C程序內(nèi)存方面的知識(shí),非常推薦!需要的朋友可以參考下2016-02-02