求助,MLE了

P10234 [yLCPC2024] B. 找机厅

可能是 `q.push` 那里的临时变量太多了,改成下面就是 WA#1 ```cpp #include <bits/stdc++.h> struct node{ int x,y,d; node()=default; node(int x,int y,int d) : x(x),y(y),d(d) {} }; const int MAX = 2005; int t; int n,m; bool vis[MAX][MAX]; char path[MAX][MAX]; std::string a[MAX]; std::array<int,4> dx{-1,1,0,0},dy{0,0,-1,1}; std::array<char,4> dir{'U','D','L','R'}; std::queue<node> q; bool in(int x,int y){return x >= 1 && x <= n && y >= 1 && y <= m;} void bfs(){ q.push({1,1,0}); node qwq; vis[1][1] = true; path[1][1] = 'S'; int x,y,d; int i,tx,ty; while(!q.empty()){ x=q.front().x,y=q.front().y,d=q.front().d; q.pop(); if(x == n && y == m){ std::cout << d << '\n'; std::vector<int> pathque; while(path[x][y] != 'S'){ pathque.push_back(path[x][y]); if(path[x][y] == 'U') x += dx[1],y += dy[1]; if(path[x][y] == 'D') x += dx[0],y += dy[0]; if(path[x][y] == 'L') x += dx[3],y += dy[3]; if(path[x][y] == 'R') x += dx[2],y += dy[2]; } std::reverse(pathque.begin(),pathque.end()); for(int i:pathque) std::cout << (char)i; std::cout << '\n'; return; } for(i = 0;i < 4;i++){ tx = x + dx[i],ty = y + dy[i]; if(in(tx,ty) && !vis[tx][ty] && a[x][y] != a[tx][ty]){ qwq.x=tx,qwq,y=ty,qwq.d=d+1; q.push(qwq); vis[tx][ty] = true; path[tx][ty] = dir[i]; std::cout << path[tx][ty]; } } } std::cout << -1 << '\n'; } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> t; while(t--){ std::cin >> n >> m; for(int i = 1;i <= n;i++){ memset(vis[i],0,sizeof(vis[i])); memset(path[i],0,sizeof(path[i])); std::cin >> a[i]; a[i] = '.' + a[i]; } bfs(); } return 0; } ```
by xiaoyang111 @ 2024-03-10 08:46:08


|