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个任务要处理, ...
随机推荐
- Gparted for partition of Linux on graphic interface
You can change the partition table on Linux by a group of tools, which is tool comprehansive for a n ...
- codeforces 868C - Qualification Rounds(构造)
原题链接:http://codeforces.com/problemset/problem/868/C 题意:有k个队伍参加比赛,比赛有n个预选的题目,有些队伍对已经事先知道了一些题目.问能不能选出若 ...
- Internet History, Technology, and Security(week1)——History: Dawn of Electronic Computing
前言: 第一次进行课程学习,在反复观看视频和查找字典翻译理解后选出了视频中个人认为较重要的概念,以下并不按照逐句翻译,中文概括大意余下自由发挥,对老师想要告诉我们的历史有一个初步的了解,顺便锻炼以下英 ...
- SpringCloud 教程 (四) docker部署spring cloud项目
一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...
- Swift权限控制
最后更新:2017-03-20 private: 只能在当前类里面访问 fileprivate: 只能在当前文件内访问 internal:internal访问级别所修饰的属性或方法在源代码所在的整个模 ...
- kali的网络IP设置
进入配置文件,配置IP leafpad /etc/network/interfaces.conf 写入如下格式的内容: /etc/network/interfaces # This file desc ...
- [洛谷P3938]:斐波那契(fibonacci)(数学)
题目传送门 题目描述 小$C$养了一些很可爱的兔子.有一天,小$C$突然发现兔子们都是严格按照伟大的数学家斐波那契提出的模型来进行繁衍:一对兔子从出生后第二个月起,每个月刚开始的时候都会产下一对小兔子 ...
- bootstrap select下拉框模糊搜索和动态绑定数据解决方法
此方法适合后台一次性返回所有数据好了废话不多说直接上代码: <!DOCTYPE html><html><head> <title>Bootstrap-s ...
- xpath 算法
w https://www.w3.org/TR/xpath20/ Before an expression can be processed, its input data must be repre ...
- C#和.NET获取绝对路径
c#获取绝对路径:System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"); .net获取绝 ...