明白错误点,但不知道怎么去更改46分
查看原帖
明白错误点,但不知道怎么去更改46分
1106580
wenjiayi楼主2024/7/31 11:34
#include<iostream>
#include<cstring>
using namespace std;
int n, m;
#define  a first
#define  b second 
char g[305][305];
int dist[305][305];
typedef pair<int, int> PII;
PII q[305 * 305];
int dx[4] = { -1,0,0,1 };
int dy[4] = { 0,-1,1,0 };
int hh = 0, tt = -1;
bool st[305][305];

void find(int &x,int &y)
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (g[i][j] == g[x][y] && (i != x || j != y))
            {
                x = i;
                y = j;
                return;
            }
        }
    }
}
void bfs(int x, int y)
{
   
    memset(dist, 0, sizeof dist);
    dist[x][y] = 0;
    q[++tt] = { x,y };
    while (hh <= tt)
    {
        auto t = q[hh++];
        for (int i = 0; i < 4; i++)
        {
            int xx = t.a + dx[i];
            int yy = t.b + dy[i];
           
            if (xx<1 || yy<1 || xx>n || yy>m) continue;
            if (g[xx][yy] == '#') continue;
            if (st[xx][yy]) continue;
            //cout << t.a << "   " << t.b << endl;
            if (g[xx][yy] >= 'A' && g[xx][yy] <= 'Z'&&!st[xx][yy])
            {
                
                find(xx, yy);
                st[xx][yy] = 1;
                dist[xx][yy] = dist[t.a][t.b] + 1;
                q[++tt] = { xx,yy };
                continue;
    
            }
            if (g[xx][yy] == '.')
            {
                st[xx][yy] = 1;
                dist[xx][yy] = dist[t.a][t.b] + 1;
               // cout << dist[xx][yy] << endl;
                q[++tt] = { xx,yy };
               
            }
            if (g[xx][yy] == '=')
            {
                dist[xx][yy] = dist[t.a][t.b] + 1;
                return;
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    int stx,sty,ansx, ansy;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> g[i][j];
            if (g[i][j] == '@')
            {
                stx = i, sty = j;
                st[i][j] = 1;
            }
            if (g[i][j] == '=')
            {
                ansx = i;
                ansy = j;
            }
        }
    }
    bfs(stx, sty);
    cout << dist[ansx][ansy] << endl;
}

46分,wa了一片,猜到了估计是因为让字母只能用一次导致的,但是不用st数组标记的话就会传送过去后立马传送回来,不知道怎么下手改了

2024/7/31 11:34
加载中...