• 消灭星星的数组高效率算法(c++代码,控制台程序)


    #include <iostream>
    using namespace std;
    
    #define ROW 12
    #define COL  10
    
    class 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 << ")" << "	";}
    			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.removeSameStar();
    	
    
    	star.checkColMove();
    	star.print();
    
    	star.checkGameOver() ?

    cout <<"over" : cout <<"not over"; system("pause"); return 0; }


  • 相关阅读:
    刷题柱 -- 暂封
    模板重搭建計劃
    思路与好题记录与小技巧
    错误记录
    随便记点东西……
    图床
    杂碎的小技巧
    hnsdfz -- 6.21 -- day7
    hsdf -- 6.21 -- day6
    hnsdfz -- 6.20 -- day5
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7029186.html
Copyright © 2020-2023  润新知