消滅星星的數組高效率演算法(c++代碼,控制台程式)

來源:互聯網
上載者:User

標籤:

#include <iostream>using namespace std;#define ROW 12#define COL  10class Star{public:enum Stat_star{willRemoved = -2,hasRemoved, normal};Star(){for (unsigned i = 0; i < ROW; i++){for (unsigned j = 0; j < COL; j++){m_arr[i][j].state = normal;m_arr[i][j].visited = false;}}}~Star(){}void print(){cout << "=====================" << endl;for (unsigned i = 0; i < ROW; i++){for (unsigned j = 0; j < COL; j++){cout << m_arr[i][j].data << "(" << m_arr[i][j].state << ")" << "\t";}cout << endl;}}void resetVisited(){for (unsigned i = 0; i < ROW; i++){for (unsigned j = 0; j < COL; j++){if(m_arr[i][j].state != hasRemoved){m_arr[i][j].visited = false;}}}}//檢測是否遊戲結束bool checkGameOver(){for (unsigned i = 0; i < ROW; i++){for (unsigned j = 0; j < COL; j++){if(findWillRemove(i, j) > 1){return false;}}}return true;}//檢測是否要整列移動void checkColMove(){int dt = 0;//列的增量for (int i = 0; i < COL - dt; i++){bool allRemoved = true;//檢測某列是否整列為空白for (int j = 0; j < ROW; j++){if(m_arr[j][i].state != hasRemoved){allRemoved = false;break;}}//開始移動列if(allRemoved){for (int x = i; x < COL; x++){for (int y = 0; y < ROW; y++){if(x + 1 < COL){Star_data tempStar = m_arr[y][x]; m_arr[y][x] = m_arr[y][x + 1];}}}//最後一列置空for (int y = 0; y < ROW; y++){m_arr[y][COL - 1].state = hasRemoved;}i--;dt++;}}}//找到相同的球後,刪除相同的球void removeSameStar(){//以列來找for (int j = 0; j < COL; j++){for (int i = ROW - 1; i >= 0; i--){if(m_arr[i][j].state == willRemoved){Star_data tempStar = m_arr[i][j]; //依次把星星下移,並且把空的星星放到最上面for (int x = i; x >= 0; x--){if(x - 1 >= 0){m_arr[x][j] = m_arr[x - 1][j];}}m_arr[0][j] = tempStar;m_arr[0][j].state = hasRemoved;i++;}}}}//點擊某個星星。找到將要被移除的星星int findWillRemove(int i, int j){int findCount = 0;findWillRemove(i, j, findCount);resetVisited();return findCount;}//尋找將要被移除的void findWillRemove(int i, int j,  int &findCount){if(i >= ROW || i < 0 || j >= COL || j < 0 || m_arr[i][j].state == hasRemoved || m_arr[i][j].visited){return;}m_arr[i][j].visited = true;findCount++;//左if(i - 1 >= 0 && m_arr[i - 1][j].state != hasRemoved && m_arr[i - 1][j].data == m_arr[i][j].data){m_arr[i][j].state = willRemoved;m_arr[i - 1][j].state = willRemoved;findWillRemove(i - 1, j, findCount);}//右if(i + 1 < ROW && m_arr[i + 1][j].state != hasRemoved && m_arr[i + 1][j].data == m_arr[i][j].data){m_arr[i][j].state = willRemoved;m_arr[i + 1][j].state = willRemoved;findWillRemove(i + 1, j, findCount);}//上if(j + 1 < COL && m_arr[i][j + 1].state != hasRemoved && m_arr[i][j + 1].data == m_arr[i][j].data){m_arr[i][j].state = willRemoved;m_arr[i][j + 1].state = willRemoved;findWillRemove(i, j + 1, findCount);}//下if(j - 1 >= 0 && m_arr[i][j - 1].state != hasRemoved && m_arr[i][j - 1].data == m_arr[i][j].data){m_arr[i][j].state = willRemoved;m_arr[i][j - 1].state = willRemoved;findWillRemove(i, j - 1, findCount);}}struct Star_data{int data;//資料Stat_star state;//狀態bool visited;//是否訪問};Star_data m_arr[ROW][COL];};int main(){Star star;int arr[ROW][COL] = {2, 1, 3, 2, 1, 2, 1, 3, 2, 1,2, 2, 5, 1, 2 ,2, 2, 5, 1, 2,3, 2, 1, 3, 3, 3, 2, 1, 3, 3,2, 5, 4, 1, 4, 2, 5, 4, 1, 4,2, 2, 3, 2, 5,2, 2, 3, 2, 5,2, 1, 3, 2, 1, 2, 1, 3, 2, 1,2, 2, 5, 1, 2 ,2, 2, 5, 1, 2,3, 2, 1, 3, 3, 3, 2, 1, 3, 3,2, 5, 4, 1, 4, 2, 5, 4, 1, 4,2, 2, 3, 2, 5,2, 2, 3, 2, 5,2, 5, 4, 1, 4, 2, 5, 4, 1, 4,2, 2, 3, 2, 5,2, 2, 3, 2, 5};for (unsigned i = 0; i < ROW; i++){for (unsigned j = 0; j < COL; j++){star.m_arr[i][j].data = arr[i][j];}}star.print();star.findWillRemove(0, 0);star.print();star.removeSameStar();star.print();star.checkColMove();star.print();star.checkGameOver() ? cout <<"over" : cout <<"not over";system("pause");return 0;}

消滅星星的數組高效率演算法(c++代碼,控制台程式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.