Codeforces Gym-101291G Gravity

http://codeforces.com/gym/101291/attachments
Gravity
You would like to implement a simple simulation of gravity on a 2-dimensional grid, consisting ofn rows and m columns.

Some grid cells may contain obstacles, some may contain a single apple, and all others are empty.

The following rules are followed until no further changes are possible:

• The obstacles do not move.
• Whenever there is an empty cell immediately below an apple, the apple moves into the emptycell.

Find the final configuration of the grid after all apples have settled.

Input

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 50).Each of the next n lines contains m characters, describing the board, from top to bottom. Obstaclesare denoted as ‘#’, apples are denoted as ‘o’, and empty cells are denoted as ‘.’.

Output

Print, on n lines, the final configuration of the grid after executing the rules stated above.

Sample Input

3 3
ooo
#..
..#

Sample Output

o..
#.o
.o#

Sample Input

4 2
oo
oo
o.
..

Sample Output

..
o.
oo
oo
#include <stdio.h>
#include <string.h>
int main()
{
    char a[55][55];
    int n,m,i,j;
    scanf("%d %d",&n,&m);
    for(i=0; i<n; i++)
    {
        getchar();
        for(j=0; j<m; j++)
        {
            scanf("%c",&a[i][j]);
        }
    }
    for(i=n-2; i>=0; i--)
    {
        for(j=m-1; j>=0; j--)
        {
            int c=i,d=j;
            while((a[c][d]=='o')&&(a[c+1][d]=='.')&&(c<n-1))
            {
                a[c][d]='.';
                c++;
                a[c][d]='o';
            }
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            printf("%c",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值