P1162警示!!!!!!!!!!!!!!!!!!!!!!!!!!

P1162 填涂颜色

Li_Bingze @ 2024-02-22 08:34:28

此题坐标增量一定要是

dx[4]={1,-1,0,0};

dy[4]={0,0,-1,1}; 否则会有一个RE


by linch @ 2024-02-22 08:42:40

@Li_Bingze 不然呢?一般都这样写的呀


by Li_Bingze @ 2024-02-22 09:49:03

@linch 是的呢小哥哥


by linch @ 2024-02-22 09:53:57

@Li_Bingze 那警示干嘛


by Li_Bingze @ 2024-02-22 10:14:16

@linch 警示某些somebody


by sir_carrot @ 2024-02-22 10:14:33

因为我就错在这里


by Li_Bingze @ 2024-02-22 10:15:28

@sir_carrot 谢谢助攻


by sir_carrot @ 2024-02-22 10:16:20

#include <iostream>
using namespace std;
int n,a[31][31],b[31][31],c[31][31],dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
int dfs(int x,int y){
    for(int i=0;i<=3;i++){
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(xx<1||xx>n||yy<1||yy>n){
            return 0;
        }
        if(!b[xx][yy]&&a[xx][yy]==0){
            b[xx][yy]=1;
            if(!dfs(xx,yy)){
                return 0;
            }
        }
    }
    return 1;
}
void two(int x,int y){
    for(int i=0;i<=3;i++){
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(!c[xx][yy]&&a[xx][yy]==0){
            c[xx][yy]=1;
            a[xx][yy]=2;
            two(xx,yy);
        }
    }
}
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++){
            if(!b[i][j]&&a[i][j]==0){
                b[i][j]=1;
                if(dfs(i,j)){
                    a[i][j]=2;
                    two(i,j);
                }
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

by sir_carrot @ 2024-02-22 10:19:45

证据


by Lazy_make_name @ 2024-03-28 22:30:31

@sir_carrot 你这样写太麻烦了,本题数据范围不大,你可以抄我的

#include<iostream>
using namespace std;
int t[35][35];
int main(){
    int n,shu;
    cin>>n;
    for(int i=2;i<=n+1;i++)
        for(int j=2;j<=n+1;j++){
            cin>>t[i][j];
            if(t[i][j]==0)
                t[i][j]=2;
            }
    for(int ci=1;ci<=900;ci++)
        for(int i=1;i<=n+2;i++)
            for(int j=1;j<=n+2;j++)
                if(t[i][j]==0){
                    if(t[i+1][j]!=1)
                        t[i+1][j]=0;
                    if(t[i-1][j]!=1)
                        t[i-1][j]=0;
                    if(t[i][j+1]!=1)
                        t[i][j+1]=0;
                    if(t[i][j-1]!=1)
                        t[i][j-1]=0;
                }   
    for(int i=2;i<=n+1;i++){
        for(int j=2;j<=n+1;j++)
            cout<<t[i][j]<<" ";
        cout<<endl;
    }
} 

by huashuo @ 2024-04-02 23:07:59

@Li_Bingze

能帮我看看我这个为啥不对吗 只过了两个点

#include<bits/stdc++.h>
using namespace std;
int Map[32][32];
queue<int> nowx;
queue<int> nowy;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int vis[32][32];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            scanf("%d", &Map[i][j]);
        }
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(Map[i][j] == 0)
            Map[i][j] = 2;
        }
    }
    for(int i = 1; i <= n; i++) if(Map[1][i] == 2)Map[1][i] = 0;
    for(int i = 1; i <= n; i++) if(Map[i][1] == 2)Map[i][1] = 0;
    for(int i = 1; i <= n; i++) if(Map[i][n] == 2)Map[i][n] = 0;
    for(int i = 1; i <= n; i++) if(Map[n][i] == 2)Map[n][i] = 0;
    vis[1][1] = 1;
    nowx.push(1);
    nowy.push(1);
    while(!nowx.empty())
    {
        for(int i = 0; i < 4; i++)
        {
            int nextx = nowx.front() + dx[i];
            int nexty = nowy.front() + dy[i];
            if(Map[nextx][nexty]!=1 && vis[nextx][nexty] == 0 
                && nextx >=1 && nextx <= n
                && nexty >=1 && nexty <= n)
            {
                vis[nextx][nexty] = 1;
                Map[nextx][nexty] = 0;
                nowx.push(nextx);
                nowy.push(nexty);
            }
        }
        nowx.pop();
        nowy.pop();
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        printf("%d ",Map[i][j]);
    printf("\n");
    }
    return 0;

}

|