P1162 BFS,第四个点为什么会TLE

P1162 填涂颜色

求助QAQ
by C_Wang @ 2023-08-08 09:39:26


@[C_Wang](/user/1051196) 入队的点太多,队列爆了。将染色放在入队前可以规避。 下面是AC代码 ```cpp #include <bits/stdc++.h> using namespace std; int n, cnt; int chess[35][35]; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; queue<pair<int , int>> q; void bfs() { while(!q.empty()) { auto t = q.front(); q.pop(); for(int i = 0; i < 4; i++) { int xx = t.first + dx[i], yy = t.second + dy[i]; if(chess[xx][yy] == 0 && xx < n && yy < n && xx >= 0 && yy >= 0) { /*this*/chess[xx][yy] = 2; q.push({xx, yy}); } else continue; } //cout << cnt << endl; } } int main() { scanf("%d", &n); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d", &chess[i][j]); for(int i = 0; i < n; i++) { bool tag = false; for(int j = 0; j < n; j++) if(chess[i][j] == 0 && chess[i - 1][j] == 1 && chess[i][j - 1] == 1 && chess[i - 1][j - 1] == 1) { /*this*/chess[i][j]=2; q.push({i, j}); tag = true; break; } if(tag) break; } bfs(); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) printf("%d ", chess[i][j]); puts(""); } return 0; } ```
by zqy729 @ 2023-08-08 10:08:01


@[zqy729](/user/498117) !!!!!谢谢!!!! %%%%%%%%%%%
by C_Wang @ 2023-08-08 10:32:00


|