求DALAO帮助,悬赏3个关注

P1746 离开中山路

laoqiu_tyxx @ 2024-07-03 18:47:52

TLE,代码:

#include<bits/stdc++.h>
using namespace std;
struct pos
{
    int x;
    int y;
    int step;
};
char a[1010][1010];
int n,i,j,x1,x2,y1,y2;
bool fl=false;
bool flag[1010][1010]={false};
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
queue<pos>q;
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            cin>>a[i][j];
        }
    }
    cin>>x1>>y1;
    cin>>x2>>y2;
    q.push({x1,y1,1});
    flag[x1][y1]=true;
    while(!q.empty())
    {
        if(q.front().x==x2&&q.front().y==y2)
        {
            cout<<q.front().step-1;
            return 0;
        }
        for(i=0;i<4;i++)
        {
            int xx=q.front().x+dx[i];
            int yy=q.front().y+dy[i];
            int st=q.front().step+1;
            if(a[xx][yy]=='0'&&((xx>=1&&xx<=n)&&(yy>=1&&yy<=n))&&!flag[xx][yy])
            {
                q.push({xx,yy,st+1});
                flag[xx][yy]=true;
            }
        }
        q.pop();
    }
}
/*
3 5
.##.#
.#...
...#.
*/

by panxz2009 @ 2024-07-03 18:56:00

#include<bits/stdc++.h>
#define y1 laoqiu_tyxx
using namespace std;
struct pos
{
    int x;
    int y;
    int step;
};
char a[1010][1010];
int n,i,j,x1,x2,y1,y2;
bool fl=false;
bool flag[1010][1010]={false};
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
queue<pos>q;
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            cin>>a[i][j];
        }
    }
    cin>>x1>>y1;
    cin>>x2>>y2;
    q.push({x1,y1,0});
    flag[x1][y1]=true;
    while(!q.empty())
    {
        for(i=0;i<4;i++)
        {
            int xx=q.front().x+dx[i];
            int yy=q.front().y+dy[i];
            int st=q.front().step+1;
            if(a[xx][yy]=='0'&&((xx>=1&&xx<=n)&&(yy>=1&&yy<=n))&&!flag[xx][yy])
            {
                q.push({xx,yy,st});
                flag[xx][yy]=true;
                if(xx==x2&&yy==y2)
                {
                    cout<<st;
                    return 0;
                }
            }
        }
        q.pop();
    }
}
/*
3 5
.##.#
.#...
...#.
*/

by panxz2009 @ 2024-07-03 18:57:02

@laoqiu_tyxx 你的 st 变量加了两遍


by laoqiu_tyxx @ 2024-07-03 19:00:24

??? 这是怎么回事


by panxz2009 @ 2024-07-03 19:02:32

y1 不能作为变量名 @laoqiu_tyxx

你可以像我一样把 y1define


by laoqiu_tyxx @ 2024-07-03 19:05:30

@panxz2009
还是超时


by panxz2009 @ 2024-07-03 19:08:46

??
你的提交记录在哪 @laoqiu_tyxx


by panxz2009 @ 2024-07-03 19:09:04

代码给我看下


by laoqiu_tyxx @ 2024-07-03 19:10:47

@panzx2009
我在本地测的,现在对了


by panxz2009 @ 2024-07-03 19:11:28

好吧


|