• 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[509][509];
    int vis[509][509];
    int n , m ;
    int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
    
    void dfs(int x , int y , int d)
    {
        vis[x][y] = 1 ;
        printf("%d " , a[x][y]);
        for(int i = 0 ; i < 2 ; i++)//当i==1说明此时需要改变方向
        {
            int xx = x + dir[(i+d)%4][0];
            int yy = y + dir[(i+d)%4][1];
            if(vis[xx][yy] || xx < 0 || xx >= n || yy < 0 || yy >= m)
            {
                continue ;
            }
    
            vis[xx][yy] = 1;
            dfs(xx , yy ,d+i);//一直往里递归
    
        }
    
    
    }
    
    
    int main()
    {
        int t ;
        scanf("%d" , &t);
        while(t--)
        {
            memset(vis , 0 , sizeof(vis));
            scanf("%d%d" , &n , &m);
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = 0 ; j < m ; j++)
                {
                    scanf("%d" , &a[i][j]);
                }
            }
            dfs(0 , 0 , 0);
            cout << endl;
        }
    
    
        return 0 ;
    }





    错误代码:

    //#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[509][509];
    int vis[509][509];
    int n , m ;
    
    void dfs(int x , int y)
    {
        vis[x][y] = 1 ;
        printf("%d " , a[x][y]);
        if(!vis[x][y+1] && y+1 < m)
            dfs(x , y+1);
        if(!vis[x+1][y] && x+1 < n)
            dfs(x+1 , y);
        if(!vis[x][y-1] && y-1 >= 0)
            dfs(x , y-1);
        if(!vis[x-1][y] && x-1 >= 0)
            dfs(x-1 , y);
    }
    
    
    int main()
    {
        int t ;
        scanf("%d" , &t);
        while(t--)
        {
            memset(vis , 0 , sizeof(vis));
            scanf("%d%d" , &n , &m);
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = 0 ; j < m ; j++)
                {
                    scanf("%d" , &a[i][j]);
                }
            }
            dfs(0 , 0);
        }
    
    
        return 0 ;
    }
  • 相关阅读:
    51nod1711 平均数
    51nod1204 Parity
    51nod1274 最长递增路径
    51nod1403 有趣的堆栈
    51nod1364 最大字典序排列
    bzoj1857: [Scoi2010]传送带
    bzoj3224: Tyvj 1728 普通平衡树
    bzoj2396: 神奇的矩阵
    bzoj2428: [HAOI2006]均分数据
    splay入门
  • 原文地址:https://www.cnblogs.com/nonames/p/11048416.html
Copyright © 2020-2023  润新知