P1101 Word square matrix【DFS】

https://www.luogu.com.cn/problem/P1101

Title description

Given a square matrix of letters n \times nn×n, it may contain multiple " yizhong" words. The words are placed consecutively in the same direction in the square matrix. The placement can be along any of the 88 directions. The same word does not change the direction when placed, and words can be crossed, so it is possible to share letters. When outputting, *replace letters that are not words to highlight words. E.g:

输入:
    8                     输出:
    qyizhong              *yizhong
    gydthkjy              gy******
    nwidghji              n*i*****
    orbzsfgz              o**z****
    hhgrhwth              h***h***
    zzzzzozo              z****o**
    iwdfrgng              i*****n*
    yyyygggg              y******g

Input format

Enter a number nn on the first line. (7 \le n \le 1007≤n≤100).

In the second line, input the letter matrix of n \times nn×n.

Output format

Highlight the n \times nn×n matrix of words.

Sample input and output

Enter #1 to copy

7
aaaaaaa
aaaaaaa
aaaaaaa
aaaaaaa
aaaaaaa
aaaaaaa
aaaaaaa

Output #1 copy

*******
*******
*******
*******
*******
*******
*******

Enter #2 to copy

8
qyizhong
gydthkjy
nwidghji
orbzsfgz
hhgrhwth
zzzzzozo
iwdfrgng
yyyygggg

Output #2 copy

*yizhong
gy******
n*i*****
o ** z ****
h***h***
with **** o **
i*****n*
y******g

Solution: Determine whether a certain point is the beginning of "yizhong", that is, whether it is "y". At this time, DFS can be performed directly in eight directions. You can also continue to determine which of the eight directions, the next character is'i', then DFS in this direction.

In DFS, you need to pay attention to how to save the path of "yizhong" currently traversed, here is saved by a path[ ], if the last traverse to'g', that is, when our traversal cnt==7, then path[ ] The coordinates inside are marked as 1, which means that these meet the requirements.

1. If it hasn't reached 7 times, it means that DFS still needs to continue. One is when the next character matches it, at this time, cnt+1, continue to traverse in this direction.

2. When we traverse to cnt == 6, we have already traversed the string. At this time, we need to save the path, that is, DFS is performed again, but the next one is not saved in vis.

3. You can also adjust the following code sequence, no longer recursive cnt==6 this time.

 

What I wrote is ugly. I wrote it again after reading the solution.

#include <stdio.h>
#include <string.h>
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
char s[200][200];
char parent[] = "yizhong";
int vis[200][200];
struct node {
    int x,y;
}path[10];
int dx[] = {1,1,1,0,0,-1,-1,-1};
int dy[] = {0,1,-1,1,-1,0,1,-1};
void dfs(int x,int y,int cnt, int k){
    if(cnt == 7){
        for(int i = 0; i < 7; i ++){
            vis[path[i].x][path[i].y] = 1;
        }
    }
    else {
        int tx = x + dx[k];
        int ty = y + dy[k];
        if(cnt == 6 || s[tx][ty] == parent[cnt+1]){
            path[cnt].x = x;
            path[cnt].y = y;
            dfs(tx,ty,cnt+1,k);
        }
    }
}
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        cin >> s[i];
    }
    memset(vis,0,sizeof(vis));
    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 ++){
                    int ti = i + dx[k];
                    int tj = j + dy[k];
                    if(s[ti][tj] == 'i'){
                        dfs(i,j,0,k);

                    }
                }
            }
        }
    }
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(vis[i][j]) cout << s[i][j];
            else cout << '*' ;
        }
        cout << endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/108903183