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. SharedPreferences Android

    类似iOS的NSUserDefaults,采用key-value(键值对)形式,主要用于轻量级的数据存储 public class MainActivity extends AppCompatActi ...

  2. Anytime项目开发记录4

    做事情列表,我在程序中命名为“正在做”. 这是一个Fragment,应用的主页面,由一个MainActivity加上DoingListFragment和PersonFragment组成.PersonF ...

  3. 小议Android多进程以致Application多次初始化

    最近遇到一个bug,当应用加了多进程后,比如总共进程数为N,会出现在`startService()`时`onStartCommand()`方法会被重复调用`(N-1)`次的奇怪现象. ***## 祸起 ...

  4. Kotlin的属性委托:无上下文情况下Android的赋值(KAD 15)

    作者:Antonio Leiva 时间:Mar 9, 2017 原文链接:https://antonioleiva.com/property-delegation-kotlin/ 如我们在前面文章中读 ...

  5. HDU 4587 TWO NODES(割点)(2013 ACM-ICPC南京赛区全国邀请赛)

    Description Suppose that G is an undirected graph, and the value of stab is defined as follows: Amon ...

  6. Python调用MySQL的一些用法小结

    目标:1个excel表内容导入到数据库中,例如:原始excel文件为 aaa.xls 首先:将aaa.xls 转换成aaa.txt ,注意当文件中含有中文字符时,可以通过notepad++打开,在“格 ...

  7. 软工实践 - 第二十一次作业 BETA 版冲刺前准备

    软工 · BETA 版冲刺前准备(团队) 过去存在的问题 组员之间缺乏沟通,前后端缺乏沟通协作 组员积极性不高 基础知识不够扎实 手动整合代码效率过低 我们已经做了哪些调整/改进 通过会议加强组员之间 ...

  8. PAT 甲级 1036 Boys vs Girls(20)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016 This time you are aske ...

  9. 【SSH】——两种添加jar包方式的比较

    [前言] 在开发过程中,我们对Eclipse或MyEclipse等IDE越来越熟悉了.在使用的过程中,小编了解到两种添加jar包的方式,今天给大家说下这两种方式的差别. 方法一: 将所需要的jar包拷 ...

  10. 【bzoj4998】星球联盟 LCT+并查集

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...