dfs(魔力转圈圈)
http://oj.jxust.edu.cn/contest/problem?id=1563&pid=4
题目描述
Storm有一个m行n列的整数矩阵。
他会从(1,1)开始,顺时针螺旋访问该矩阵,每个元素恰好被访问一次。
请你按Storm的访问顺序输出每个元素。
输入
第一行输入整数t,表示有t组数据,0 < t < 50;
第一行输入两个数m和n,其中0<m,n≤500;
之后m行,每行n个数以空格隔开,表示这个矩阵。
输出
输出t行,每行表示每组螺旋输出的结果
样例输入
1
3 4
1 2 3 4
5 6 7 8
9 10 11 12
样例输出
1 2 3 4 8 12 11 10 9 5 6 7 题意很简单就是顺时针呈螺旋状将所有数组元素都访问一遍。 正确代码:
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
int a[][];
int vis[][];
int n , m ;
int dir[][] = {{,},{,},{,-},{-,}}; void dfs(int x , int y , int d)
{
vis[x][y] = ;
printf("%d " , a[x][y]);
for(int i = ; i < ; i++)//当i==1说明此时需要改变方向
{
int xx = x + dir[(i+d)%][];
int yy = y + dir[(i+d)%][];
if(vis[xx][yy] || xx < || xx >= n || yy < || yy >= m)
{
continue ;
} vis[xx][yy] = ;
dfs(xx , yy ,d+i);//一直往里递归 } } int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
memset(vis , , sizeof(vis));
scanf("%d%d" , &n , &m);
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
scanf("%d" , &a[i][j]);
}
}
dfs( , , );
cout << endl;
} return ;
}
错误代码:
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
int a[][];
int vis[][];
int n , m ; void dfs(int x , int y)
{
vis[x][y] = ;
printf("%d " , a[x][y]);
if(!vis[x][y+] && y+ < m)
dfs(x , y+);
if(!vis[x+][y] && x+ < n)
dfs(x+ , y);
if(!vis[x][y-] && y- >= )
dfs(x , y-);
if(!vis[x-][y] && x- >= )
dfs(x- , y);
} int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
memset(vis , , sizeof(vis));
scanf("%d%d" , &n , &m);
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
scanf("%d" , &a[i][j]);
}
}
dfs( , );
} return ;
}
dfs(魔力转圈圈)的更多相关文章
- 小b和矩阵
2486 小b和矩阵 2 秒 262,144 KB 5 分 1 级题 小b有一个m行n列的矩阵. 她会从(1,1)开始,顺时针螺旋访问该矩阵,每个元素恰好被访问一次. 请你按小b的访问顺序输出每个 ...
- T4模板使用记录,生成Model、Service、Repository
自己目前在搭建一个.NET Core的框架,本来是打算使用前端做代码生成器直接生成到文件的,快做好了.感觉好像使用T4更方便一些,所以也就有了这篇文章~ 我还是有个问题没解决,就是我想生成每个类(接 ...
- 【疑难杂症】windows下如何有效重装印象笔记
重装这么简单的操作还用得着写篇文章吗??emmmm,言之有理,简单的重装就是卸载后重新下载最新的安装包然后安装就完事了,这里说的肯定是不简单的重装[滑稽]. 背景是这样的,之前在mac上对印象笔记的笔 ...
- 你未必知道的49个CSS知识点
作者:老姚,<JS正则迷你书>的作者 https://github.com/qdlaoyao/css-gif 本文的每一条,都是我曾经发过的掘金沸点,其中有很多条超过了百赞(窃喜).鉴于时 ...
- day 50 jquary 终极版本
jQuary 一.jquary对象和dom对象 jquary找到的标签对象成为-- jquary对象 原生js找到的标签对象成为 -- dom对象 dom对象只能使用dom对象的方法,不能使用jque ...
- web笔记全
1.项目流程与数据库 1.课程体系 阶段1(服务器开发): 项目导入/数据库/JS基础/NodeJS 阶段2(前端核心技术): HTML/AJAX/CSS/bootstrap 阶段3(前端进阶技术): ...
- SCI-HUB打不开了?附SCIHUB最新下载方式
写在前面: 今天给大家推荐一个文献下载工具包:飞鸟科研助手 www.flybird.cc输入flybird.cc同样可以访问,存书签不失联!强调下:flybird.cc 读研之前,在一家NGS生殖应用 ...
- 文件共享NFS&&autofs
文件传输工具 NFS服务 ftp vsftpd Samba linux和Windows之间进行文件共享 专用于linux和linux之间的专门的文件共享服务 (NFS服务),network,files ...
- 百度之星热身赛-1001(dfs拓扑排序)
题意:作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机.这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的).有一天,这台计算机得到了n个任务要处理, ...
随机推荐
- Python之常用模块一(主要RE和collections)
一.认识模块 什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.py文件 ...
- python如何调用c编译好可执行程序
python如何调用c编译好可执行程序 以下总结出几种在Python 中调用 C/C++ 代码的方法 ------------------------------------------- ...
- 【usaco 2013 open yinyang】阴阳
题目 Farmer John 正在在计划自己的农场漫步.他的农场的结构就像一棵树:农场有N个谷仓(1<= N <=100,000),分别由N-1条路链接.这样,他便可以通过这些谷仓间的道路 ...
- LeetCode 1143 最长公共子序列
链接:https://leetcode-cn.com/problems/longest-common-subsequence 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序 ...
- css3 中的2D转换
一.CSS3转换 通过转换实现对对元素进行旋转.缩放.移动.拉伸的效果:这种原来必须要通过JS或者图片处理才可以实现的效果,现在都可以通过CSS3来完成. 2D转换采用transform属性来实现效果 ...
- Nginx动静分离-tomcat
一.动静分离 1.通过中间件将动态请求和静态请求分离. 2.为什么? 分离资源,减少不必要的请求消耗,减少请求延时. 3.场景 还可以利用php,fastcgi,python 等方式 处理动态请求 # ...
- 1.Windows下安装nginx
1. 到nginx官网http://nginx.org/上下载相应的安装包 下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图所示: 进入window的cm ...
- [ethereum源码分析](3) ethereum初始化指令
前言 在上一章介绍了关于区块链的一些基础知识,这一章会分析指令 geth --datadir dev/data/02 init private-geth/genesis.json 的源码,若你的eth ...
- 音频学习(一)-离线播放(AVAudioPlayer)
最后更新:2017-05-23 方法 - (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)out ...
- Java称霸编程语言排行榜
笔者精挑细选了本周研发频道的热门看点,供您在这个周末阅读欣赏.内容涵盖TIOBE编程语言8月份排行榜.开源挑战.WebGL演示.HTML5在线工具.IT职业身涯的14个建议,还有即将举行的SDCC(中 ...