HDU - 3295 An interesting mobile game

An interesting mobile game

Problem Description
XQ,one of the three Sailormoon girls,is usually playing mobile games on the class.Her favorite mobile game is called “The Princess In The Wall”.Now she give you a problem about this game.
Can you solve it?The following picture show this problem better.

This game is played on a rectangular area.This area is divided into some equal square grid..There are N rows and M columns.For each grid,there may be a colored square block or nothing.
Each grid has a number.
“0” represents this grid have nothing.
“1” represents this grid have a red square block.
“2” represents this grid have a blue square block.
“3” represents this grid have a green square block.
“4” represents this grid have a yellow square block.

1. Each step,when you choose a grid have a colored square block, A group of this block and some connected blocks that are the same color would be removed from the board. no matter how many square blocks are in this group. 
2. When a group of blocks is removed, the blocks above those removed ones fall down into the empty space. When an entire column of blocks is removed, all the columns to the right of that column shift to the left to fill the empty columns.

Now give you the number of the row and column and the data of each grid.You should calculate how many steps can make the entire rectangular area have no colored square blocks at least.
 

Input
There are multiple test cases. Each case starts with two positive integer N, M,(N, M <= 6)the size of rectangular area. Then n lines follow, each contains m positive integers X.(0<= X <= 4)It means this grid have a colored square block or nothing.
 

Output
Please output the minimum steps.
 

Sample Input
   
   
5 6 0 0 0 3 4 4 0 1 1 3 3 3 2 2 1 2 3 3 1 1 1 1 3 3 2 2 1 4 4 4
 

Sample Output
   
   
4
Hint
0 0 0 3 4 4 0 0 0 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 3 3 0 0 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 1 2 3 3 0 0 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 3 3 2 2 2 3 3 0 2 2 2 4 4 0 2 2 0 0 0 0 0 0 0 0 0 0 2 2 1 4 4 4 2 2 4 4 4 0 2 2 4 4 4 0 2 2 2 0 0 0 0 0 0 0 0 0
 

解题思路:两次BFS,第一次找到不为0的点,第二次找到包含该点的块,数据很弱,放心去写。

#include<iostream>
#include<queue>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
int n,m;

vector<vector<int> > maze;
bool vis[7][7];

struct point{
    int x;
    int y;
    point(int a,int b){
        x=a;
        y=b;
    }
};

int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

vector<vector<int> > bfs2(int ii,int jj,vector<vector<int> > mm){
    queue<point> que;
    que.push(point(ii,jj));

    int cur=mm[ii][jj];

    while(!que.empty()){

        point tp=que.front();
        que.pop();
        mm[tp.x][tp.y]=0;
        vis[tp.x][tp.y]=1;

        for(int i=0;i<4;i++){
            int x=tp.x+dx[i];
            int y=tp.y+dy[i];

            if(x==-1||x==n||y==-1||y==m)
                continue;

            if(mm[x][y]==cur)
                que.push(point(x,y));

        }
    }



    for(int j=0;j<m;j++){
        bool yes=1;
        for(int i=0;i<n;i++){
            if(mm[i][j]!=0)
                yes=0;
        }
        if(yes){

            for(int k=j+1;k<m;k++){
                for(int i=0;i<n;i++){
                    swap(mm[i][k],mm[i][k-1]);
                }
            }
        }
    }


    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(mm[i][j]==0){
                for(int k=i-1;k>=0;k--)
                    swap(mm[k][j],mm[k+1][j]);
            }
        }
    }

    return mm;

}

struct node{
    vector<vector<int> > m;
    int time;
    node(){
        m.clear();
        time=0;
    }
};

int bfs(){

    queue<node> que;
    node ttt;
    ttt.m=maze;
    ttt.time=0;
    que.push(ttt);


    while(!que.empty()){
        node tp=que.front();
        que.pop();

        memset(vis,0,sizeof(vis));



        bool yes=1;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(tp.m[i][j]!=0)
                    yes=0;
            }
        }
        if(yes)
            return tp.time;


        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                if(vis[i][j]==0&&tp.m[i][j]!=0){
                ttt.m=bfs2(i,j,tp.m);
                ttt.time=tp.time+1;
                que.push(ttt);
                }
            }



    }


   return 0;
}




int main(){


    while(cin>>n>>m){


        maze.clear();
        for(int i=0;i<n;i++){
            vector<int> tl;
            for(int j=0;j<m;j++){
                int temp;
                cin>>temp;
                tl.push_back(temp);

            }
            maze.push_back(tl);
        }

        cout<<bfs()<<endl;



    }


    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值