3 solutions

  • 1
    @ 2022-6-18 14:07:16

    带记录方向的DFS

    #include<bits/stdc++.h>
    using namespace std;
    #define ioio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define endl "\n"
    #define debug(x) cout<<#x<<":"<<x<<endl;
    #define L(k) k<<1
    #define R(k) k<<1|1
    #define P pair
    #define P1 first
    #define P2 second
    #define u_map unordered_map
    #define p_queue priority_queue
    typedef long long ll;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    const int INF2 = (1 << 31);
    const int N = 2e2 + 7;
    int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
    /*-------------------------------------------------*/
    
    int n,m;
    int ma[N][N];
    bool vis[N][N];
    vector<int>v;
    
    void dfs(int x,int y,int pre){
    	v.push_back(ma[x][y]);
    	vis[x][y]=1;
    	for(int i=pre,flag=0;i<=4;i++){
    		if(flag==1&&i==4)break;
    		else if(i==4){
    			i=0;
    			flag=1;
    		}
    		int xx=x+dx[i];
    		int yy=y+dy[i];
    		if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==0){
    			dfs(xx,yy,i);
    			break;
    		}
    	}
    }
    void slove() {
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++)
    			cin>>ma[i][j];
    	dfs(1,1,0);
    	for(int x:v)
    		cout<<x<<" ";
    }
    int main() {
    	ioio
    	int t=1;
    	//freopen("./test.txt","r",stdin);
    	//cin>>t;
    	while(t--)
    		slove();
    	return 0;
    }
    
    • 0
      @ 2023-11-8 11:59:22

      ① 改变方向条件,越界或者已经访问过了,访问过就用vis数组记录为1

      ② 如果遍历四个方向,如果四个方向都走完都没有路可以走,就break;

      易错点:不能把访问过的数字记录成0,因为矩阵里面可能有0的存在。(整了好久,才发现)

      代码:

      #include <bits/stdc++.h>
      using namespace std;
      int n, m, a[205][205], D[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
      bool vis[205][205];
      int main() {
      	scanf("%d%d", &m, &n);
      	for (int i = 1; i <= m; ++i)
      		for (int j = 1; j <= n; ++j)
      			scanf("%d", &a[i][j]);
      	int k = 0, num = 0, x = 1, y = 1;
      	printf("%d ", a[x][y]);
      	vis[x][y] = 1;
      	while (true) {
      		int xx = x + D[k][0], yy = y + D[k][1];
      		while ((xx < 1 || xx > m || yy < 1 || yy > n || vis[xx][yy]) && num < 5) {
      			k += 1;
      			k %= 4;
      			xx = x + D[k][0], yy = y + D[k][1];
      			num += 1;
      		}
      		if (num >= 5)
      			break;
      		x = xx, y = yy;
      		printf("%d ", a[xx][yy]);
      		vis[xx][yy] = 1;
      		num = 0;
      	} 	
      	return 0;
      }
      
      • 0
        @ 2022-3-30 20:24:26

        代码很好懂

        #include<iostream>
        #include<cstdio>
        #include<cmath>
        #include<cstdlib>
        #include<cstring>
        #include<algorithm>
        #include<vector>
        #include<string>
        #include<map>
        #include <climits>
        
        using namespace std;
        
        int  num[205][205];
        bool numi[205][205];
        int main(void)
        {
        	int a, b;
        	cin >> a >> b;
        
        	int k = a * b;
        	for(int i=0;i<a;i++)
        		for (int t = 0; t < b; t++) 
        		{
        			cin >> num[i][t];
        			numi[i][t] = true;
        		}
        	int x = 0, y = 0;
        	
        	while (k)
        	{
        		for (int i = y; i < a; i++)
        		{
        			if (!numi[i][x]) break;
        			y = i;
        			k--;
        			numi[i][x] = false;
        			k != 0 ? cout << num[i][x] << " " : cout << num[i][x];
        		}
        		if (!k) break;
        		x++;
        		for (int i = x; i < b; i++)
        		{
        			if (!numi[y][i]) break;
        			x = i;
        			k--;
        			numi[y][i] = false;
        			k != 0 ? cout << num[y][i] << " " : cout << num[y][i];
        		}
        		if (!k) break;
        		y--;
        		for (int i = y; i >= 0; i--)
        		{
        			if (!numi[i][x]) break;
        			y = i;
        			k--;
        			numi[i][x] = false;
        			k != 0 ? cout << num[i][x] << " " : cout << num[i][x];
        		}
        		if (!k) break;
        		x--;
        		for (int i = x; i >= 0; i--)
        		{
        			if (!numi[y][i]) break;
        			x = i;		
        			k--;
        			numi[y][i] = false;
        			k != 0 ? cout << num[y][i] << " " : cout << num[y][i];
        		}
        		if (!k) break;
        		y++;
        	}
        
        	return 0;
        }
        
        • 1

        Information

        ID
        1176
        Time
        1000ms
        Memory
        512MiB
        Difficulty
        8
        Tags
        # Submissions
        39
        Accepted
        8
        Uploaded By