35分求助!
查看原帖
35分求助!
820210
AndyPomeloMarsGotoHE楼主2023/2/6 17:45

rt

#include <bits/stdc++.h>
using namespace std;

bool Map[20][20],book[20][20],flag;
int direct[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
int N,M,SX,SY,FX,FY,step;
string path;

inline void dfs(int x,int y,string str){
    if (x==FX&&y==FY){
        if (!flag) flag = 1;
        cout << str.substr(0,str.size()-2) << endl;
        return;
    }
    else{
        for (int i=0;i<4;++i){
            int tx = x + direct[i][0];
            int ty = y + direct[i][1];
            if (tx<1||ty<1||tx>N||ty>M||(!Map[tx][ty])||book[tx][ty]) continue;
            book[tx][ty] = 1;
            dfs(tx,ty,str+"("+to_string(tx)+","+to_string(ty)+")->");
            book[tx][ty] = 0;
        }
    }
}

int main(){
    cin >> N >> M;
    for (int i=1;i<=N;++i) for (int j=1;j<=M;++j) cin >> Map[i][j];
    cin >> SX >> SY >> FX >> FY;
    path = "(" + to_string(SX) + "," + to_string(SY) + ")->";
    dfs(SX,SY,path);
    if (!flag) cout << -1 << endl;
    return 0;
}
2023/2/6 17:45
加载中...