hdoj 2258 Continuous Same Game (1) (DFS+模拟)
来源:互联网 发布:域名搜索引擎 编辑:程序博客网 时间:2024/06/03 02:28
没什么意思,,根据题意模拟出过程就好。。
Continuous Same Game (1)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 412 Accepted Submission(s): 145
Problem Description
Continuous Same Game is a simple game played on a grid of colored blocks. Groups of two or more connected (orthogonally, not diagonally) blocks that are the same color may be removed from the board. 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. Points are scored whenever a group of blocks is removed. The number of points per block increases as the group becomes bigger. When N blocks are removed, N*(N-1) points are scored.
LL was interested in this game at one time, but he found it is so difficult to find the optimal scheme. So he always play the game with a greedy strategy: choose the largest group to remove and if there are more than one largest group with equal number of blocks, choose the one which contains the most preceding block ( (x1,y1) is in front of (x2,y2) if and only if (x1<x2 || x1==x2 && y1<y2) ). Now, he want to know how many points he will get. Can you help him?
LL was interested in this game at one time, but he found it is so difficult to find the optimal scheme. So he always play the game with a greedy strategy: choose the largest group to remove and if there are more than one largest group with equal number of blocks, choose the one which contains the most preceding block ( (x1,y1) is in front of (x2,y2) if and only if (x1<x2 || x1==x2 && y1<y2) ). Now, he want to know how many points he will get. Can you help him?
Input
Each test case begins with two integers n,m ( 5<=n,m<=20 ), which is the size of the board. Then n lines follow, each contains m characters, indicating the color of the block. There are 5 colors, and each with equal probability.
Output
For each test case, output a single line containing the total point he will get with the greedy strategy.
Sample Input
5 53555231154332222113412314
Sample Output
32Hint35552 00552 00002 00002 00000 0000031154 05154 05104 00004 00002 0000033222 01222 01222 00122 00104 0010021134 21134 21134 25234 25234 2523012314 12314 12314 12314 12314 12312The total point is 12+6+6+2+6=32.
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 25;char mp[N][N];int vis[N][N];int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};int n, m;int cnt;void dfs1(int x, int y, char c){ vis[x][y] = 1; cnt++; for(int i = 0; i < 4; i++){ int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(mp[xx][yy] == c && !vis[xx][yy]) dfs1(xx, yy, c); }}void dfs2(int x, int y, char c){ mp[x][y] = 0; for(int i = 0; i < 4; i++){ int xx = x + dir[i][0]; int yy = y + dir[i][1]; if(mp[xx][yy] == c) dfs2(xx, yy, c); }}int main(){ while(scanf("%d%d", &n, &m) != EOF){ memset(mp, '\0', sizeof(mp)); for(int i = 1; i <= n; i++){ scanf("%s", mp[i] + 1); for(int j = 1; j <= m; j++){ if(mp[i][j] == '0') mp[i][j] = 0; } } int ans = 0; while(true){ memset(vis, 0, sizeof(vis)); int Max = -1, mx, my; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++){ if(mp[i][j] && !vis[i][j]){ cnt = 0; dfs1(i, j, mp[i][j]); // cout << "cnt = " << cnt << endl; if(cnt > Max){ mx = i; my = j; Max = cnt; } } } if(Max < 2) break; // cout << "Max = " << Max << endl; ans += Max * (Max - 1); dfs2(mx, my, mp[mx][my]); for(int j = 1; j <= m; j++){ int k, flag = 0; if(mp[n][j]) flag = 1; for(int i = n - 1; i > 0; i--){ if(mp[i][j]){ flag = 1; k = i; while(k < n && mp[k + 1][j] == 0){ mp[k + 1][j] = mp[k][j]; mp[k][j] = 0; k++; } } } if(flag) continue; for(int i = 1; i <= n; i++) for(k = j; k > 0; k--) mp[i][k] = mp[i][k - 1]; } } printf("%d\n", ans); } return 0;}
0 0
- hdoj 2258 Continuous Same Game (1) (DFS+模拟)
- HDOJ-2258 Continuous Same Game (1) dfs
- HDU-2258-Continuous Same Game (1)(DFS+模拟)
- HDU 2258 Continuous Same Game (1) (dfs+模拟)
- HDU-2259-Continuous Same Game (2)(BFS+DFS+模拟)
- hdu2258 Continuous Same Game (1)
- ZOJ 3048 (HDU 2258) Continuous Same Game (1)
- ZJU_3048 Continuous Same Game
- 【模拟】POJ_1027_The Same Game
- poj1027--The Same Game(模拟)
- The Same Game-POJ1027模拟
- poj1027 The Same Game (简单模拟题)
- POJ 1027 The Same Game 模拟题
- poj 1027 The Same Game(大模拟)
- poj 1027 The Same Game 模拟
- POJ 1027 The Same Game棋盘游戏(模拟)
- CSUOJ 1862 The Same Game 模拟消消乐
- 文章标题CSU 1862: The Same Game(模拟)
- 对C语言中的static关键字的深入理解
- 去除字符串末尾的空格的C语言实现
- 英文吵架必备100句·
- ORACLE局部索引和全局索引
- 异常中断_1
- hdoj 2258 Continuous Same Game (1) (DFS+模拟)
- servlet对象之ServletConfig对象
- JSP和Servlet的区别
- tomcat7用户配置自用
- 异常中断_2
- iOS -- 地图画线,画线区域内的经纬度坐标点添加大头针
- LINUX的IIC驱动从这开始(四)
- 增强版内容轮播器(jQuery.flexslider.js的应用)
- 使用Xpath对XML进行模糊查询