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

  1. 5 6
  2. 0 0 0 3 4 4
  3. 0 1 1 3 3 3
  4. 2 2 1 2 3 3
  5. 1 1 1 1 3 3
  6. 2 2 1 4 4 4

Sample Output

  1. 4

Source

 
解题:IDA*,每次优先选择能使消除数目多的颜色进行搜索。写的第一道IDA*题目。。。。。IDA*不要存储状态,真是极好的
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <climits>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <queue>
  10. #define LL long long
  11. #define INF 0x3f3f3f
  12. using namespace std;
  13. int table[][],r,c,ans,cnt;
  14. const int dir[][] = {,-,,,-,,,};
  15. bool vis[][];
  16. struct node{
  17. int x,y;
  18. };
  19. struct po{
  20. int x,y,c;
  21. };
  22. bool check(int (*t)[]) {
  23. for(int i = r; i ; i--) {
  24. for(int j = ; j <= c; j++)
  25. if(t[i][j]) return false;
  26. }
  27. return true;
  28. }
  29. bool cmp(const po &a,const po &b){
  30. return a.c > b.c;
  31. }
  32. void calc(int (*t)[],int x,int y,const int color){
  33. vis[x][y] = true;
  34. cnt++;
  35. for(int i = ; i < ; i++){
  36. int px = x+dir[i][];
  37. int py =y+dir[i][];
  38. if(!vis[px][py] && t[px][py] == color)
  39. calc(t,px,py,color);
  40. }
  41. }
  42. void cancle(int (*t)[],int x,int y,const int color){
  43. bool vis[][] = {false};
  44. queue<node>q;
  45. vis[x][y] = true;
  46. int i,j,px,py;
  47. t[x][y] = ;
  48. q.push((node){x,y});
  49. while(!q.empty()){
  50. node temp = q.front();
  51. q.pop();
  52. for(i = ; i < ; i++){
  53. px = temp.x+dir[i][];
  54. py = temp.y+dir[i][];//这个地方写成dir[i][0]害我调了很久
  55. if(!vis[px][py] && t[px][py] == color){
  56. vis[px][py] = true;
  57. t[px][py] = ;
  58. q.push((node){px,py});
  59. }
  60. }
  61. }
  62. }
  63. void shift(int (*t)[]){
  64. int i,j,k;
  65. bool isempty[] = {false};
  66. for(j = ; j <= c; j++){
  67. for(i = r-; i; i--){
  68. for(k = i;k < r &&!t[k+][j]&&t[k][j]; k++)
  69. swap(t[k+][j],t[k][j]);
  70. }
  71. if(!t[r][j]) isempty[j] = true;
  72. }
  73. for(j = c-; j; j--){
  74. if(isempty[j]){
  75. for(i = ; i <= r; i++){
  76. for(k = j; k <= c; k++)
  77. t[i][k] = t[i][k+];
  78. }
  79. }
  80. }
  81. }
  82. bool dfs(int (*t)[],int step){
  83. int mp[][],i,j,m = ;
  84. bool flag;
  85. if(check(t) && ans == step) return true;
  86. if(step > ans) return false;
  87. po p[];
  88. memset(vis,false,sizeof(vis));
  89. for(i = r; i; i--){
  90. flag = true;
  91. for(j = ; j <= c; j++){
  92. if(t[i][j]) flag = false;
  93. if(t[i][j] && !vis[i][j]){
  94. cnt = ;
  95. calc(t,i,j,t[i][j]);
  96. p[m++] = (po){i,j,cnt};
  97. }
  98. }
  99. if(flag) break;
  100. }
  101. sort(p,p+m,cmp);
  102. for(i = ; i < m; i++){
  103. memcpy(mp,t,sizeof(mp));
  104. cancle(mp,p[i].x,p[i].y,t[p[i].x][p[i].y]);
  105. shift(mp);
  106. if(dfs(mp,step+)) return true;
  107. }
  108. return false;
  109. }
  110. int main() {
  111. int i,j;
  112. while(~scanf("%d %d",&r,&c)) {
  113. memset(table,,sizeof(table));
  114. for(i = ; i <= r; i++)
  115. for(j = ; j <= c; j++)
  116. scanf("%d",table[i]+j);
  117. if(check(table)) {puts("");continue;}
  118. for(ans = ;; ans++)
  119. if(dfs(table,)) break;
  120. printf("%d\n",ans);
  121. }
  122. return ;
  123. }

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. UVa 1220 Party at Hali-Bula 晚会

    #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #i ...

  2. Clone a Pluggable Database – 12c Edition

    1. 1.Tnsnames when connecting to either Container or Pluggable instance The tnsnames.ora should be c ...

  3. 关于html/css的路径问题

    非原创,转自:http://blog.sina.com.cn/s/blog_6c21f6480101cb33.html [问题描述]: 比如你有Web项目solo,假如目录结构如下: 在cy.css中 ...

  4. 493 Reverse Pairs 翻转对

    给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...

  5. FFmpegUtil

    这几天没事研究音频玩 比如 X配音app的配音功能 录一段 pcm或者wav格式的文件 替换mp4指定位置的音频刚开始卡在 pcm混合以及pcm指定位置插入.思路 一段段的视频进行切割 用到MP4Co ...

  6. spring mvc支持跨域请求

    @WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...

  7. c# sqlserver连接字符串

    odbc: string cnnstring = @"Driver={SQL Server Native Client 11.0};Initial Catalog = sxquadb;ser ...

  8. [转]Qt 5.5 操作 Excel 的速度 效率问题

    转自:http://blog.csdn.net/li494816491/article/details/50274305 1. QAxObject *_excelObject1 =newQAxObje ...

  9. 介绍三款大前端UI框架

    一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...

  10. Linux之基础命令——文件搜索

    grep(匹配符合条件的字符串) 无参:显示匹配行 -c:显示匹配行数 -e 字符串:匹配特殊字符串,如-开头 -i:忽略大小写 -v:输出不匹配行 -w:匹配指定字符串 可以和别的命令通过" ...