洛谷 P1101 单词方阵(dfs)

https://www.luogu.org/problem/P1101

八个方向中的任一方向有没有出现过"yizhong"单词

// luogu-judger-enable-o2
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long lll;
const int maxn = 200005;
const lll INF = 0x3f3f3f3f3f;
string s[105];
int vis[105][105],n;
int dir[8][2]= {0,1,0,-1,1,0,-1,0,-1,-1,1,-1,-1,1,1,1};
char a[10]= {"yizhong"};
bool flag;
void dfs(int x,int y,int k,int kk)
{
    if(kk > 6) flag =1;
    else
    {
        int fx = x+dir[k][0],fy = y+dir[k][1];
        if(fx>=0&&fx<n&&fy>=0&&fy<n&&s[fx][fy] == a[kk])
        {
            dfs(fx,fy,k,kk+1);
            if(flag == 1) vis[fx][fy]=1;
        }
    }
}
int main()
{
    cin >> n;
    for(int i=0; i<n; i++) cin >> s[i];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++) if(s[i][j] == 'y')
            {
                for(int k=0; k<8; k++)
                    {
                    flag =0;
                    dfs(i,j,k,1);
                    if(flag == 1) vis[i][j] =1;
                    }
            }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(vis[i][j] == 1) cout << s[i][j];
            else cout << "*" ;
        }
        cout << endl;
    }
    return 0;
}

 

posted on 2019-08-03 10:35  一只小毛球  阅读(152)  评论(0编辑  收藏  举报

导航