0分求调

B3625 迷宫寻路

MESSI_ @ 2024-05-04 20:36:41

#include<bits/stdc++.h>
using namespace std;
int n,m;
string st[101];
int mapp[101][101];
int flag;
int dx[4]={0,-1,0,1};
int dy[4]={1,0,-1,0};
void dfs(int x,int y)
{
    if(x==n&&y==m){
        flag=1;
        return;
    }
    mapp[x][y]=1;
    for(int i=1;i<=4;i++)
    {
        int xx=x+dx[i],yy=y+dy[i];
        if(mapp[xx][yy]==0&&xx>=1&&xx<=n&&yy==1&&yy==m)
        {
            dfs(xx,yy);
        }
    }
}
int main(void){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        cin >> st[i];
        for(int j=0;j<m;j++){
            if(st[i][j]=='#') mapp[i][j+1]=1;
            else mapp[i][j+1]=0;
        }
    }
    dfs(1,1);
    if(flag==1)
    cout << "Yes";
    else
    cout << "No";
    return 0;
}

by sxms_tanjialei @ 2024-05-04 20:55:53

@MESSI_ 我给你改了一点,改了个60分代码

#include<bits/stdc++.h>
using namespace std;
int n,m;
string st[101];
int mapp[101][101];
int flag;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
void dfs(int x,int y)
{
    if(x==n&&y==m){
        cout<<"Yes";
        exit(0);
    }
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i],yy=y+dy[i];
        if(mapp[xx][yy]==0&&xx>=1&&xx<=n&&yy>=1&&yy<=m)
        {
            mapp[xx][yy]=1;
            dfs(xx,yy);
        }
    }
    return ;
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        cin >> st[i];
        for(int j=1;j<=m;j++)
        {
            if(st[i][j]=='#')
                mapp[i][j]=1;
            else
                mapp[i][j]=0;
        }
    }
    dfs(1,1);
    cout << "No";
    return 0;
}

by sxms_tanjialei @ 2024-05-04 20:58:28

@MESSI_

AC代码仅供参考,可以看看自己哪里有问题

#include<bits/stdc++.h>
using namespace std;
int n,m;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
bool v[101][101];
char a[101][101]={0};
void dfs(int x,int y)
{
    if(x==n&&y==m)
    {
        cout<<"Yes";
        exit(0);
    }
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(xx>n||yy>m||xx<1||yy<1||a[xx][yy]=='#'||v[xx][yy]==true)
            continue;
        v[xx][yy]=true;
        dfs(xx,yy);
    }
    return ;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    if(a[1][2]=='#'&&a[2][1]=='#')
    {
        cout<<"No";
        exit(0);
    }
    dfs(1,1);
    cout<<"No";
    return 0;
}

by MESSI_ @ 2024-05-07 19:34:33

@sxms_tanjialei

Thank you very much


by MESSI_ @ 2024-05-07 19:37:29

@sxms_tanjialei

已关注


by dy182312 @ 2024-06-16 15:52:48

@MESSI_ 垃


by yanhaoming @ 2024-06-26 17:03:35

# include<bits/stdc++.h>
using namespace std;
struct point {
    int x;
    int y;
    void operator=(int a[2]) {
        x = a[0];
        y = a[1];
    }
    void operator=(point b) {
        x = b.x;
        y = b.y;
    }

};

const int xj[4] = {-1, 1, 0, 0};
const int yj[4] = {0, 0, -1, 1};
signed main() {
    point S, E;
    int x, y;
    scanf("%d", &y);
    scanf("%d", &x);
    S.x = 0;S.y = 0;
    E.x = x-1;E.y = y-1;
    int migong[155][155];
    for (int yy = 0; yy < y; yy++) {
        for (int xx = 0; xx < x; xx++) {
            char c;
            cin >> c;

            if (c == '.') {
                migong[xx][yy] = 0;
            }
            if (c == '#') {
                migong[xx][yy] = 1;
            }
        }
    }
    point pts[24111];
    int ptsi = 0;
    pts[ptsi] = S;
    ptsi++;
    migong[S.x][S.y] = -1;
    int flag = 1, cnt = 0;
    while (flag) {
        flag = 0;
        vector<point> newp;

        for (int i = 0; i < ptsi; i++) {

            if (pts[i].x == E.x && pts[i].y == E.y) {
                printf("Yes");
                return 0;
            }

            for (int j = 0; j < 4; j++) {
                point wz = pts[i];
                wz.x += xj[j];
                wz.y += yj[j];
                if (wz.x >= 0 && wz.x < x && wz.y >= 0 && wz.y < y && migong[wz.x][wz.y] == 0) {
                    flag = 1;
                    newp.push_back(wz);
                    migong[wz.x][wz.y] = -1;
                }
            }

        }
        ptsi = 0;
        for (int i = 0; i < newp.size(); i++) {
            pts[ptsi] = newp[i];
            ptsi++;
        }
        cnt++;
    }
    printf("No");
    return 0;
}

|