比赛 板子大赛 评测结果 AWWWWAAAAA
题目名称 走迷宫 最终得分 60
用户昵称 Gao 运行时间 0.033 s
代码语言 C++ 内存使用 3.33 MiB
提交时间 2025-01-22 14:51:49
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n,m,a[20][20],h[6]={-1,0,0,1},l[6]={0,-1,1,0},kun;
bool b[20][20];
int sx,sy,fx,fy,p[400],q[400],cnt=1;
void sgt(int x,int y){
    if(x==fx&&y==fy){
        kun=1;
        for(int i=1;i<=cnt;i++){//(,)
            cout<<"("<<p[i]<<","<<q[i]<<")";
            if(i<cnt)cout<<"->";
        }
        cout<<endl;
        return;
    }
    for(int o=0;o<=3;o++){
        int xx=x+h[o],yy=y+l[o];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&b[xx][yy]==0&&a[xx][yy]==1){
            b[xx][yy]=1;
            cnt++;
            p[cnt]=xx;
            q[cnt]=yy;
            sgt(xx,yy);
            b[xx][yy]=0;
            cnt--;
        }
    }
}
int main(){
    freopen("maize.in","r",stdin);
    freopen("maize.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    cin>>sx>>sy>>fx>>fy;
    p[1]=sx;
    q[1]=sy;
    sgt(sx,sy);
    if(kun==0){
        cout<<"-1";
    }
    return 0;
}