acm题目及我的程序(1)——ten drops(十滴水游戏)

前一阵子研究算法时,在网上看到一道acm的题目,觉得挺有意思,贴出来请朋友们指正。题目可以参看:http://acm.zju.edu.cn/show_problem.php?pid=2902

=========================================================================

Have you ever played the flash game "10 drops" before?

I have tried this game several times, but I am not good at playing this game because of my bad imagination. So I come to you, one of the best programmers in the world, for help. The target is to find the status of the grid after several given actions. Do you think that is too easy? But, to me, it is an impossible mission. So, if you help me to solve it, I'll appreciate it.

 

 INSTRUCTIONS:
    You start with m drops in your tank;
    Use them to grow blobs until they burst;
    We use a number 0 <= x < k to describe the size of the blob: 0 indicate there is no blobs, and a blob with a given size k will burst. Once a drop attach to a blob, the size of it will increase by 1. The burst blob will release four drops moving in four directions, and they move in the same speed.
    If a drop moves to a grid which contains a blob, the blob will grow. What's more, if more than one drops move to the same grid simultaneously, you can assume that the one comes from north (up) attaches the blob first, then south second, west third and east last.
    An action is a left click in some grid in the game (the left top one is (0,0), and (0,1) is to the east of (0,0)). A left click indicates that you use 1 drop in your tank (if it's not empty) to grow the blob in that grid. Of cause, a left click in grid without blob should be ignored because it's meaningless.

Input

There are multiple test cases. Each case begins with a line containing four integer n, m, k, p where n < 20 indicates the size of the grid is n * n; m <= 1000 is the number of rest drops in your tank; k <= 10 is the burst size of blob; and p is the number of actions. The next n lines each with n integers less than k. And then p lines each with 2 integers x, y stand for an action. Both x and y are less than n.

Process to the end of file.

Output

Print the number of left drops after p actions in tank in a line. Then print the description of the grid after p actions as the input shows.

Print a blank line between cases.

Sample Input

6 10 5 3
002243
423200
030132
132214
143213
403202
1 2
2 1
3 1
6 10 5 4
002243
423200
030132
132214
143213
403202
1 2
2 1
3 1
2 1

Sample Output

7
002243
424200
040132
142214
143213
403202

6
003243
000400
000232
304214
204213
403202

=========================================================================

 我写的源程序如下(为方便,没有将输入数据放在文件中处理):

/************************************************************************
 * 十滴水游戏栅格水滴变化程序
 *   点击栅格即加入水滴
 *   水滴数量达到k即破裂,破裂后朝四个方向各释放1个水滴
 *   释放的水滴运动到该栅格,若该栅格中有水滴,则加入,否则,继续按该方向运动
 ***********************************************************************
*/


#include 
< stdio.h >

#define  N 6

/************************************************************************
 * 加水滴
 *   (x,y)为欲加入水滴的栅格的坐标
 *   k: 水滴破裂时所含有的水滴数量
 *   m: 坛子里的水滴数量
 *   direction为水滴运动的方向
 *      0:表示点击该栅格
 *      1:释放的水滴向左运动
 *      2:释放的水滴向右运动
 *      3:释放的水滴向上运动
 *      4:释放的水滴向下运动
 ***********************************************************************
*/

void  drop( int  game[][N], int  x, int  y, int  k, int &  m, int  direction)
{
    
if(x<0 || y<0 || x>N-1 || y>N-1)
        
return;

    
if(direction==0)        //点击该栅格加入水滴
    {
        m
--;                //该水滴为坛子中的水滴
        if(game[x][y]<k)    //水滴数量<k-1时,直接加入,不会破裂
            game[x][y]++;
    }

    
else    //表示加入的水滴为破裂的水滴释放的水滴
    {
        
if(game[x][y]==0)        //该水滴按照原来的方向继续移动
        {
            
if(direction==1)
                drop(game,x
-1,y,k,m,1);    //go on moving to left
            else if(direction==2)
                drop(game,x
+1,y,k,m,2);    //go on moving to right
            else if(direction==3)
                drop(game,x,y
-1,k,m,3);    //go on moving to up
            else if(direction==4)
                drop(game,x,y
+1,k,m,4);    //go on moving to down
        }

        
else if(game[x][y]<k)    //水滴数量<k-1时,直接加入,不会破裂
            game[x][y]++;
    }


    
//若水滴满,则破裂,并释放水滴
    if(game[x][y]==k)
    
{
        game[x][y]
=0;    //水滴破裂

        drop(game,x
-1,y,k,m,1);    //release a drop moving to left
        drop(game,x+1,y,k,m,2);    //release a drop moving to right
        drop(game,x,y-1,k,m,3);    //release a drop moving to up
        drop(game,x,y+1,k,m,4);    //release a drop moving to down
    }

}


// 显示
void  display( int  game[][N])
{
    
for(int i=0;i<N;i++)
    
{
        
for(int j=0;j<N;j++)
            printf(
"%3d",game[i][j]);
        printf(
" ");
    }

}


void  main()
{
    
int n;    //栅格边长
    int m;    //坛子中的水滴数量
    int k;    //水滴破裂时含有的水滴数量
    int p;    //操作个数(操作:点击左键加入水滴)

    n
=N;
    m
=10;
    k
=5;
    
//p=3;
    p=4;

/*
    int game[N][N]={    //栅格中的水滴
        0,0,2,2,4,3,
        4,2,3,2,0,0,
        0,3,0,1,3,2,
        1,3,2,2,1,4,
        1,4,3,2,1,3,
        4,0,3,2,0,2
    };
*/

    
int game[N][N]={    //栅格中的水滴
        0,0,2,2,4,3,
        
4,2,3,2,0,0,
        
0,3,0,1,3,2,
        
1,3,2,2,1,4,
        
1,4,3,2,1,3,
        
4,0,3,2,0,2
    }
;

/*
    int action[3][2]={    //加入水滴时被加入水滴所在栅格的坐标
        1,2,
        2,1,
        3,1
    };
*/

    
int action[4][2]={    //加入水滴时被加入水滴所在栅格的坐标
        1,2,
        
2,1,
        
3,1,
        
2,1
    }
;

    printf(
"the original drops is:%d ",m);
    printf(
"the original game is: ");
    display(game);

    
for(int i=0;i<p;i++)
    
{
        drop(game,action[i][
0],action[i][1],k,m,0);    //点击该栅格
    }


    printf(
" the remain drops is:%d ",m);
    printf(
"the game after drop is: ");
    display(game);
}
 

你可能感兴趣的:(游戏,Integer,input,action,each,output)