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个任务要处理, ...
随机推荐
- uoj280 【UTR #2】题目难度提升 堆维护中位数+set
题目传送门 http://uoj.ac/problem/280 题解 这道题很妙啊. 这种题目如果给予选手足够的时间,每一个选手应该都能做出来. 大概就是核心思路看上去很简单,但是想要推出来并不简单. ...
- 洛谷 P4665 [BalticOI 2015]Network
洛谷 P4665 [BalticOI 2015]Network 你有一棵 $ n $ 个节点的树,你可以在树上加一些边,使这棵树变成一张无重边.自环的图,且删掉任意一条边它仍然联通.求最少要加多少条边 ...
- HTML与CSS中的颜色与单位个人分享
颜色与单位 Web安全色有216中其中色彩有210中,非色彩6中 前景色与背景色 前景色就是设置字体的颜色 背景色就是为指定元素设置背景色 - 浏览器默认背景色的颜色为透明色 颜色的命名 1.使用单词 ...
- 题解 P3166 【[CQOI2014]数三角形】
做完之后看了看题解,怎么一篇和我思路一样的也没有...我好慌啊qwq(所以一定是窝太弱了看不懂dalao的思路) 好吧窝的方法确实很奇怪: 核心代码只有3行 输入 循环 输出 一气呵成 是题解中的豪杰 ...
- Delphi-----接口请求,Get与Post
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- postman实现Base64加密
1.新建一个Collection 2.新建一个request 3.新增一个环境变量(全局变量也可以) 4.在variable中填入需要加密的变量名称,比如password 5.在body中填好参数,需 ...
- Ubuntu操作
1. 从windows 拷贝文件到Ubuntu: scp /c/Users/init/Downloads/pycharm-community-2019.3.tar.gz alice@192 ...
- 6398. 【NOIP2018模拟10.30】Generator(树状数组区间修改)
题目描述 Description Input Output 输出 q 行,第 i 行表示数据 Di 的答案. Sample Input 4 3 2 1 1 2 4 2 1 2 1 1 3 5 2 2 ...
- 倍增求LCA算法详解
算法介绍: 看到lca问题(不知道lca是什么自(bang)行(ni)百度),不难想到暴力的方法: 先把两点处理到同一深度,再让两点一个一个祖先往上找,直到找到一个相同的祖先: 这么暴力的话,时间复杂 ...
- Spring Cloud Commons教程(二)Spring RestTemplate作为负载平衡器客户端
RestTemplate可以自动配置为使用功能区.要创建负载平衡RestTemplate创建RestTemplate @Bean并使用@LoadBalanced限定符. 警告 通过自动配置不再创建Re ...