全部超时

B3625 迷宫寻路

jiejielove @ 2023-03-06 14:02:56


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<string>
#include<algorithm>
#include <cstdlib>
#include<stdlib.h>
#include<sstream>
#include<stack>
using namespace std;

char a[107][107];
int b[107][107];
int main()
{
    b[1][1] = 1;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    int i=1, j=1;
    while (i != n || j != m)
    {
        if (a[i + 1][j] == '.' && b[i + 1][j] == 0)
        {
            b[i + 1][j] = 1;
            i = i + 1;
        }
        else if (a[i][j + 1] == '.' && b[i][j + 1] == 0)
        {
            b[i][j + 1] =1;
            j = j + 1;
        }
        else if (a[i - 1][j] == '.' && b[i - 1][j] == 0)
        {
            b[i - 1][j] = 1;
            i = i - 1;
        }
        else if (a[i][j - 1] == '.' && b[i][j - 1] == 0)
        {
            b[i][j - 1] = 1;
            j = j - 1;
        }
        if (i == n && j == m)
        {
            cout << "Yes";
            return 0;
        }
        if (a[i + 1][j] != '.' && a[i][j + 1] != '.' && a[i - 1][j] != '.' && a[i][j - 1] != '.')
        {
            cout << "No";
            return 0;
        }
    }
    return 0;
}

by onlyfiee @ 2023-03-07 16:37:12

@jiejielove 还有一点就是起点也要标记


by jiejielove @ 2023-03-07 20:32:39

@onlyfiee 谢谢大佬,关注了


by onlyfiee @ 2023-03-08 12:30:53

@jiejielove

不,我是蒻苣,不过有什么问题也可可以问我


上一页 |