首页 > 其他 > 详细

hdu 3295 模拟过程。数据很水

时间:2014-04-14 11:10:16      阅读:512      评论:0      收藏:0      [点我收藏+]

An interesting mobile game

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 255    Accepted Submission(s): 123


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.
bubuko.com,布布扣

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

 

Author
B.A.C
 

 

Source
 

 

bubuko.com,布布扣
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<queue>
  6 using namespace std;
  7 
  8 struct node
  9 {
 10     int a[7][7];
 11     int step;
 12 }start;
 13 struct st
 14 {
 15     int x,y;
 16 };
 17 int n,m;
 18 int to[4][2]={ {1,0},{0,1},{0,-1},{-1,0} };
 19 bool use[7][7];
 20 bool hash[7][7];
 21 queue<node>Q;
 22 queue<st>S;
 23 
 24 void Init_use(node &t)
 25 {
 26     int i,j;
 27     for(i=1;i<=n;i++)
 28         for(j=1;j<=m;j++)
 29             if(t.a[i][j]==0)use[i][j]=true;
 30             else use[i][j]=false;
 31 }
 32 bool pd(node &t)
 33 {
 34     int i,j;
 35     bool flag=true;
 36     for(i=1;i<=n;i++)
 37         for(j=1;j<=m;j++)
 38             if(t.a[i][j]!=0)flag=false;
 39     return flag;
 40 }
 41 void change(node &t)
 42 {
 43     int i,j;
 44     int tmp[7][7],k,s;
 45     bool flag;
 46 
 47     for(i=1;i<=n;i++)
 48     {
 49         for(j=1;j<=m;j++)
 50             if(!use[i][j] && hash[i][j])
 51             {
 52                 use[i][j]=true;
 53                 t.a[i][j]=0;
 54             }
 55     }
 56     memset(tmp,0,sizeof(tmp));
 57     for(i=1;i<=m;i++)
 58     {
 59         k=n;
 60         for(j=n;j>=1;j--)
 61             if(t.a[j][i]>0)
 62                 tmp[k--][i]=t.a[j][i];
 63     }
 64     memset(t.a,0,sizeof(t.a));
 65     for(i=1,s=1;i<=m;i++)
 66     {
 67         flag=true;
 68         for(j=1;j<=n;j++) if(tmp[j][i]!=0){flag=false;break;}
 69         if(flag)continue;
 70         for(j=1;j<=n;j++)
 71             t.a[j][s]=tmp[j][i];
 72         s++;
 73     }
 74 }
 75 void bfs(int x,int y,node t,int num)
 76 {
 77     int i;
 78     st ans,cur;
 79     memset(hash,false,sizeof(hash));
 80     while(!S.empty())
 81     {
 82         S.pop();
 83     }
 84     ans.x=x;
 85     ans.y=y;
 86     S.push(ans);
 87     hash[x][y]=true;
 88 
 89     while(!S.empty())
 90     {
 91         cur=S.front();
 92         S.pop();
 93 
 94         for(i=0;i<4;i++)
 95         {
 96             ans=cur;
 97             ans.x=ans.x+to[i][0];
 98             ans.y=ans.y+to[i][1];
 99             if(ans.x>=1&&ans.x<=n && ans.y>=1&&ans.y<=m && !hash[ans.x][ans.y])
100             {
101                 if(t.a[ans.x][ans.y]==num)
102                 {
103                     hash[ans.x][ans.y]=true;
104                     S.push(ans);
105                 }
106             }
107         }
108     }
109 }
110 void dbfs()
111 {
112     int i,j;
113     node cur,t;
114     Q.push(start);
115     while(!Q.empty())
116     {
117         cur=Q.front();
118         Q.pop();
119 
120         if(pd(cur)==true)
121         {
122             printf("%d\n",cur.step);
123             return;
124         }
125         Init_use(cur);
126 
127         for(i=1;i<=n;i++)
128             for(j=1;j<=m;j++)
129             {
130                 if(use[i][j]==false && cur.a[i][j]!=0)
131                 {
132                     t=cur;
133                     bfs(i,j,t,cur.a[i][j]);
134                     change(t);
135                     t.step++;
136                     Q.push(t);
137                 }
138             }
139     }
140 }
141 int main()
142 {
143     int i,j;
144     while(scanf("%d%d",&n,&m)>0)
145     {
146         for(i=1;i<=n;i++)
147             for(j=1;j<=m;j++)
148                 scanf("%d",&start.a[i][j]);
149         start.step=0;
150         while(!Q.empty())
151         {
152             Q.pop();
153         }
154         dbfs();
155     }
156     return 0;
157 }
bubuko.com,布布扣

 

hdu 3295 模拟过程。数据很水,布布扣,bubuko.com

hdu 3295 模拟过程。数据很水

原文:http://www.cnblogs.com/tom987690183/p/3662855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!