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 ...
随机推荐
- HAL之PWM
PWM是定时器的一个输出功能,要分配在有对应输出的管脚上.分频和定时值决定了周期,捕获寄存器的值就是占空比,当计数寄存器的值小于捕获值时输出固定电平(H),当大于时翻转电平,当计数器值溢出时将重载值载 ...
- [转]Intellij Idea自动添加注释的方法
Intellij Idea自动添加注释的方法 阿历Ali 关注 2017.08.20 21:22* 字数 914 阅读 2741评论 0喜欢 6 程序媛阿历终于要写第一篇关于开发的文章了!!! 阿历用 ...
- jQuery Validate自定义各种验证方法(转)
一.封装自定义验证方法-validate-methods.js /***************************************************************** j ...
- 如何正确理解关键字"with"与上下文管理器
转自:https://foofish.net/with-and-context-manager.html 如果你有阅读源码的习惯,可能会看到一些优秀的代码经常出现带有 “with” 关键字的语句,它通 ...
- VCS filelist 文件格式
VCS在运行仿真一般都会加仿真参数 –f filelist,filelist 是包含其他的仿真参数和整个工程的文件列表.具体格式如下: //file list format, just for exa ...
- Kali 2017.3开启VNC远程桌面登录
通过启用屏幕共享来开启远程桌面登录,开启后需要关闭encryption,否则会出现无法连接的情况.关闭encryption可以使用系统配置工具dconf来完成.所以先安装dconf-editor. 更 ...
- Tomcat和搜索引擎网络爬虫的攻防
不知道广大程序员朋友们注意到一个现象么?使用百度是无法搜索到淘宝网的网页.为什么会造成这种现象?这就要从网络爬虫说起了. 咱们程序员假如自己搭设个人网站,在上面分享少量自己的技术文章,面临的一个重要问 ...
- Android(java)学习笔记183:多媒体之图形颜色的变化
1.相信大家都用过美图秀秀中如下的功能,调整颜色: 2. 下面通过案例说明Android中如何调色: 颜色矩阵 ColorMatrix cm = new ColorMatrix(); paint.se ...
- DBMS的工作模式
数据库管理系统(DBMS)是指数据库系统中对数据进行管理的软件系统,它是数据库系统的核心组成部分,对数据库的一切操作(增删改查)都是通过DBMS进行的 DBMS的工作模式如下: 1>接受应用程序 ...
- Java面试题之HashSet 的实现原理?
HashSet 的实现原理?首先,我们需要知道它是Set的一个实现,所以保证了当中没有重复的元素.一方面Set中最重要的一个操作就是查找.而且通常我们会选择 HashSet来实现,因为它专门对快速查找 ...