请求加强数据

P1162 填涂颜色

cannotlauchdemo_exe @ 2024-11-09 14:11:42

错误思路ac,这是不合理的

#include <bits/stdc++.h>

#define int long long
#define eb emplace_back
#define pb push_back
#define endl '\n'
#define rep1(i, a, b) for (int i = (a); i <= (b); i++)
#define rep2(i, b, a) for (int i = (b); i >= (a); i--)

using ll = long long;
using ull = unsigned long long;
using namespace std;

constexpr double PI = 3.14159265358979323846;
constexpr ll llmax = LLONG_MAX / 2;
constexpr ll llmin = LLONG_MIN / 2;

template <typename T> bool chkmin(T &a, T b)
{
    return (b < a) ? a = b, 1 : 0;
}

template <typename T> bool chkmax(T &a, T b)
{
    return (b > a) ? a = b, 1 : 0;
}

template <typename T> void readVectorWithLength(std::vector<T> &vec, int n)
{
    vec.resize(n);
    std::copy_n(std::istream_iterator<T>(std::cin), n, vec.begin());
}

template <typename T> void readVectorWithComma(std::vector<T> &vec)
{
    std::string input;
    std::getline(std::cin, input);
    std::stringstream ss(input);
    for (T num; ss >> num; vec.push_back(num))
    {
        ss.ignore(1, ',');
    }
}

template <typename T> void readVectorNoLength(std::vector<T> &vec)
{
    for (T num; std::cin >> num; vec.push_back(num))
    {
        if (std::cin.peek() == '\n')
            break;
    }
}

void solve();

signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    while (T--)
    {
        solve();
    }

    return 0;
}
int n;
int a[40][40];
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
void dfs(int x, int y)
{
    if (x < 1 || x > n || y < 1 || y > n)
        return;
    if (a[x][y] != 0)
        return;
    a[x][y] = 2;
    for (int i = 0; i < 4; i++)
    {
        dfs(x + dx[i], y + dy[i]);
    }
}
bool vali(int x, int y)
{
    bool up = false, down = false, left = false, rig = false;
    for (int i = 1; i <= x - 1; i++)
    {
        if (a[i][y] == 1)
        {
            up = true;
            break;
        }
    }
    for (int i = x + 1; i <= n; i++)
    {
        if (a[i][y] == 1)
        {

            down = true;
            break;
        }
    }
    for (int i = 1; i <= y - 1; i++)
    {
        if (a[x][i] == 1)
        {

            left = true;
            break;
        }
    }
    for (int i = y + 1; i <= n; i++)
    {
        if (a[x][i] == 1)
        {

            rig = true;
            break;
        }
    }
    if (up && down && left && rig)
        return true;
    else
        return false;
}
void solve()
{
    //
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
        }
    }
    bool bk = false;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (a[i][j] == 0)
            {
                if (vali(i, j) == true)
                {
                    dfs(i, j);
                    bk = true;
                    break;
                }
            }
        }
        if (bk == true)break;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

by cyfcyf0405 @ 2024-11-28 18:50:16

请求相同

#include<bits/stdc++.h>
using namespace std;
int n,a[40][40]={0},b[40][40]={0};
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            b[i][j]=a[i][j];
        }
    }
    for(int i=0;i<=n+1;i++){
        for(int j=0;j<=n+1;j++){
            if(i==0||j==0||i==n+1||j==n+1){
                a[i][j]=3;
            }
            else if(a[i][j]==1)continue;
            else if(a[i-1][j]==3||a[i][j-1]==3){
                a[i][j]=3;
            }
            else if(a[i][j+1]==3||a[i+1][j]==3){
                a[i][j]=3;
            }
        }
    }
    for(int i=n+1;i>=0;i--){
        for(int j=n+1;j>=0;j--){
            if(i==0||j==0||i==n+1||j==n+1){
                a[i][j]=3;
            }
            else if(a[i][j]==1)continue;
            else if(a[i-1][j]==3||a[i][j-1]==3){
                a[i][j]=3;
            }
            else if(a[i][j+1]==3||a[i+1][j]==3){
                a[i][j]=3;
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]==0) b[i][j]=2;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<b[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

纯暴力扫两边也能过???


|