样例二的‘斜着的y’过不了啊,只有这个y进不了队列
  • 板块P1101 单词方阵
  • 楼主limcqt
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/4/6 00:46
  • 上次更新2024/4/6 10:17:47
查看原帖
样例二的‘斜着的y’过不了啊,只有这个y进不了队列
983560
limcqt楼主2024/4/6 00:46
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
int n;
char arr[100][100];
int g[100][100];
char brr[]={'y','i','z','h','o','n','g'};
int dx[]={-1,-1,0,1,1,1,0,-1};
int dy[]={0,1,1,1,0,-1,-1,-1};
typedef pair<int,int>PII;
queue<PII>q;
int ans=0;
int temp;
void dfs(int x,int y,int count)
{
    for(int i=0;i<8;i++)
    {
        int a=x+dx[i];
        int b=y+dy[i];
        if(a<1||b<1||b>n||a>n)continue;
        if(count+1==1&&arr[a][b]=='i')
        {
            temp=i; 
            q.push({a,b});
            dfs(a,b,count+1);
        }
        else if(count+1>=2&&count+1<6)
        {
        	if(arr[a][b]==brr[count+1]&&i==temp)
        	{
        	q.push({a,b});
            dfs(a,b,count+1);
			}
		}
        else if(arr[a][b]=='g'&&count==5&&temp==i)
        {
        //	ans++;
        	//cout<<"ans::"<<ans<<endl;
            q.push({a,b});
         //   cout<<q.size()<<endl;
            while(q.size())
            {         	
                PII t=q.front();
               // cout<<t.first<<" "<<t.second<<endl;
               g[t.first][t.second]=1;
                q.pop();
                
            }
            //cout<<endl;
            return;
        }
    }
    while(q.size())
    {
        q.pop();
    }
}
int main()
{
	//q.push({2,2});
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        cin>>arr[i][j];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(arr[i][j]=='y')
            {
            //	cout<<i<<" "<<j<<endl;
            //	q.push({i,j});
            	q.push({i,j});
            	dfs(i,j,0);
			}
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(g[i][j]==1)
            {
                cout<<arr[i][j];
            }
            else cout<<'*';
        }
        cout<<endl;
    }
    return 0;
}
2024/4/6 00:46
加载中...