An interesting mobile game

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3295
64-bit integer IO format: %I64d      Java class name: Main

 
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

Source

 
解题:IDA*,每次优先选择能使消除数目多的颜色进行搜索。写的第一道IDA*题目。。。。。IDA*不要存储状态,真是极好的
 
 #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的更多相关文章

  1. 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 ...

  2. 【HDOJ】3295 An interesting mobile game

    其实就是一道搜索模拟题.因为数据量小,用char就够了. /* 3295 */ #include <iostream> #include <cstdio> #include & ...

  3. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  4. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  5. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  6. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  7. xtu summer individual 2 D - Colliders

    Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  8. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

  9. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

随机推荐

  1. ASP.NET Web API 2 框架揭秘

    这不是一本传统意义上的入门书籍 任何 —本书都具有对应的受众群体,所以我不得不将这句话放在最前面,并且希望所有 打算购买此书的读者能够看到.如果你之前对As氵NET W山API(或者AsPNET MⅤ ...

  2. elasticsearch6安装head插件

    1.head 插件Github地址:https://github.com/mobz/elasticsearch-head 2.npm install 3.npm run start 由于head插件监 ...

  3. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  4. hihocoder1736 最大的K-偏差排列

    思路: 容易写错的贪心题. 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, k; while ( ...

  5. 微信小程序 逻辑层

    1. 注册程序小程序APP在小程序的根目录下有一个app.js文件.有App(Object),App() 函数用来注册一个小程序.接受一个 Object 参数,其内便是小程序的生命周期.App() 必 ...

  6. android控件之webview和js与java交互

    首先添加权限:<uses-permission android:name="android.permission.INTERNET"/> 布局文件: <Relat ...

  7. 《Python基础教程》 读书笔记 第五章(下)循环语句

    5.5.1while循环 x=1 while x<=100: print x x+=1 确保用户输入了名字: name="" while not name: name=raw ...

  8. ubuntu下安装apcu扩展

    apcu前身是apc,apc分为系统缓存和用户缓存 1.系统缓存是指PHP执行时增加缓存,减少PHP文件的反复检查和编译,从而达到系统加速的目的. 2.用户缓存是指,PHP代码中将数据写入缓存,是用户 ...

  9. AJAX中文乱码解决方案

    通过AJAX获取数据中文乱码解决方案: @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到 ...

  10. Android(java)学习笔记165:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...