2 条题解

  • 0
    @ 2026-5-20 20:11:56
    using namespace std;
    
    const int N = 20;
    int n, m;
    int maze[N][N];
    int sx, sy, ex, ey;
    bool visited[N][N];
    int pathX[N * N], pathY[N * N];
    int pathLen;
    bool hasPath = false;
    int dx[4] = {0, -1, 0, 1};
    int dy[4] = {-1, 0, 1, 0};
    
    void dfs(int x, int y) {
        if (x == ex && y == ey) {
            hasPath = true;
            for (int i = 0; i < pathLen; i++) {
                cout << "(" << pathX[i] << "," << pathY[i] << ")";
                if (i != pathLen - 1) {
                    cout << "->";
                }
            }
            cout << endl;
            return;
        }
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && 
                maze[nx][ny] == 1 && !visited[nx][ny]) {
                visited[nx][ny] = true;
                pathX[pathLen] = nx;
                pathY[pathLen] = ny;
                pathLen++;
                dfs(nx, ny);
                pathLen--;
                visited[nx][ny] = false;
            }
        }
    }
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> maze[i][j];
            }
        }
        cin >> sx >> sy >> ex >> ey;
        if (maze[sx][sy] == 0 || maze[ex][ey] == 0) {
            cout << -1 << endl;
            return 0;
        }
        visited[sx][sy] = true;
        pathX[0] = sx;
        pathY[0] = sy;
        pathLen = 1;
        dfs(sx, sy);
        if (!hasPath) {
            cout << -1 << endl;
        }
    }
    
    
    • -5
      @ 2023-4-1 16:18:39
      //#include<huhe>
      #include<iostream>
      #include<queue>
      #define huhe int
      #define hu_h_e_ for
      #define h using
      #define uh namespace
      #define e std
      #define _huhe const
      #define huhesss return
      #define huhe__ cin
      #define __huhe__ main
      #define huhe_ void
      #define huheh struct
      #define huhehe queue
      #define _h_uhe char
      #define _h_u_h_e_ push 
      #define _huhe_ empty()
      #define h_u_h_e bool
      #define h_uh_e size()
      #define hu_h_e front()
      #define huhep pop()
      #define huh cout
      h uh e;
      struct node{
      	int hu,he,huh;
      };
      queue<node> huhehehe1;
      queue<node> huhehehe2;
      huhe huhen,huhem,
      huhedx[]={1,-1,0,0},
      huhedy[]={0,0,1,-1};
      _h_uhe huhec[52][52];
      huhe huhev[52][52],huhedis[52][52];
      huhe_ hu_he(){
      	huhehehe1._h_u_h_e_((node){1,1,1});
      	huhehehe2._h_u_h_e_((node){huhen,huhem,1});
      	huhev[1][1]=1;
      	huhev[huhen][huhem]=2;
      	huhedis[1][1]=1;
      	huhedis[huhen][huhem]=2;
      	while(!huhehehe1._huhe_||!huhehehe2._huhe_){
      		node huhetop;
      		h_u_h_e huheflag=0;
      		if(huhehehe1.h_uh_e>huhehehe2.h_uh_e){
      			huhetop=huhehehe2.front();
      			huhehehe2.pop();
      		}
      		else{
      			huhetop=huhehehe1.front();
      			huhehehe1.pop();
      			huheflag=1;
      		}
      		hu_h_e_(huhe huhei=0;huhei<4;huhei++){
      			huhe huhexx=huhetop.hu+huhedx[huhei],huheyy=huhetop.he+huhedy[huhei]; 
      			if(huhexx>=1&&huhexx<=huhen&&huheyy>=1&&huheyy<=huhem){
      				if(!huhev[huhexx][huheyy]&&huhec[huhexx][huheyy]=='.'){
      					if(huheflag){
      						huhev[huhexx][huheyy]=1;
      						huhedis[huhexx][huheyy]=huhedis[huhetop.hu][huhetop.he]+1;
      						huhehehe1._h_u_h_e_((node){huhexx,huheyy,huhetop.huh+1});
      					}
      					else{
      						huhev[huhexx][huheyy]=2;
      						huhedis[huhexx][huheyy]=huhedis[huhetop.hu][huhetop.he]+1;
      						huhehehe2._h_u_h_e_((node){huhexx,huheyy,huhetop.huh+1});
      					}
      				}
      				else if(huhev[huhexx][huheyy]+huhev[huhetop.hu][huhetop.he]==3){
      					cout<<huhedis[huhexx][huheyy]+huhedis[huhetop.hu][huhetop.he]-1;
      					exit(0);
      				}
      			}
      		}
      	}
      }
      huhe __huhe__(){
      	huhe__>>huhen>>huhem;
      	hu_h_e_(huhe huhei=1;huhei<=huhen;huhei++){
      		hu_h_e_(huhe huhej=1;huhej<=huhem;huhej++){
      			huhe__>>huhec[huhei][huhej];
      		}
      	}
      	hu_he();
      }
      /*
                 ,
                / \
               {   }
               p   !
               ; h ;
               | u |
               | h |
               l e l
               l 之l
               H剑 H
               H ; H
               H ; H
               H ; H
               d | b 
               U | E
               U | E
               U I E
       ,;,     U I E     ,;,
      ;H@U;    ;_H_;,   ;H@E;
      '\H/u_,;|HU@HE|;,_e\H/'
       '\;HHUUU$@@@$HHEEE;/'
         "~~~*;!h@h!;*~~~"
               ;u8u;
               ;h8h;
               ;e8e;
               ;h8h;
               du@ub
               Oh@hO
               Te0eT
                '~' 
      */
      
      • 1

      信息

      ID
      1303
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      递交数
      200
      已通过
      48
      上传者