请问一下,我为什么TLE了

P10234 [yLCPC2024] B. 找机厅

虽然看不到代码,但盲猜你用`memset`来清空数组了。~~猜错的话就把我忽略了吧~~
by _zuoqingyuan @ 2024-03-09 20:03:13


@[brave_gentleman](/user/372326) 看不到代码
by xiangzhenze611 @ 2024-03-09 20:07:29


啊? @[_zuoqingyuan](/user/731650) @[xiangzhenze611](/user/1005749)
by stibium @ 2024-03-09 20:23:45


```cpp #include<bits/stdc++.h> using namespace std; const int N = 2 * 1145; int t; int n,m; int a[N][N],b[N][N],c[N][N]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; map<int,char> mp; int main() { //ios::sync_with_stdio(0); //cin.tie(0); mp[0] = 'D'; mp[1] = 'R'; mp[2] = 'U'; mp[3] = 'L'; scanf("%d",&t); //t = 1; while(t--) { memset(a,-1,sizeof(a)); memset(b,-1,sizeof(b)); memset(c,-1,sizeof(c)); scanf("%d%d",&n,&m); for(int i = 1; i <= n; i ++ ) { for(int j = 1; j <= m; j ++ ) { char ch; wzq: ch = getchar(); if(ch == '\n') goto wzq; if(ch == '1') a[i][j] = 1; else a[i][j] = 0; } } queue<int> qx; queue<int> qy; qx.push(1); qy.push(1); c[1][1] = 0; while(!qx.empty()) { int tx = qx.front(); int ty = qy.front(); qx.pop(); qy.pop(); //cout << tx << " " << ty << endl; if(tx == n && ty == m) { printf("%d\n",c[tx][ty]); int sx = n,sy = m; stack<char> st; while(sx != 1 || sy != 1) { //cout << sx << " " << sy << " " << mp[b[sx][sy]] << endl; st.push(mp[b[sx][sy]]); int tmp = b[sx][sy] + 2; tmp %= 4; sx += dx[tmp]; sy += dy[tmp]; } while(!st.empty()) { putchar(st.top()); st.pop(); } puts(""); } for(int i = 0; i < 4; i ++ ) { int nx = tx + dx[i]; int ny = ty + dy[i]; if(a[nx][ny] == -1) continue; if(a[nx][ny] != a[tx][ty] && c[nx][ny] == -1) { b[nx][ny] = i; c[nx][ny] = c[tx][ty] + 1; qx.push(nx); qy.push(ny); } } } puts("-1"); continue; } } ```
by stibium @ 2024-03-09 20:24:06


@[_zuoqingyuan](/user/731650) 为什么memset会T
by stibium @ 2024-03-09 20:27:25


你代还永远会输出-1
by xiangmuchen @ 2024-03-09 20:32:03


memset超时了 用两层for循环初始化就行
by number2003 @ 2024-03-09 20:36:36


@[xiangmuchen](/user/750405) ```cpp #include<bits/stdc++.h> using namespace std; const int N = 2 * 1145; int t; int n,m; int a[N][N],b[N][N],c[N][N]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; map<int,char> mp; int main() { //ios::sync_with_stdio(0); //cin.tie(0); mp[0] = 'D'; mp[1] = 'R'; mp[2] = 'U'; mp[3] = 'L'; scanf("%d",&t); //t = 1; while(t--) { memset(a,-1,sizeof(a)); memset(b,-1,sizeof(b)); memset(c,-1,sizeof(c)); scanf("%d%d",&n,&m); for(int i = 1; i <= n; i ++ ) { for(int j = 1; j <= m; j ++ ) { char ch; wzq: ch = getchar(); if(ch == '\n') goto wzq; if(ch == '1') a[i][j] = 1; else a[i][j] = 0; } } queue<int> qx; queue<int> qy; qx.push(1); qy.push(1); c[1][1] = 0; while(!qx.empty()) { int tx = qx.front(); int ty = qy.front(); qx.pop(); qy.pop(); //cout << tx << " " << ty << endl; if(tx == n && ty == m) { printf("%d\n",c[tx][ty]); int sx = n,sy = m; stack<char> st; while(sx != 1 || sy != 1) { //cout << sx << " " << sy << " " << mp[b[sx][sy]] << endl; st.push(mp[b[sx][sy]]); int tmp = b[sx][sy] + 2; tmp %= 4; sx += dx[tmp]; sy += dy[tmp]; } while(!st.empty()) { putchar(st.top()); st.pop(); } puts(""); } for(int i = 0; i < 4; i ++ ) { int nx = tx + dx[i]; int ny = ty + dy[i]; if(a[nx][ny] == -1) continue; if(a[nx][ny] != a[tx][ty] && c[nx][ny] == -1) { b[nx][ny] = i; c[nx][ny] = c[tx][ty] + 1; qx.push(nx); qy.push(ny); } } } puts("-1"); continue; } } ```
by stibium @ 2024-03-09 20:37:11


@[brave_gentleman](/user/372326) memset每一次都会把整个数组循环清空一遍,如果说每一次的数据规模很小,数据组数很多的话那么就会超时
by return_TLE @ 2024-03-09 20:51:53


|