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

来源:互联网 发布:淘宝卖家都在哪里进货 编辑:程序博客网 时间:2024/05/18 10:24


Problem K : Treasure Map


From: DHUOJ, 2017060311
 (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 23 2 1 43 31 2 3 4 5 6 7 8 9

 

Sample Output

3 24 11 2 38 9 47 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