60分,求助

B3625 迷宫寻路

Glocker @ 2024-06-17 17:25:50

#include "bits/stdc++.h"
using namespace std;
int dicx[4]={1,-1,0,0};
int dicy[4]={0,0,1,-1};
int maze[105][105];
int flag[105][105];
int n,m;
string jud="No";
void find(int x,int y){
    if(x==n-1&&y==m-1){
        jud="Yes";
        return ;
    }
    for(int i=0;i<4;i++){
        int tepx=x+dicx[i];
        int tepy=y+dicy[i];
        if(x>=0&&x<n&&y>=0&&y<m&&maze[tepx][tepy]==0&&flag[tepx][tepy]==0){
            flag[tepx][tepy]=1;
            find(tepx,tepy);
        }
    }
}
int main(){

    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            char tep;
            cin>>tep;
            maze[n][m]=(tep=='#');
        }
    }
    flag[0][0]=1; 
    find(0,0);
    cout<<jud;
}

by dengjunhaodejia09 @ 2024-06-17 18:36:50

@Glocker maze[n][m]=(tep=='#');错了

maze[i][j]==(tep=='#')


by Glocker @ 2024-06-18 16:59:29

@dengjunhaodejia09 真奇怪,这还得了60分


by yanhaoming @ 2024-06-26 16:59:41

可以参考一下:

# 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;
}

禁止抄袭

禁止抄袭

禁止抄袭

禁止抄袭


by The_Land_of_Smile @ 2024-07-06 11:56:29

@Glocker JO


|