3 solutions

  • 0
    @ 2023-10-11 16:30:12
    #include<iostream>
    using namespace std;
    #define N 10
    struct point 
    {
        int x ;
        int y;
    };
    point path[N*N];//记录每个格子的坐标,每隔元素是结构体类型
    int n,m,step,cnt;
    int dx[2]={1,0};//方向数组
    int dy[2]={0,1};
    void dfs(int a,int b,int step){
        //终止条件:搜索到终点
        if(a==n&&b==m){ 
          cnt++;
          cout<<cnt<<":";
          for(int i=0;i<step;i++){
            if(path[i].x==n&&path[i].y==m)cout<<path[i].x<<","<<path[i].y;
            else  cout<<path[i].x<<","<<path[i].y<<"->";   
          }
          cout<<n<<","<<m;
          puts("");
          return ;
        }
          for(int i=0;i<2;i++){
            int tx=a+dx[i];
            int ty=b+dy[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                step++;
                path[step].x=tx;
                path[step].y=ty;
                dfs(tx,ty,step);
                step--;  
            }
          }
        }
    int main(){
        path[0].x=path[0].y=1;
        cin>>n>>m;
        dfs(1,1,0);
        return 0;
    }
    
    
    
    
    
    
    • 0
      @ 2022-4-10 17:04:05
      #include<iostream>
      using namespace std;
      int n,m;
      int sum;
      struct data{
      	int p,q;
      }a[110];//代表每次步骤的坐标记录下来 
      int g[100][110];
      int dx[2]={1,0};
      int dy[2]={0,1};
      void dfs(int x,int y,int u)
      {
      	if(x==n&&y==m)
      	{
      		a[u].p=x;
      		a[u].q=y;
      		sum++;
      		cout<<sum<<":"<<a[1].p<<","<<a[1].q;
      		for(int i=2;i<=u;i++)
      			cout<<"->"<<a[i].p<<","<<a[i].q;
      		cout<<endl;
      		return ;
      	}
      	a[u].p=x;
      	a[u].q=y;
      	g[x][y]=1;	
      	for(int i=0;i<2;i++)
      	{
      		int xx=x+dx[i],yy=y+dy[i];
      		if(xx>=1&&yy>=1&&xx<=n&&yy<=m&&!g[xx][yy])
      		{
      			dfs(xx,yy,u+1);
      			g[x][y]=0;
      		}
      	}
      }
      int main(){
      	cin>>n>>m;
      	dfs(1,1,1);
      	return 0;
      }
      /*
      dfs要记录路径
      每次搜索的深度,也就是每个步骤上,每次到达什么地方 
      
      */
      
      • 0
        @ 2022-4-6 12:10:05
        #include<bits/stdc++.h>
        using namespace std;
        int dx[2] = {1, 0};
        int dy[2] = {0, 1};
        vector<vector<int>> jl;
        vector<int> t;
        int n, m;
        int cnt = 0;
        void dfs(int x, int y, int depth) {
            t.push_back(x);
            t.push_back(y);
            jl.push_back(t);
            t.clear();
            if (x == n && y == m) {
                cnt++;
                cout << cnt << ":";
                for (unsigned int i = 0;i < jl.size() - 1;i++) {
                    cout << jl[i][0] << "," << jl[i][1] << "->";
                }
                cout << n << "," << m << endl;
                return;
            }
            int nx , ny;
            for (int i = 0;i < 2;i++) {
                nx = x + dx[i];
                ny = y + dy[i];
                if (ny > 0 && ny <= m && nx > 0 && nx <= n) {
                    dfs(nx, ny, depth + 1);
                    jl.pop_back();
                }
            }
        }
        int main() {
            ios::sync_with_stdio(false);
            cin.tie(0);
            cin >> n >> m;
            dfs(1, 1, 0);
            return 0;
        }
        
        • 1

        Information

        ID
        697
        Time
        1000ms
        Memory
        16MiB
        Difficulty
        5
        Tags
        # Submissions
        195
        Accepted
        71
        Uploaded By