蛇形填数

来源:互联网 发布:js点击缓慢移动 编辑:程序博客网 时间:2024/06/05 04:41

Ø 描述

Problem K : Treasure Map

时间限制:3 Sec  内存限制: 128 MB

题目描述

DescriptionThere 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.

 

 

InputThere are several test cases, each one contains two lines.

First line: two integers N and M (1 ≤ N, M ≤ 100), indicate the size of the map.

Second line: N × M integers, indicate the weight of the treasures in order. OutputFor 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

321 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

Ø 解题思路

定义四个变量,up,down,left,right作为矩阵不可逾越的边界,然后在界限内进行填数。

Ø 翻译:

DescriptionThere是一个机器人,它的任务是把财宝埋在N×M网格地图上,每一个宝贝都可以由它的重量,这是一个整数。

机器人开始埋葬的宝藏左上的网格。因为它是愚蠢的,它只能直走直到边界或下一个网格已经埋宝藏,然后结果是正确的。

它的任务是完成当所有宝藏埋在这里。请输出藏宝图N×M矩阵。

InputThere几个测试用例,每一个包含两行。

第一行:两个整数N和M(1≤N≤100),显示地图的大小。

第二行:N×M整数,表示珍宝的重量。OutputFor每个测试用例,输出一个N×M矩阵包含宝藏埋在机器人的重量。两个整数之间有一个空间。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 102;
int str[maxn*maxn];
int str1[maxn][maxn];
int k,i,j;
int main()
{
    
int m,n;
    
while(scanf("%d %d",&n,&m)!=EOF)
    {
        
int up=0,right=m,down=n,left=0;         
                                                
        
for(k=0;k<n*m;k++)
            scanf(
"%d",&str[k]);
        k=
0;
        
while(1)
        {
            
if(k<n*m)    //向右填数;判断是否继续
            {
                
for(j=left;j<right;j++)
                {
                    str1[up][j]=str[k++];
                }
                up=up+
1;
            }
            
else    break;
            
if(k<n*m)    //向下填数;判断是否继续
            {
                
for(i=up;i<down;i++)
                {
                    str1[i][right-
1]=str[k++];
                }
                right=right-
1;
            }
            
else    break;
            
if(k<n*m)    //向左填数;判断是否继续
            {
                
for(j=right-1;j>=left;j--)
                {
                    str1[down-
1][j]=str[k++];
                }
                down=down-
1;
            }
            
else    break;
            
if(k<n*m)    //向上填数;判断是否继续
            {
                
for(i=down-1;i>=up;i--)
                {
                    str1[i][left]=str[k++];
                }
                left=left+
1;
            }
            
else    break;
        }
        
for(i=0;i<n;i++)            //打印数组;
        {
            
for(j=0;j<m;j++)
            {
                
if(j<m-1)
                    printf(
"%d ",str1[i][j]);
                
else
                    printf(
"%d\n",str1[i][j]);
            }
        }
    }
    
return 0;
}

原创粉丝点击