yanhaoming @ 2024-06-26 16:51:24
点击查看测评结果 上代码:
# include<bits/stdc++.h>
using namespace std;
int ar[33][33];
int n;
struct point {
int x, y;
};
const int xj[4] = {-1, 1, 0, 0};
const int yj[4] = {0, 0, -1, 1};
int bfs(int x, int y) {
point S;
S.x=x;
S.y=y;
if (ar[x][y] != 0)return 0;
point pts[24111];
int ptsi = 0;
pts[ptsi] = S;
ptsi++;
ar[S.x][S.y] = -1;
int flag = 1, cnt = 0;
while (flag) {
flag = 0;
vector<point> newp;
for (int i = 0; i < ptsi; i++) {
for (int j = 0; j < 4; j++) {
point wz = pts[i];
wz.x += xj[j];
wz.y += yj[j];
if (wz.x >= 0 && wz.x < x && wz.y >= 0 && wz.y < y && ar[wz.x][wz.y] == 0) {
flag = 1;
newp.push_back(wz);
ar[wz.x][wz.y] = -1;
}
}
}
ptsi = 0;
for (int i = 0; i < newp.size(); i++) {
pts[ptsi] = newp[i];
ptsi++;
}
}
return 0;
}
int main() {
cin >> n;
char st[1001][1001];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> st[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ar[j][i] = st[i][j] - '0';
}
}
// cout << " ";
// for (int i = 0; i < n; i++) {
// cout << "__";
// }
// cout << endl;
// for (int i = 0; i < n; i++) {
// cout << "|";
// for (int j = 0; j < n; j++) {
//
// cout << (ar[j][i] ? "█ " : " ");
// }
// cout << "|\n";
// }
// cout << " ";
// for (int i = 0; i < n; i++) {
// cout << " ̄";
// }
for (int i = 0; i < n; i++) {
bfs(0, i);
}
for (int i = 1; i < n - 1; i++) {
bfs(i, n - 1);
}
for (int i = n - 1; i >= 0; i--) {
bfs(n - 1, i);
}
for (int i = n - 2; i > 0; i--) {
bfs(i, 0);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (ar[j][i] == -1)cout << 0;
else if (ar[j][i] == 0)cout << 2;
else if (ar[j][i] == 1)cout << 1;
}
cout << endl;
}
return 0;
}
注:用的是广搜。 能帮帮忙吗?
by 123huchenghao @ 2024-06-28 18:30:38
#include<bits/stdc++.h>
using namespace std;
struct pos
{
int x;
int y;
};
bool mark[32][32];
int pic[32][32],dx[]= {0,0,1,-1},dy[]= {1,-1,0,0},n;
queue<pos>q;
void bfs(int x,int y)
{
int tx,ty;
q.push(pos{x,y});
mark[x][y]=true;
while(!q.empty())
{
tx=q.front().x;
ty=q.front().y;
q.pop();
for(int i=0; i<4; i++)
{
if(tx+dx[i]<0||ty+dy[i]<0||tx+dx[i]>n+1||ty+dy[i]>n+1||mark[tx+dx[i]][ty+dy[i]])continue;
q.push(pos{tx+dx[i],ty+dy[i]});
mark[tx+dx[i]][ty+dy[i]]=true;
}
}
}
int main()
{
memset(pic,0,sizeof(pic));
memset(mark,0,sizeof(mark));
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>pic[i][j];
mark[i][j]=pic[i][j];
}
}
bfs(0,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(mark[i][j])cout<<pic[i][j]<<" ";
else cout<<"2"<<" ";
}
cout<<endl;
}
}
by yanhaoming @ 2024-06-28 20:31:18
非常感谢!
@123huchenghao