全部超时

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 15:59:48

@jiejielove 说实话我有点看不懂你是什么意思


by jiejielove @ 2023-03-07 16:22:08

@onlyfiee 就是从(1,1)点开始疯狂找没有走过并且是路的点,每次都找四个方向。


by onlyfiee @ 2023-03-07 16:24:13

@jiejielove OK我看看


by onlyfiee @ 2023-03-07 16:26:46

@jiejielove 有没有可能,四个点都访问过


by onlyfiee @ 2023-03-07 16:27:31

@jiejielove 而且还有个问题


by jiejielove @ 2023-03-07 16:28:02

@onlyfiee 访问过的话就输出“No”了


by onlyfiee @ 2023-03-07 16:29:01

@jiejielove 超时间是因为i,j越界了


by onlyfiee @ 2023-03-07 16:32:25

@jiejielove 改好了


by onlyfiee @ 2023-03-07 16:32:49

#include<bits/stdc++.h>
using namespace std;
char a[107][107];
int b[107][107];
int n,m;
bool pd(int i,int j)
{
    if(i<=0||j<=0||i>n||j>m)    return false;
    return true;
}
int main()
{
    b[1][1] = 1;
    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 &&pd(i,j))
        {
            b[i + 1][j] = 1;
            i = i + 1;
        }
        else if (a[i][j + 1] == '.' && b[i][j + 1] == 0 &&pd(i,j))
        {
            b[i][j + 1] =1;
            j = j + 1;
        }
        else if (a[i - 1][j] == '.' && b[i - 1][j] == 0&&pd(i,j))
        {
            b[i - 1][j] = 1;
            i = i - 1;
        }
        else if (a[i][j - 1] == '.' && b[i][j - 1] == 0&&pd(i,j))
        {
            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:33:22

@jiejielove 哈哈刚刚有句话说错了


| 下一页