C_Wang @ 2023-08-08 09:37:56
思路是找到先找到方框内左上角的0(其上方和左方都是1)再开始搜,但第四个点会TLE
代码如下:
#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();
chess[t.first][t.second] = 2;
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)
{
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)
{
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;
}
第四个点的数据
30
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
by C_Wang @ 2023-08-08 09:39:26
求助QAQ
by zqy729 @ 2023-08-08 10:08:01
@C_Wang 入队的点太多,队列爆了。将染色放在入队前可以规避。 下面是AC代码
#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 C_Wang @ 2023-08-08 10:32:00
@zqy729 !!!!!谢谢!!!! %%%%%%%%%%%