这两份代码不是一样的吗,为什么手写队列会RE一个点啊,搞不明白
查看原帖
这两份代码不是一样的吗,为什么手写队列会RE一个点啊,搞不明白
1522128
qwq57楼主2025/1/21 16:05
#include<iostream>
#include<queue>
//模拟队列
using namespace std;
const int N=1050;

int n,m,mp[N][N],cnt=0;
int t1=0,h1=1,t2=0,h2=1;
int fx[]={1,0,0,-1};
int fy[]={0,1,-1,0};
queue<int>q1,q2;

// void push(int x)
// {
//     q[++tt]=x;
// }

// void pop()
// {
//     hh++;
// }

int main()
{
    std::ios_base::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            char t;
            cin>>t;
            if(t!='0')
            {
                mp[i][j]=1;
            }
        }
    }
    
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j])
            {
                cnt++;
                q1.push(i);
                q2.push(j);
                mp[i][j]=0;
                while(!q1.empty())
                {
                    for(int i=0;i<4;i++)
                    {
                        int tx=q1.front()+fx[i];
                        int ty=q2.front()+fy[i];
                        if(mp[tx][ty])
                        {
                            mp[tx][ty]=0;
                            q1.push(tx);
                            q2.push(ty);
                        }
                    }
                    q1.pop();
                    q2.pop();
                }
                
            }
            
        }
    }
    
    
    cout<<cnt<<endl;;
    
    return 0;
}

#include<iostream>
//模拟队列
using namespace std;
const int N=205;

int n,m,mp[N][N],cnt=0;
int q1[N],q2[N],t1=0,h1=1,t2=0,h2=1;
int fx[]={1,0,0,-1};
int fy[]={0,1,-1,0};

// void push(int x)
// {
//     q[++tt]=x;
// }

// void pop()
// {
//     hh++;
// }

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            char t;
            cin>>t;
            if(t!='0')
            {
                mp[i][j]=1;
            }
        }
    }
    
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j])
            {
                cnt++;
                q1[++t1]=i;
                q2[++t2]=j;
                mp[i][j]=0;
                while(t1>=h1)
                {
                    for(int k=0;k<4;k++)
                    {
                        int tx=q1[h1]+fx[k];
                        int ty=q2[h2]+fy[k];
                        if(mp[tx][ty])
                        {
                            mp[tx][ty]=0;
                            q1[++t1]=tx;
                            q2[++t2]=ty;
                        }
                    }
                    h1++;
                    h2++;
                    
                }
                
            }
            
        }
    }
    
    
    cout<<cnt;
    
    return 0;
}
2025/1/21 16:05
加载中...