1 / 14

The same game 解题报告

The same game 解题报告. 00001097 吴明辉 北京大学数学科学学院. The same game 解题报告. 题目重述 算法设计 主要数据 实现 优化. The same game 题目重述.

Download Presentation

The same game 解题报告

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The same game 解题报告 00001097 吴明辉 北京大学数学科学学院

  2. The same game 解题报告 • 题目重述 • 算法设计 • 主要数据 • 实现 • 优化

  3. The same game 题目重述 • The game named "Same" is a single person game played on a 10 \Theta 15 board. Each square contains a ball colored red (R), green (G), or blue (B). Two balls belong to the same cluster if they have the same color, and one can be reached from another by following balls of the same color in the four directions up, down, left, and right. At each step of the game, the player chooses a ball whose cluster has at least two balls and removes all balls in the cluster from the board. Then, the board is "compressed" in two steps: 1. Shift the remaining balls in each column down to fill the empty spaces. The order of the balls in each column is preserved. 2. If a column becomes empty, • shift the remaining columns • to the left as far as possible. • The order of the columns is • preserved. For example, • choosing the ball at the • bottom left corner in the • sub-board below causes:

  4. The same game 题目重述 • The objective of the game is to remove every ball from the board, and the game is over when every ball is removed or when every cluster has only one ball. The scoring of each game is as follows. The player starts with a score of 0. When a cluster of m balls is removed, the player's score increases by (m-2)^2 . A bonus of 1000 is given if every ball is removed at the end of the game. You suspect that a good strategy might be to choose the ball that gives the largest possible cluster at each step, and you want to test this strategy by writing a program to simulate games played using this strategy. If there are two or more balls to choose from, the program should choose the leftmost ball giving the largest cluster. If there is still a tie, it should choose the bottommost ball of these leftmost balls.

  5. The same game 算法设计 • 这是一道模拟类的题目 • 需要存储的如果按照题目所说的完整的步骤模拟共分为以下几步: • 1.对于每一个点进行图的遍历,统计所在的cluster的大小。 • 2.选出最大的cluster,把它move掉 • 3.按照题目所说进行压缩

  6. The same game 主要数据 • 根据算法要求需要存储的数据有 • 1.char map[10][15] 用来存储每一个点的颜色属性 • 2.bool visit[10][15] 用来存储每一个点在图的遍历时的访问状态 • 3.int cluster[10][15] 用来存储每一个点所在的cluster的大小

  7. The same game 程序实现 • 图的遍历 • void visitall(int x,int y){ • visit[x][y]=1; • sum++; • if(x>0&&map[x-1][y]==map[x][y]&&visit[x-1][y]==0)visitall(x-1,y); • if(y>0&&map[x][y-1]==map[x][y]&&visit[x][y-1]==0)visitall(x,y-1); • if(x<9&&map[x+1][y]==map[x][y]&&visit[x+1][y]==0)visitall(x+1,y); • if(y<14&&map[x][y+1]==map[x][y]&&visit[x][y+1]==0)visitall(x,y+1); • } • 递归结束后全局变量sum的值为(x,y)所在cluster的大小

  8. The same game 程序实现 • 选出最大的cluster • for(j=0;j<15;j++){ • for(i=0;i<10;i++){ • cal(i,j); • if(cluster[i][j]>cluster[x][y]){ • x=i; • y=j; • } • } • }

  9. The same game 程序实现 • 移除选定的cluster 并且压缩 • cal(x,y); • for(i=0;i<10;i++)for(j=0;j<15;j++)if(visit[i][j]==1)map[i][j]='0'; • for(i=0;i<15;i++){ • for(j=0;j<9;j++){ • for(k=j+1;k<10;k++){ • if(map[j][i]!='0')break; • if(map[k][i]!='0'){ • map[j][i]=map[k][i]; • map[k][i]='0'; • break; • } • } • } • }

  10. The same game 程序实现 • for(i=0;i<14;i++){ • if(map[0][i]!='0')continue; • for(j=i+1;j<15;j++){ • if(map[0][j]!='0'){ • for(k=0;k<10;k++){ • map[k][i]=map[k][j]; • map[k][j]='0'; • } • break; • } • } • }

  11. The same game 程序优化 • 1.不是所有的点都要进行图的遍历 • 2.如果一边遍历一边比较大小,则可以不需要数组cluster[10][15]; • 3.压缩算法优化

  12. The same game 程序优化 • pair<int,int> maxpair(){ • int x=0,y=0,i,j; • tempcluster=0; • pair<int,int> temp; • for(i=0;i<10;i++)for(j=0;j<15;j++)visit[i][j]=0; • for(j=0;j<15;j++){ • for(i=0;i<10;i++){ • if(visit[i][j]==1)continue; • cal(i,j); • if(sum>tempcluster){ • x=i; • y=j; • tempcluster=sum; • } • } • } • temp.first=x; • temp.second=y; • return temp; • }

  13. The same game 程序优化 • for (k=0, j=0; k<15; k++) • { • u=0; v=0; • while (v<=9) • { • while (v<=9&&map[v][k]=='0') • v++; • while (v<=9&&map[v][k]!='0') • { • if (v>u||j<k) • { • map[u][j]=map[v][k]; • map[v][k]='0'; • } • u++; v++; • } • } • if (u>0) • j++; • }

  14. The same game 结束 • 总结: • 在模拟类的程序中,有时算法是没法有太多的变化的。但只要你仔细研究会发现有许多操作时没必要的,优化的潜力很大。这一点edge detection也可以很好的说明。 • That’s all, • Thank you !

More Related