一个新奇的思路,向大佬求助!

P1162 填涂颜色

1901吴昊 @ 2021-10-12 22:06:02

#include <bits/stdc++.h>
using namespace std;
int a[35][35];
int main(){

    freopen("P1162_2.in","r",stdin);

    int n,m=0;
    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(m==0)
            {
                if(a[i][j]==1)
                {
                    m=1;
                }
            }
            else if(m==1)
            {
                if(a[i][j]==1&&a[i+1][j]==0)
                {
                    m=0;
                }
                else if(a[i][j]==0)
                {
                    a[i][j]=2;
                }
            }
            cout<<a[i][j]<<" ";
        }
        m=0;
        cout<<endl;
    }
    return 0;
}

一个新奇的思路,希望有大佬帮我改代码(35分)


by 1901吴昊 @ 2021-10-12 22:07:02

测试点2:

6


by queen_street @ 2021-10-13 19:57:51

你跟我的思路还挺像的,贴一下我的AC代码吧(滑稽

#include<bits/stdc++.h>
using namespace std;

int a[31][31],n;

int pd(int x,int y)
{
    int i,j;
    for(j=y;j<=n;j++)
        if(a[x][j]==1)
            return 1;
    return 0;
}

int pd2(int x,int y)
{
    int i,j;
    for(i=x;i<=n;i++)
        if(a[i][y]==1)
            return 1;
    return 0;
}

int pd3(int x,int y)
{
    int i,j;
    for(i=x;i>=1;i--)
        if(a[i][y]==1)
            return 1;
    return 0;
}

int pd4(int x,int y)
{
    int i,j;
    for(j=y;j>=1;j--)
        if(a[x][j]==1)
            return 1;
    return 0;
}

int check(int x,int y)
{
    if(a[x][y]==2 && (a[x+1][y]==0 || a[x][y+1]==0 || a[x-1][y]==0 || a[x][y-1]==0))
        return 1;
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,j;
    cin>>n;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            cin>>a[i][j];
    for(i=2;i<=n;i++)
    {
        int flag=0;
        for(j=1;j<=n;j++)
        {
            int x=0;
            if(flag==2 && a[i][j]==1 && a[i][j-1]!=1)
                flag=0;
            if(flag==0 && a[i][j]==1)
                flag=2;
            else if(flag==2 && a[i][j]!=1 && i<n && j<n)
                if(pd(i,j)==1 && pd2(i,j)==1 && pd3(i,j)==1 && pd4(i,j)==1)
                    a[i][j]=2;
        }
    }
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(check(i,j)==1)
                a[i][j]=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}

我已经尽力压行了,但还是这么长鬼知道我下载了多少测试点,因为这题踩得点太多了。。。


by queen_street @ 2021-10-13 19:58:37

@1901吴昊


by 1901吴昊 @ 2021-10-22 22:33:22

@游走的灵魂 我是认为一旦一个围起来的空间,它一定会有1个开始的1和一个结束的1出现在同一行内当然这两可以是同一个

1 1 1
1 0 1
1 1 1

这样才可能是封闭空间,我的代码就基于这个


by queen_street @ 2021-10-24 21:46:44

az


by splendid_followers @ 2022-03-04 12:43:02

@1901吴昊 如果出现

00000000 01100110 10011001 10010001 11111111 这样的情况呢?


by WHH_FZZ @ 2022-08-09 21:34:37

测试点4 :

输入
20
0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0
1 1 1 1 1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 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