2017上海金马五校程序设计竞赛 K:Treasure Map

Time Limit: 3 s

Description

There is a robot, its task is to bury treasures in order on a N × M grids map, and each treasure can be represented by its weight, which is an integer.

The robot begins to bury the treasures from the top-left grid. Because it is stupid, it can only Go straight until the border or the next grid has already been buried a treasure, and then it turns right.

Its task is finished when all treasures are buried. Please output the treasure map as a N × M matrix.

 

Input

There are several test cases, each one contains two lines.
First line: two integers N and M (1 ≤ NM ≤ 100), indicate the size of the map.
Second line: N × M integers, indicate the weight of the treasures in order.

 

Output

For each test case, output a N × M matrix contains the weight of the treasures buried by the robot. There is one space between two integers.

Sample Input

2 2
3 2 1 4
3 3
1 2 3 4 5 6 7 8 9

 

Sample Output

3 2
4 1
1 2 3
8 9 4
7 6 5
本题题意:

题目意思就是给你两个数N M,然后给你N*M个数,让你把这些数依次按照上边的方式填在N*M的表中。

解题思路:

这其实就是一道蛇形填数的题。注意判断上下左右的边界就好。


#include<iostream>  
#include<stdio.h>  
#include<string.h>  
#include<algorithm>  
#include<queue>  
using namespace std;  
  
const int maxn = 102;  
int N,M;  
int Map[maxn][maxn];  
int a[maxn*maxn];  
int main()  
{  
    int up,down,left,right;     ///分别记录上边界,下边界,左边界,右边界  
    while(~scanf("%d%d",&N,&M))  
    {  
        for(int i = 1; i <= N*M; i++)  
            scanf("%d",&a[i]);  
        up = 0;  
        down = N+1;  
        left = 0;  
        right = M+1;  
        int ccount = 1;  
        while(1)  
        {  
            ///先向右填数  
            if(up+1<down) ///必须要判断,上下边界相邻,不能填数了。同理左右边界相邻也不能填数了。  
            {  
                for(int i = left+1; i<right; i++)  
                {  
                    Map[up+1][i] = a[ccount++];  
                }  
                up++;  
            }  
            else break;  
            ///然后往下走  
            if(left<right-1)  
            {  
                for(int i = up+1; i < down; i++)  
                {  
                    Map[i][right-1] = a[ccount++];  
                }  
                right--;  
            }  
            else break;  
            ///然后往左走  
            if(up<down-1)  
            {  
                for(int i = right-1; i > left; i--)  
                {  
                    Map[down-1][i] = a[ccount++];  
                }  
                down--;  
            }  
            else break;  
            ///然后往上走  
            if(left+1<right)  
            {  
                for(int i = down-1; i > up; i--)  
                {  
                    Map[i][left+1] = a[ccount++];  
                }  
                left++;  
            }  
            else break;  
        }  
        for(int i = 1; i <= N; i++)  
        {  
            for(int j = 1; j <= M; j++)  
            {  
                if(j == 1) printf("%d",Map[i][j]);  
                else printf(" %d",Map[i][j]);  
            }  
            printf("\n");  
        }  
    }  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值