wrxw @ 2024-12-14 14:59:38
#include<iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int a[35][35];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
for (int i = 2; i < n; i++)
{
for (int j = 2; j < n; j++)
{
int left = 0, right = 0, up = 0, down = 0;
if (a[i][j] == 0)
{
for(int k=1;k<j;k++)
{
if (a[i][k] == 1)
{
left = 1;
break;
}
}
for (int k = j + 1; k <= n; k++)
{
if (a[i][k] == 1)
{
right = 1;
break;
}
}
for (int h = 1; h < i; h++)
{
if (a[h][j] == 1)
{
up = 1;
break;
}
}
for (int h = i + 1; h <= n; h++)
{
if (a[h][j] == 1)
{
down = 1;
break;
}
}
if (left == 1 && right == 1 && up == 1 && down == 1)a[i][j] = 2;
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout<< a[i][j]<<" ";
}
cout << endl;
}
return 0;
}
输入: 8
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1
1 0 1 0 0 0 0 0
1 0 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1
编译器输出:
1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 1
1 2 1 1 1 1 2 1
1 2 1 2 2 1 1 1
1 2 1 0 0 0 0 0
1 2 1 1 1 1 1 1
1 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1
感觉没问题
洛谷输出: 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 1
1 2 1 1 1 1 2 1
1 2 1 0 0 1 1 1
1 2 1 0 0 0 0 0
1 2 1 1 1 1 1 1
1 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1
嗯,就有问题了。
by LIUHAOYU2013 @ 2024-12-14 16:28:11
@wrxw
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1005;
int a[N][N], n, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, sx, sy, tx, ty, vis[N][N];
struct node{
int x, y;
};
node path[N][N];
bool check(int x, int y)
{
if(x < 0 || x > n + 1 || y < 0 || y > n + 1)
return true;
else if(a[x][y] == 1 || vis[x][y])
return true;
return false;
}
void bfs()
{
vis[sx][sy] = true;
queue <node> q;
q.push({sx, sy});
while(q.size())
{
node t = q.front();
q.pop();
for(int i = 0; i < 4; ++ i)
{
int px = t.x + dx[i], py = t.y + dy[i];
if(check(px, py))
continue;
vis[px][py] = true;
q.push({px, py});
}
}
return ;
}
signed main()
{
cin >> n;
for(int i = 1; i <= n; ++ i)
{
for(int j = 1; j <= n; ++ j)
{
cin >> a[i][j];
}
}
bfs();
for(int i =1 ;i <= n; ++ i)
{
for(int j =1 ;j <= n; ++ j)
{
if(a[i][j] == 0 && vis[i][j] == 0)
cout << "2 ";
else
cout << a[i][j] << " ";
}
cout << "\n";
}
return 0;
}