70分求助

B3625 迷宫寻路

I_love_big_ba_lei_te @ 2024-11-20 20:13:28

#include<bits/stdc++.h>
using namespace std;
int n,m;
char a[1005][1005];
void dfs(int i,int j,char s)
{
    a[i][j]='#';
    if(a[i][j+1]=='.')dfs(i,j+1,s);
    if(a[i+1][j]=='.')dfs(i+1,j,s);
    if(a[i][j-1]=='.')dfs(i,j-1,s);
    if(a[i-1][j]=='.')dfs(i-1,j,s);
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
      for(int j=1;j<=m;j++)
        cin>>a[i][j];
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++) 
        {
            if(a[i][j]=='.')
              {
                char k='.';
                dfs(i,j,k);
              }
        }
    if(a[n][m]=='#')
      cout<<"Yes";
    else cout<<"No";
    return 0;
}

by ddlove2014 @ 2024-11-20 20:23:09

#include <bits/stdc++.h>
using namespace std;
string ans = "No";
int p, q;
char a[105][105];
bool vis[105][105];
void dfs(int n, int m)
{
    if(n > p || m > q || n < 1 || m < 1 || vis[n][m] == 1) {return;}
    vis[n][m] = 1;
    if(n == p && m == q) 
    {
        ans = "Yes";
        return;
    }
    if(a[n][m] != '#') 
    {
        dfs(n - 1, m);
        dfs(n, m - 1);
        dfs(n + 1, m);
        dfs(n, m + 1);
    }
}
int main()
{
    cin >> p >> q;
    for(int i = 1; i <= p; i++)
        for(int j = 1; j <= q; j++)
            cin >> a[i][j];
    dfs(1, 1);
    cout << ans;
}

//这题不会?孩子,你在写什么,dfs孩子,为什么写一个洪水填充啊?


by I_love_big_ba_lei_te @ 2024-11-25 19:33:31

@ddlove2014 谢谢


|