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. 剖析DI

    0x00.前言 当我们研究一些晦涩的源码,上网查阅资料的时候,映入眼帘的总有这么些名词:DIP.IOC.DI.DL.IOC容器这些专业名词.如果不懂这些名词背后的含义,我们内心有可能是这样的: 0x0 ...

  2. jmeter插件之VariablesFromCSV

    项目需求: 由于该项目的特殊性,需要新建很多个jmx脚本,并且这些jmx的全局用户变量都一样,如果要修改的话,没法做到统一修改. 实现思路: 为了满足需求,在jemter官网找到该插件Variable ...

  3. Qt QML之不显示标题栏、边框

    原文连接:http://blog.csdn.net/u010780613 我使用的Qt版本是Qt 5.3.0,Qt Creator 是3.1.1. QML做界面实在太方便了,动画效果很不错. 创建一个 ...

  4. Qt 解析网络数据出现ssl错误

    最近写了点小东西,哈哈, 网络部分是同学帮我搞的 在编译的时候,出现了一下错误 qt.network.ssl: QSslSocket: cannot call unresolved function ...

  5. CSS3 : transform 与 transform-origin 属性可以使元素样式发生转变

    CSS3 : transform 用于元素样式的转变,比如使元素发生位移.角度变化.拉伸缩小.按指定角度歪斜 transform结合transition可实现各类动画效果 transform : tr ...

  6. Spring Cloud 自定义ConfigServer 解决敏感信息存储问题

    公司需要将系统配置信息中的敏感信息独立存放. 现有系统采用Spring Cloud Config提供配置信息,其中敏感信息主要是Db配置,分解本次需求: (1)数据库配置信息分离(主要是Db信息). ...

  7. c#执行插入sql 时,报错:异常信息:超时时间已到。在操作完成之前超时时间已过或服务器未响应

    问题:c#执行插入sql 时,报错:异常信息:超时时间已到.在操作完成之前超时时间已过或服务器未响应 解决: SqlCommand cmd = new SqlCommand(); cmd.Comman ...

  8. C++STL——list

    一.相关定义 list 链表,分配的内存不连续 可以高效地进行插入/删除元素 不可随机访问,访问速度慢 特征 只能通过迭代器来访问list中的元素 在头和尾都可以插入元素 二.list [前提条件] ...

  9. spark1.6.0伪分布式搭建

    环境: hadoop2.6.0 jdk1.8 ubuntu 14.04 64位 1 安装scala环境 版本是scala-2.10.6,官网下载地址http://www.scala-lang.org/ ...

  10. Chromium之工程依赖关系.

    Chromium各版本可能有差异,我的版本是chromium.r197479,2013/08前后下载的source code. Visual Studio Ultimate版本有工具可以自动生成项目依 ...