2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)
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 (蛇形填数)的更多相关文章
- 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)
Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...
- 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 ...
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)
Description Given a string S, which consists of lowercase characters, you need to find the longest p ...
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...
- 2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)
Description Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dime ...
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
- 2017Summmer_上海金马五校 F题,G题,I题,K题,J题
以下题目均自己搜 F题 A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...
- 算法竞赛入门经典第二版 蛇形填数 P40
#include<bits/stdc++.h> using namespace std; #define maxn 20 int a[maxn][maxn]; int main(){ ; ...
- HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)
题目链接 2016 CCPC东北地区大学生程序设计竞赛 B题 题意 给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...
随机推荐
- 什么鬼,又不知道怎么命名class了
什么鬼,又不知道怎么命名class了 2015/10/25 · CSS · class 分享到:5 原文出处: 结一(@结一w3cplus) 相信写css的人都会遇到下面的问题: 糟糕,怎么命名 ...
- CentOS 7 安装Nginx并实现域名转发
CentOS 7 条件 教程中的步骤需要root用户权限. 一.添加Nginx到YUM源 添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令: sudo rpm -Uvh http ...
- 自动化测试学习之路--java String、StringBuilder
Java中的String和StringBuilder类: 1.String对象是不可变的.每一个看起来修改了String值的方法,实际上都是创建了全新的String对象.代码示例如下: String ...
- JMeter接口响应数据出现乱码的三种解决方法
第一种方法: Content encoding设置为utf-8,若仍为乱码,请用方法2 图1 第二种方法: 修改bin文件夹下的jmeter.properties文件 搜索ISO,把“#sampler ...
- vs code 在终端下使用 code ./ 打开当前项目
Mac OS Visual Studio Code的扩展工具菜单中有Install command line的快捷安装 运行 VS code并打开命令面板( ⇧⌘P ),然后输入 shell comm ...
- c# 对List<T> 某字段排序,取TOP条数据
//排序的对象里的字段数据准备 try { cmr.v4 = Double.Parse(cmr.v3) - Double.Parse(cmr.v2); } catch (Exception e) { ...
- python完成简单购物功能
# # -*- coding: utf8 -*- # # Author:wxq # # date:2017/11/13 # # python 3.6 # 创建一个商品列表: product_lis = ...
- 详细介绍弹性盒模型(display:flex)
弹性盒模型,即Flexbox,是css3中的新特性,其实弹性盒模型的原身是dispaly:box:这里,我们暂时不考虑旧的,我们只看新的. 为容器指定弹性盒子,只需在父元素(也就是容器)中设置:dis ...
- 变量可以通过into赋值
- [Leetcode] set matrix zeroes 矩阵置零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...