HDU-2258-Continuous Same Game (1)(DFS+模拟)
来源:互联网 发布:g76螺纹编程第一刀 编辑:程序博客网 时间:2024/05/19 22:49
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.
思路:DFS找最大的联通块消去。注意向左移动的时候连续两列都为空的情况。
#include <stdio.h>int n,m,total,nxt[4][2]={{0,-1},{-1,0},{1,0},{0,1}};bool vis[20][20];char d[20][21];void dfs(int x,int y,int num){ for(int i=0;i<4;i++) { x+=nxt[i][0]; y+=nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && d[x][y]==num) { vis[x][y]=1; total++; dfs(x,y,num); } x-=nxt[i][0]; y-=nxt[i][1]; }}void tran(int x,int y,int num){ for(int i=0;i<4;i++) { x+=nxt[i][0]; y+=nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && d[x][y]==num) { d[x][y]='0'; tran(x,y,num); } x-=nxt[i][0]; y-=nxt[i][1]; }}int main(){ int i,j,k,mx,x,y,ans,remain,t; while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++) scanf("%s",d[i]); ans=0; remain=n*m; while(remain) { mx=0; for(i=0;i<n;i++) for(j=0;j<m;j++) vis[i][j]=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(d[i][j]>'0' && !vis[i][j]) { vis[i][j]=1; total=1; dfs(i,j,d[i][j]); if(total>mx) { mx=total; x=i; y=j; } } } } remain-=mx; ans+=mx*(mx-1); tran(x,y,d[x][y]); d[x][y]='0'; for(i=n-1;i>=0;i--)//向下移动 { for(j=0;j<m;j++) { if(d[i][j]=='0') { for(k=i-1;k>=0;k--) { if(d[k][j]>'0') { d[i][j]=d[k][j]; d[k][j]='0'; break; } } } } } t=m-1; while(t--)//向左移动,注意连续两列都为空的情况 { for(j=0;j<m-1;j++) { for(i=0;i<n;i++) if(d[i][j]>'0') break; if(i==n) { for(i=0;i<n;i++) { d[i][j]=d[i][j+1]; d[i][j+1]='0'; } } } } } printf("%d\n",ans); }}
51 0
- HDU-2258-Continuous Same Game (1)(DFS+模拟)
- HDU 2258 Continuous Same Game (1) (dfs+模拟)
- hdoj 2258 Continuous Same Game (1) (DFS+模拟)
- HDU-2259-Continuous Same Game (2)(BFS+DFS+模拟)
- HDOJ-2258 Continuous Same Game (1) dfs
- ZOJ 3048 (HDU 2258) Continuous Same Game (1)
- hdu2258 Continuous Same Game (1)
- ZJU_3048 Continuous Same Game
- 【模拟】POJ_1027_The Same Game
- poj1027--The Same Game(模拟)
- The Same Game-POJ1027模拟
- hdu 3259 An interesting mobile game (dfs+bfs+模拟)
- poj1027 The Same Game (简单模拟题)
- POJ 1027 The Same Game 模拟题
- poj 1027 The Same Game(大模拟)
- poj 1027 The Same Game 模拟
- HDU 3699--DFS+模拟
- HDU 3696 - Farm Game (DFS)
- poj 1061 青蛙的约会 数论
- 用开放地址法中的线性探查法解决冲突实现哈希表的运算
- jmap详解
- javascript实现获取json数据分页功能
- TX电话面试小结
- HDU-2258-Continuous Same Game (1)(DFS+模拟)
- C++中两个类的头文件互相包含问题
- Java中的FileOutputStream与FileInputStream
- 字符串运算
- Oracle遇到错误
- 00.设计模式导读
- object-c 学习 知识点总结
- poj 2115 C Looooops 数论
- Linux程序设计学习笔记----多线程编程基础概念与基本操作