站外求调(违规紫衫

题目总版

_Vistion_ @ 2024-11-26 20:40:37

问从S到E最小时间,没有输出oop!

每次都输出oop!,求大佬帮忙更改

/******************************************************************************

Welcome to GDB Online.
  GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
  C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <bits/stdc++.h>
#define int long long
#define ll ((i)*2)
#define rr ((i)*2+1)
using namespace std;
const int M=2e2+10;
const int mod=1e9+7;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int r,c,vis[M][M];
char Map[M][M];
int sx,sy,fx,fy,ans=INT_MAX;
int f;
int next[5][3]={{1,0},{0,1},{-1,0},{1,0}};
void dfs(int x,int y,int d)
{
    if(x==fx&&y==fy){
        ans=min(ans,d);
        f=1;
        return;
    }
    for(int i=0; i<4; i++){
        int nx=next[i][0]+x,ny=next[i][1]+y;
        if(Map[nx][ny]=='#'||vis[nx][ny]||nx<1||nx>r||ny<1||ny>c) continue;
        vis[nx][ny]=1;
        dfs(nx,ny,d+1);
        vis[nx][ny]=0;
    }
}
signed main()
{
    r=read(),c=read();
    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            cin>>Map[i][j];
            if(Map[i][j]=='S') sx=i,sy=j;
            if(Map[i][j]=='E') fx=i,fy=j;
        }
    }
    vis[sx][sy]=1;
    dfs(sx,sy,0);
    if(!f) cout<<"opp!";
    else cout<<ans;
    return 0;
}
/*
3 4
.S..
###.
..E.
*/

by _Vistion_ @ 2024-11-26 20:46:03

玄关


by Holmes_Wang @ 2024-11-26 20:48:51

@Vistion

你的 nxny 的边界判断有问题。你的数组是 [0,r-1][0,c-1] 的,那边界判断条件应该是

nx<0||nx>=r||ny<0||ny>=c


by _Vistion_ @ 2024-11-26 20:50:21

@Holmes_Wang

感谢大佬,已关


by fire_hua @ 2024-11-27 14:04:50

@Vistion

int next[5][3]={{1,0},{0,1},{-1,0},{0,-1}};

应该这样吧


by _Vistion_ @ 2024-11-27 19:28:43

@fire_hua

顺序没啥大影响吧


by fire_hua @ 2024-11-28 17:33:29

@Vistion 你写了两个1,0;没写0,-1


by _Vistion_ @ 2024-11-28 17:42:21

@fire_hua

哦谢谢


|