xtu summer individual 1 A - An interesting mobile game
An interesting mobile game
This problem will be judged on HDU. Original ID: 3295
64-bit integer IO format: %I64d Java class name: Main
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
Output
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
Source
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <vector>
- #include <climits>
- #include <algorithm>
- #include <cmath>
- #include <queue>
- #define LL long long
- #define INF 0x3f3f3f
- using namespace std;
- int table[][],r,c,ans,cnt;
- const int dir[][] = {,-,,,-,,,};
- bool vis[][];
- struct node{
- int x,y;
- };
- struct po{
- int x,y,c;
- };
- bool check(int (*t)[]) {
- for(int i = r; i ; i--) {
- for(int j = ; j <= c; j++)
- if(t[i][j]) return false;
- }
- return true;
- }
- bool cmp(const po &a,const po &b){
- return a.c > b.c;
- }
- void calc(int (*t)[],int x,int y,const int color){
- vis[x][y] = true;
- cnt++;
- for(int i = ; i < ; i++){
- int px = x+dir[i][];
- int py =y+dir[i][];
- if(!vis[px][py] && t[px][py] == color)
- calc(t,px,py,color);
- }
- }
- void cancle(int (*t)[],int x,int y,const int color){
- bool vis[][] = {false};
- queue<node>q;
- vis[x][y] = true;
- int i,j,px,py;
- t[x][y] = ;
- q.push((node){x,y});
- while(!q.empty()){
- node temp = q.front();
- q.pop();
- for(i = ; i < ; i++){
- px = temp.x+dir[i][];
- py = temp.y+dir[i][];//这个地方写成dir[i][0]害我调了很久
- if(!vis[px][py] && t[px][py] == color){
- vis[px][py] = true;
- t[px][py] = ;
- q.push((node){px,py});
- }
- }
- }
- }
- void shift(int (*t)[]){
- int i,j,k;
- bool isempty[] = {false};
- for(j = ; j <= c; j++){
- for(i = r-; i; i--){
- for(k = i;k < r &&!t[k+][j]&&t[k][j]; k++)
- swap(t[k+][j],t[k][j]);
- }
- if(!t[r][j]) isempty[j] = true;
- }
- for(j = c-; j; j--){
- if(isempty[j]){
- for(i = ; i <= r; i++){
- for(k = j; k <= c; k++)
- t[i][k] = t[i][k+];
- }
- }
- }
- }
- bool dfs(int (*t)[],int step){
- int mp[][],i,j,m = ;
- bool flag;
- if(check(t) && ans == step) return true;
- if(step > ans) return false;
- po p[];
- memset(vis,false,sizeof(vis));
- for(i = r; i; i--){
- flag = true;
- for(j = ; j <= c; j++){
- if(t[i][j]) flag = false;
- if(t[i][j] && !vis[i][j]){
- cnt = ;
- calc(t,i,j,t[i][j]);
- p[m++] = (po){i,j,cnt};
- }
- }
- if(flag) break;
- }
- sort(p,p+m,cmp);
- for(i = ; i < m; i++){
- memcpy(mp,t,sizeof(mp));
- cancle(mp,p[i].x,p[i].y,t[p[i].x][p[i].y]);
- shift(mp);
- if(dfs(mp,step+)) return true;
- }
- return false;
- }
- int main() {
- int i,j;
- while(~scanf("%d %d",&r,&c)) {
- memset(table,,sizeof(table));
- for(i = ; i <= r; i++)
- for(j = ; j <= c; j++)
- scanf("%d",table[i]+j);
- if(check(table)) {puts("");continue;}
- for(ans = ;; ans++)
- if(dfs(table,)) break;
- printf("%d\n",ans);
- }
- return ;
- }
xtu summer individual 1 A - An interesting mobile game的更多相关文章
- HDU-3295-An interesting mobile game(BFS+DFS)
Problem Description XQ,one of the three Sailormoon girls,is usually playing mobile games on the clas ...
- 【HDOJ】3295 An interesting mobile game
其实就是一道搜索模拟题.因为数据量小,用char就够了. /* 3295 */ #include <iostream> #include <cstdio> #include & ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- xtu summer individual 2 D - Colliders
Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
随机推荐
- UVa 1220 Party at Hali-Bula 晚会
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #i ...
- Clone a Pluggable Database – 12c Edition
1. 1.Tnsnames when connecting to either Container or Pluggable instance The tnsnames.ora should be c ...
- 关于html/css的路径问题
非原创,转自:http://blog.sina.com.cn/s/blog_6c21f6480101cb33.html [问题描述]: 比如你有Web项目solo,假如目录结构如下: 在cy.css中 ...
- 493 Reverse Pairs 翻转对
给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...
- FFmpegUtil
这几天没事研究音频玩 比如 X配音app的配音功能 录一段 pcm或者wav格式的文件 替换mp4指定位置的音频刚开始卡在 pcm混合以及pcm指定位置插入.思路 一段段的视频进行切割 用到MP4Co ...
- spring mvc支持跨域请求
@WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...
- c# sqlserver连接字符串
odbc: string cnnstring = @"Driver={SQL Server Native Client 11.0};Initial Catalog = sxquadb;ser ...
- [转]Qt 5.5 操作 Excel 的速度 效率问题
转自:http://blog.csdn.net/li494816491/article/details/50274305 1. QAxObject *_excelObject1 =newQAxObje ...
- 介绍三款大前端UI框架
一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...
- Linux之基础命令——文件搜索
grep(匹配符合条件的字符串) 无参:显示匹配行 -c:显示匹配行数 -e 字符串:匹配特殊字符串,如-开头 -i:忽略大小写 -v:输出不匹配行 -w:匹配指定字符串 可以和别的命令通过" ...