Continuous Same Game (1)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 558 Accepted Submission(s): 241
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 5 35552 31154 33222 21134 12314
Sample Output
32Hint35552 00552 00002 00002 00000 00000 31154 05154 05104 00004 00002 00000 33222 01222 01222 00122 00104 00100 21134 21134 21134 25234 25234 25230 12314 12314 12314 12314 12314 12312 The total point is 12+6+6+2+6=32.思路:题目并不难,就是模拟消消乐感觉,有思路慢慢敲就行了。。代码主要有三部分构成:1.findmaxBlocks()查找,深搜查找最大块2.deleteBlocks()消除,把最大色块所包含的位置全部置为03.refresh() 刷新,按题目原则,有一整列消除,则该侧左边的色块全部右移补上,注意不是左移。。。代码有点乱,没怎么整理和优化。。#include<stdio.h> #include<vector> #include<queue> #include<string.h> using namespace std; int v[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int map[25][25]; int n,m; bool vis[25][25]; int count; struct Point { int x, y; Point(int _x,int _y) { x=_x; y=_y; } }; struct Node{ queue<Point> q;//存放num个坐标信息 int x,y; int num; Node(){} Node(int _num){ num=_num; } bool friend operator<(Node a,Node b) { if(a.num==b.num){//如果有两个色块结点数相等 if(a.x==b.x)//起始点的x坐标相同 return a.y>b.y;//返回 y小的在先 else return a.x>b.x;//x不相同,x小的在先 } return a.num<b.num;//排序,节点多的在先 } }; priority_queue<Node> q;//用来存放已找到的色块信息,并排序 queue<Point> con; //只做一个公共容器来用 //找出最大颜色相同块 void find(int x,int y)//查找颜色连续块 { char cur=map[x][y];//起始位置值 // printf("%d %d %d",x,y,cur); for(int i=0;i<4;i++) { int xx=x+v[i][0]; int yy=y+v[i][1]; if(vis[xx][yy]||map[xx][yy]!=cur||xx<0||xx>=n||yy<0||yy>m) continue; vis[xx][yy]=true;//标记 count++;//数量++ con.push(Point(xx,yy));//若为相同色块压入队列 find(xx,yy);//继续查找 } } void findmaxBlocks()//保存块数信息,并排序 { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(vis[i][j]||map[i][j]=='0') continue; count=1; while(!con.empty()) con.pop();//初始化 con.push(Point(i,j));//初始节点压入队列 vis[i][j]=true; find(i,j); Node node; node.num=count; node.q=con; node.x=i;//保存色块起始位置用来,当有两个或两个以上最大色块时,用来排序 node.y=j; if(count!=1) q.push(node);//将所查找的色块信息压入队列 } } } //消除 void deleteBlocks(Node node) { con=node.q; while(!con.empty()) { Point po=con.front(); con.pop(); map[po.x][po.y]='0'; } } //恢复 void resetRow(int x,int y)//递归恢复,当发现下方有空缺,则往下补一个位置, { if(x>=n-1) return ;//设置递归结束条件,到最后一行停止 if(map[x+1][y]=='0') { char temp; temp=map[x][y]; map[x][y]='0'; map[x+1][y]=temp; resetRow(x+1,y); } } void resetColumn(int x)//右移 { for(int j=x;j>0;j--)//列 { for(int i=0;i<n;i++)//行 { char temp=map[i][j]; map[i][j]=map[i][j-1]; map[i][j-1]=temp; } } } void refresh() { //行补充 for(int i=n-1;i>=0;i--)//由最下层开始,往上依次补充 { for(int j=0;j<m;j++) { resetRow(i,j); } } //列补充 for(int i=0;i<m;i++) { int flag=true; for(int j=0;j<n;j++) //检测第i+1列是否全部为0 { if(map[j][i]!='0') { flag=false; break; } } if(flag) resetColumn(i); } } int main() { // Node node; // node.x=1; // node.num=5; // node.y=5; // q.push(node); // // node.x=9; // node.num=5; // node.y=1; // q.push(node); // node=q.top(); // printf("%d",node.y); while(scanf("%d%d",&n,&m)!=EOF) { // memset(map,0,sizeof(map));//初始化map数组 getchar(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&map[i][j]); } getchar(); } int sum=0; while(true)//重复查找,消除,恢复过程,直到找不到可以消除的色块 { memset(vis,false,sizeof(vis));//查找前初始化标志数组 findmaxBlocks();//查找最大色块 if(q.empty()) break;//没有可以消除的色块推出循环 Node node=q.top();//取出保存最大色块信息的node节点 sum+=node.num*(node.num-1);//计分 con=node.q;//输出当前要消除的最大色块所包含的节点 while(!q.empty()) q.pop();//清空q,准备下一次查找 // printf("q.size==%d ",q.size()); // printf("num==%d con.size==%d\n",node.num,con.size()); // while(!con.empty()) // { // Point po=con.front(); // con.pop(); // printf("%d %d %c\n",po.x,po.y,map[po.x][po.y]); // } deleteBlocks(node);//消除最大色块 refresh();//刷新map // for(int i=0;i<n;i++) // { // for(int j=0;j<m;j++) // { // printf("%c",map[i][j]); // } // printf("\n"); // } } printf("%d\n",sum); } return 0; } /* 5 8 35414552 31424154 33434222 21414134 12454314 8 8 11223344 55112233 44551122 33445511 22334455 11223344 55112233 44551122 9 9 111222333 444555111 222333444 555111222 333444555 111222333 444555111 222333444 555111222 5 5 12345 54321 11122 22333 12345 6 6 112233 222333 444441 221111 555222 222211 8 8 12345432 12345321 32145231 13542311 23541351 13135421 31354212 31354132 */