90分求调
查看原帖
90分求调
1272262
hank119楼主2025/7/22 14:37
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2010;
struct node{
    int x,y,step;
    int flag;
}cur,nxt;
int dx[6] = {0,0,1,-1};
int dy[6] = {1,-1,0,0};
char mp[MAXN][MAXN];
bool v[MAXN][MAXN][2];
int n,m,ax,ay,f;
queue<node>q;
void bfs()
{
    cur.flag = 0;cur.step = 0;
    cur.x = 1;cur.y = 1;
    q.push(cur);
    v[1][1][0] = true;
    while (!q.empty())
    {
        cur = q.front();
        q.pop();
        if (cur.flag == 1) f = 1;
        else f = 0;
        for (int i=0; i<4; ++i)
        {
            int xx = dx[i]+cur.x;
            int yy = dy[i]+cur.y;
            if (xx>0&&xx<=n&&yy>0&&yy<=m&&mp[xx][yy]!='#'&&!v[xx][yy][f])    //是否出界 
            {
                if (xx==n&&yy==m)
                {
                    printf("%d",cur.step+1);
                    return ;
                }
                v[xx][yy][f] = true;
                nxt.flag = f;nxt.step = cur.step+1;
                nxt.x = xx;nxt.y = yy;
                q.push(nxt);
            }
        }
        int xx = cur.x+dx[4], yy = cur.y+dy[4];
        if (f==0&&!v[xx][yy][1]&&xx>0&&xx<=n&&yy>0&&yy<=m&&mp[xx][yy]!='#')
        {
            if (xx==n&&yy==m)
            {
                printf("%d",cur.step+1);
                return ;
            }
            v[xx][yy][1] = true;
            nxt.flag = 1;nxt.step = cur.step+1;
            nxt.x = xx;nxt.y = yy;
            q.push(nxt);
        }
    }
    printf("-1");    
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&dx[4],&dy[4]);
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=m; ++j)
            cin>>mp[i][j];
    bfs();
    return 0;
}

第7个测试点RE了,求调很急Q-Q

2025/7/22 14:37
加载中...