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


Problem K : Treasure Map


 (Out of Contest)

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
题目意思:

给出M和N,然后给出M*N个数,然后按照蛇形矩阵填数的方法,把数按顺序输出。

解题思路:

大一的时候就写过这样的题目。不管别人怎么写。我都是铁打不变的方法,定义四个变量,up,down,left,right作为矩阵不可逾越的边界,然后在界限内

进行填数。


#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;
}


终于把做出的题题解写完了,最近比赛有好多未解决的问题还都是难题,感觉好累啊,只能等有空慢慢啃了。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值