HDU-3295-An interesting mobile game(BFS)

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

Author

B.A.C

Source

2010 “HDU-Sailormoon” Programming Contest

思路:因为方块会越消越少,所以没必要判重。注意消去之后,上面的会掉下来,如果某一列全为是空的,右边的会往左移。

#include <stdio.h>

struct{
int d[6][6],step;
}que[1000000],t;

int n,m,temp[6][6],nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool vis[6][6];

void dfs(int x,int y,int num)
{
    for(int i=0;i<4;i++)
    {
        x+=nxt[i][0];
        y+=nxt[i][1];

        if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && temp[x][y]==num)
        {
            vis[x][y]=1;

            t.d[x][y]=0;

            dfs(x,y,num);
        }

        x-=nxt[i][0];
        y-=nxt[i][1];
    }
}

int main()
{
    int i,j,k,p,q,top,bottom;
    bool flag;

    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&que[0].d[i][j]);

        top=0;
        bottom=1;

        que[0].step=0;

        while(top<bottom)
        {
            t=que[top];

            flag=1;

            for(i=0;i<n && flag;i++) for(j=0;j<m && flag;j++) if(t.d[i][j]) flag=0;

            if(flag)
            {
                printf("%d\n",t.step);

                break;
            }

            t.step++;

            for(i=0;i<n;i++) for(j=0;j<m;j++) temp[i][j]=t.d[i][j],vis[i][j]=0;

            for(i=0;i<n;i++) for(j=0;j<m;j++)
            {
                if(temp[i][j] && !vis[i][j])
                {
                    vis[i][j]=1;

                    t.d[i][j]=0;

                    dfs(i,j,temp[i][j]);

                    for(p=n-1;p>=0;p--)//向下移动
                    {
                        for(q=0;q<m;q++)
                        {
                            if(!t.d[p][q])
                            {
                                for(k=p-1;k>=0;k--)
                                {
                                    if(t.d[k][q])
                                    {
                                        t.d[p][q]=t.d[k][q];
                                        t.d[k][q]=0;

                                        break;
                                    }
                                }
                            }
                        }
                    }

                    for(q=0;q<m-1;q++)//向左移动
                    {
                        for(p=0;p<n;p++) if(t.d[p][q]) break;

                        if(p<n) continue;

                        for(p=0;p<n;p++)
                        {
                            t.d[p][q]=t.d[p][q+1];
                            t.d[p][q+1]=0;
                        }
                    }

                    que[bottom++]=t;

                    for(p=0;p<n;p++) for(q=0;q<m;q++) t.d[p][q]=temp[p][q];
                }
            }

            top++;
        }
    }
}

HDU-3295-An interesting mobile game(BFS)

时间: 2024-10-23 08:04:28

HDU-3295-An interesting mobile game(BFS)的相关文章

【HDOJ】3295 An interesting mobile game

其实就是一道搜索模拟题.因为数据量小,用char就够了. 1 /* 3295 */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdlib> 6 #include <queue> 7 using namespace std; 8 9 typedef struct { 10 char m[6][6]; 11 char x, y; 12 ch

xtu summer individual 1 A - An interesting mobile game

An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 2102 A计划(双层BFS)(详解)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她

HDU 1195 Open the Lock 双向BFS

题目链接:Open the Lock 题意:就是给定两个4位数,起始,结束.问从起始数字到达结束数字 最少变换多少步,每个数 可以+1 / -1 或交换位置,都算做是一步. 单广,双广都用了,感觉双向BFS,太棒了,HDU的这个题双向BFS时间优化的太棒了 有图,有真相! 时间优化了近9倍... PS:今天还学习一个sscanf函数,挺棒的 单搜的代码就不贴了,贴个双搜的 #include<cstdio> #include <iostream> #include<cstrin

hdu 1195 Open the Lock (bfs+优先队列)

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4253    Accepted Submission(s): 1858 Problem Description Now an emergent task for you is to open a password lock. The password is c

HDU 1180——诡异的楼梯( BFS)

诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 8717    Accepted Submission(s): 2148 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在竖

hdu 2102 A计划(双层BFS)(具体解释)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,由于他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来解救公主.只是公主早已习以为常,她

HDU3295An interesting mobile game(BFS +模拟)

题意:消灭星星= =, 解法:由于每次会减少一些,故不需要判重,全部转移的状态入队,直接暴力模拟操作 #include<iostream> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> #include<set> #include<stack&