hdu 3259 An interesting mobile game (dfs+bfs+模拟)
来源:互联网 发布:天音软件 编辑:程序博客网 时间:2024/06/01 09:35
An interesting mobile game
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 222 Accepted Submission(s): 104
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.
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
4Hint0 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
Author
B.A.C
Source
2010 “HDU-Sailormoon” Programming Contest
ps:这题和hdu4090很像,基本方法和它一样。
思路:
先bfs查找块(能一次消除的),然后对这些块dfs,然后模拟下降操作和左移操作就够了。
注意:
数组传递,传递的是指针,如果修改值则修改的是实际值,可以好好利用这一特性,同时也要避免犯错。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define maxn 7#define INF 0x3f3f3f3fusing namespace std;int n,m,ans,sx,sy;int mp[maxn][maxn];int dx[]= {-1,1,0,0};int dy[]= {0,0,-1,1};struct Node{ int cnt; int x[50],y[50];};struct node{ int x,y,c;}cur,now,q[50];void bfs(int cxx,int tmp[][7],Node tt[]){ int i,j,nx,ny,tx,ty,cnt=0; int head=0,tail=-1; cur.x=sx; cur.y=sy; cur.c=tmp[sx][sy]; tmp[sx][sy]=0; q[++tail]=cur; while(head<=tail) { now=q[head]; head++; cnt++; nx=tt[cxx].x[cnt]=now.x; ny=tt[cxx].y[cnt]=now.y; for(i=0;i<4;i++) { tx=nx+dx[i]; ty=ny+dy[i]; if(tx<1||tx>n||ty<1||ty>m||tmp[tx][ty]!=now.c) continue ; cur.x=tx; cur.y=ty; cur.c=tmp[tx][ty]; tmp[tx][ty]=0; q[++tail]=cur; } } tt[cxx].cnt=cnt;}int solve(int tmp[][7],Node tt[]){ int i,j,cxx=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(tmp[i][j]) { sx=i,sy=j; cxx++; bfs(cxx,tmp,tt); } } } return cxx;}void change(int tmp[7][7]){ int i,j,pc,flag; int pp[7][7]={0},hh[7][7]={0},pos[7]; for(j=1;j<=m;j++) { pos[j]=n; } for(i=n;i>=1;i--) // 模拟下降 { for(j=1;j<=m;j++) { if(tmp[i][j]) { pp[pos[j]][j]=tmp[i][j]; pos[j]--; } } } pc=1; for(j=1;j<=m;j++) { flag=0; for(i=1;i<=n;i++) { if(pp[i][j]) flag=1; } if(flag) { for(i=1;i<=n;i++) { hh[i][pc]=pp[i][j]; } pc++; } } memcpy(tmp,hh,sizeof(hh));}void dfs(int p[7][7],int step){ int i,j,t; if(step>=ans) return ; int tmp[7][7]; Node tt[50]; memcpy(tmp,p,sizeof(tmp)); // 第一次发现这里不能写sizeof(p) 汗 debug了一会 t=solve(tmp,tt); if(!t) { if(ans>step) ans=step; return ; } for(i=1;i<=t;i++) { memcpy(tmp,p,sizeof(tmp)); for(j=1;j<=tt[i].cnt;j++) { tmp[tt[i].x[j]][tt[i].y[j]]=0; } change(tmp); dfs(tmp,step+1); }}int main(){ int i,j; while(~scanf("%d%d",&n,&m)) { memset(mp,0,sizeof(mp)); for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { scanf("%d",&mp[i][j]); } } ans=INF; dfs(mp,0); printf("%d\n",ans); } return 0;}
- hdu 3259 An interesting mobile game (dfs+bfs+模拟)
- HDU-3295-An interesting mobile game(BFS+DFS)
- hdu 3295 An interesting mobile game(bfs)
- HDU3295An interesting mobile game(BFS +模拟)
- HDU 3295 An interesting mobile game
- hdu3295 An interesting mobile game (IDA*)
- HDU-2259-Continuous Same Game (2)(BFS+DFS+模拟)
- HDU 2351 - One is an Interesting Number (模拟)
- BUPT OJ148 An Interesting Game
- 图论专题 An Interesting Game
- sdut2414 An interesting game 费用流
- hdu多校第三场 Solve this interesting problem (5323 线段树模拟 + DFS)
- hdu 4090 GemAnd Prince (bfs+dfs+模拟)
- HDU-2258-Continuous Same Game (1)(DFS+模拟)
- HDU 2258 Continuous Same Game (1) (dfs+模拟)
- hdu 5323 Solve this interesting problem 【DFS】
- HDU 5323 Solve this interesting problem(DFS)
- HDU 5323Solve this interesting problem(DFS)
- 问题11:双链表的相关操作
- 详谈 vc++源码免杀全套思路方法
- fight club
- hdu 4712
- node.js对mongodb的一些操作
- hdu 3259 An interesting mobile game (dfs+bfs+模拟)
- my hibernate note
- hdu4714 Tree2cycle 树形DP
- 飞腾评卷系统单机版上线
- 【Linux配置】:ruijie配置 & eclipse cdt 安装 & grub改变选择菜单等待时间
- XAMPP3.1.0中Apache2.4.3的多站点配置(vhost/virtual host)
- 系统托盘编程完全指南
- 内存溢出与数据库锁表的问题
- 浮点运算和字节对齐