MLE 20pts求调

B3625 迷宫寻路

hailibu @ 2023-08-31 16:02:02

MLE 20pts

#include <bits/stdc++.h>
using namespace std;
const int N = 100+5;
int n,m;
char a;
int b[N][N];
int remi,remj;
int temp;
void dfs(int i,int j){
//  cout << i << ' ' << j  << ' ' << temp << endl;
    if(i == 1 && j == 1){
        temp = 1;
        return;
    }
    if(i-1>0 && b[i-1][j] == 1 && i-1 != remi){
        remi = i;
        remj = j;
        dfs(i-1,j);
    }
    if(j+1<=m && b[i][j+1] == 1 && j+1 != remj){
        remj = j;
        remi = i;
        dfs(i,j+1);
    }
    if(j-1>0 && b[i][j-1] == 1 && j-1 != remj){
        remj = j;
        remi = i;
        dfs(i,j-1);
    }
    if(i+1<=n && b[i+1][j] == 1 && i+1 != remi){
        remi = i;
        remj = j;
        dfs(i+1,j);
    }
    b[i][j] = 2;
    return;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;++i){
        for(int j = 1;j <= m;++j){
            scanf("%c",&a);
//          cout << a << endl;
//          cout << i << ' ' << j << endl;
            if((int)a == 35){
                b[i][j] = 2;
//              cout << b[i][j] << endl;
            }
            else if((int)a == 46){
                b[i][j] = 1;
            }
            else{
                j --;
            }
        }
    }
//  for(int i = 1;i <= n;i ++){
//      for(int j = 1;j <= m;j ++){
//          cout << b[i][j] << ' ';
//      }
//      cout << endl;
//  }
    remi = n;
    remj = m;
    dfs(n,m);
    if(temp == 1){
        printf("Yes");
        return 0;
    }
    printf("No");
    return 0;
} 

by jiqimaomaomao @ 2023-09-09 08:59:59

#include <bits/stdc++.h>
using namespace std;

struct point
{
    int x, y;
};

int main ()
{
    queue <point> q;
    int n, m;
    point tmp;

    char arr[101][101];
    bool is[101][101];

    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> arr[i][j];
            is[i][j] = false;
        }
    }

    tmp.x = 1;
    tmp.y = 1;

    q.push(tmp);
    is[1][1] = true;

    while (!q.empty()) {
        tmp = q.front();

        if (tmp.x == n && tmp.y == m) {
            cout << "Yes";
            return 0;
        }

        if (tmp.x != 1 && arr[tmp.x - 1][tmp.y] == '.' && is[tmp.x - 1][tmp.y] == false) {
            tmp.x--;
            q.push(tmp);
            is[tmp.x][tmp.y] = true;
            tmp.x++;
        }

        if (tmp.x != n && arr[tmp.x + 1][tmp.y] == '.' && is[tmp.x + 1][tmp.y] == false) {
            tmp.x++;
            q.push(tmp);
            is[tmp.x][tmp.y] = true;
            tmp.x--;
        }

        if (tmp.y != 1 && arr[tmp.x][tmp.y - 1] == '.' && is[tmp.x][tmp.y - 1] == false) {
            tmp.y--;
            q.push(tmp);
            is[tmp.x][tmp.y] = true;
            tmp.y++;
        }

        if (tmp.y != m && arr[tmp.x][tmp.y + 1] == '.' && is[tmp.x][tmp.y + 1] == false) {
            tmp.y++;
            q.push(tmp);
            is[tmp.x][tmp.y] = true;
            tmp.y--;
        }

        q.pop();
    }

    cout << "No";

    return 0;
}

by jiqimaomaomao @ 2023-09-09 09:00:40

AC代码送给你


by jiqimaomaomao @ 2023-09-09 09:03:19

本人是个蒟蒻,不会调代码,只能再给你一份啦(记得先看懂再提交,改改变量、顺序之类的


|