0pts,RE

P8662 [蓝桥杯 2018 省 AB] 全球变暖

luogu_hezhenmin1 @ 2024-09-28 17:28:22

rt

#include<bits/stdc++.h>
using namespace std;
int n,cnt=0;
bool vis[1002][1002],vk[1002][1002];
queue<pair<int,int> >q;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            char c;
            vis[i][j]=0;vk[i][j]=0;
            cin>>c;
            if(c=='#') vis[i][j]=1;
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(vis[i][j]==1 and vk[i][j]==0){
                vk[i][j]=1;
                q.push({i,j});
                while(!q.empty()){
                    pair<int,int>bk=q.front();q.pop();
                    int x=bk.first,y=bk.second;
                    bool f=1;
                    for(int k=0;k<=3;i++){
                        int nx=x+dx[i],ny=y+dy[i];
                        if(!vis[nx][ny]) f=0;
                        if(nx>0 and ny>0 and nx<=n and ny<=n and vis[nx][ny]==1 and !vk[nx][ny]){
                           q.push({nx,ny});
                           vk[nx][ny]=1;
                        }
                    }
                    if(f) cnt++;
                }
            }
    cout<<cnt;
    return 0;
}

by luogu_hezhenmin1 @ 2024-09-28 17:37:46

已微调
12pts,WA

#include<bits/stdc++.h>
using namespace std;
int n,cnt=0;
bool vis[1002][1002],vk[1002][1002];
queue<pair<int,int> >q;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            char c;
            vis[i][j]=0;vk[i][j]=0;
            cin>>c;
            if(c=='#') vis[i][j]=1,cnt++;
        }
    /*for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cout<<vis[i][j];
        cout<<endl;
    }*/
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(vis[i][j]==1 and vk[i][j]==0){
                vk[i][j]=1;
                q.push({i,j});
                while(!q.empty()){
                    pair<int,int>bk=q.front();q.pop();
                    int x=bk.first,y=bk.second;
                    bool f=1;
                    for(int k=0;k<4;k++){
                        int nx=x+dx[k],ny=y+dy[k];
                        if(vis[nx][ny]==0) f=0;
                        if(nx>0 and ny>0 and nx<=n and ny<=n and vis[nx][ny]==1 and !vk[nx][ny]){
                            q.push({nx,ny});
                            vk[nx][ny]=1;
                        }
                    }
                    if(!f) cnt--;
                }
            }
    cout<<cnt;
    return 0;
}

by luogu_hezhenmin1 @ 2024-09-28 17:38:34

@luogu_hezhenmin1 只AC了#2


by claoday @ 2024-09-28 17:41:09

@luogu_hezhenmin1

你第26行的 int nx=x+dx[i],ny=y+dy[k] 应该为 int nx=x+dx[k],ny=y+dy[k];。因为你枚举方向用的是 for(int k=0;k<=3;k++) 所以 dx[]dy[] 数组应该是 dx[k]dy[k]

求关


by luogu_hezhenmin1 @ 2024-09-28 17:44:13

你会发现他只得了12pts


by luogu_hezhenmin1 @ 2024-09-28 17:44:51

@luogu_hezhenmin1 已AC
此贴结


|