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 ≤ N, M ≤ 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个数,然后按照蛇形矩阵填数的方法,把数按顺序输出。

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int N,M;
int Map[102][102];
int a[10009];
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;
}

2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)的更多相关文章

  1. 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)

    Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...

  2. 2017年上海金马五校程序设计竞赛:Problem G : One for You (博弈)

    Description Given a m × n chessboard, a stone is put on the top-left corner (1, 1). Kevin and Bob ta ...

  3. 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)

    Description Given a string S, which consists of lowercase characters, you need to find the longest p ...

  4. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)

    Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...

  5. 2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)

    Description Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dime ...

  6. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  7. 2017Summmer_上海金马五校 F题,G题,I题,K题,J题

    以下题目均自己搜 F题  A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...

  8. 算法竞赛入门经典第二版 蛇形填数 P40

    #include<bits/stdc++.h> using namespace std; #define maxn 20 int a[maxn][maxn]; int main(){ ; ...

  9. HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)

    题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题 题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...

随机推荐

  1. runtime总结 iOS

    Runtime的特性主要是消息(方法)传递,如果消息(方法)在对象中找不到,就进行转发,具体怎么实现的呢.我们从下面几个方面探寻Runtime的实现机制. Runtime介绍 Runtime消息传递 ...

  2. 圣思源Java视频36节练习源码分享(自己的190+行代码对比老师的39行代码)

    题目: * 随机生成50个数字(整数),每个数字范围是[10,50],统计每个数字出现的次数 * 以及出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来, * 如果某个数字出现次数为0, ...

  3. Android PopupWindow 疑难杂症之宽度WRAP_CONTENT

    一直以来都觉得 Android 中的 PopupWindow 不好用.主要有以下两点:1.宽度不好控制2.位置不好控制 今天单说第1点. 由于应用有好几种国家的语言,加上各设备宣染效果不完全一样,对p ...

  4. Wordpress 从 MySQL 获取文章链接 permalinks

    SELECT wpp.post_title, wpp.guid, wpp.post_date, REPLACE( REPLACE( REPLACE( REPLACE( wpo.option_value ...

  5. Oracle 11g 审计跟踪管理

    在Oracle11g之前,oracle数据库自带的审计功能是关闭的,考虑到性能和审计管理的复杂性,用户一般不打开审计功能.如果有审计要求,DBA会采用trigger来实现对DDL审计的方法来折中.例如 ...

  6. ardupilot_gazebo仿真(四)

    ardupilot_gazebo仿真(四) 标签(空格分隔): 未分类 Multi-MAV simulation 参考官网给出的multi-vehicle-simulation的方法 在每次打开sim ...

  7. ubuntu中 VI 方向键、删除键问题

    这两天重新装的ubuntu系统,发觉使用VI时,方向键按下去后变成ABCD,删除键无效.网上搜寻一番,应该是VI软件本身的问题,顾卸载重装即可,步骤如下: 1.执行命令 sudo apt-get re ...

  8. term "JavaScript"

    在Web浏览器上下文中理解的总称“JavaScript”包含几个非常不同的元素. 其中一个是核心语言(ECMAScript),另一个是Web API的集合,包括DOM(文档对象模型) JavaScri ...

  9. IDE API SDK JDK

    一.IDE 英文全称:Integrated Development Environment 中文名称:集成开发环境 本质:应用程序 功能:提供程序开发环境 组成:代码编辑器.编译器.调试器.图形用户界 ...

  10. C++STL——堆栈

    一.相关定义 原理:stack队列是一个线性存储表,插入和删除只在栈顶进行,从而构成了一个后进先出LIFO表. 入栈&出栈:元素的插入称为入栈,元素的删除称为出栈. stack是一种关联容器, ...