HDU

来源:互联网 发布:itunes无法连接到网络 编辑:程序博客网 时间:2024/06/05 23:53

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 60 0 0 3 4 40 1 1 3 3 32 2 1 2 3 31 1 1 1 3 32 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 00 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 02 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 01 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 02 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;}