洛谷 P1101 单词方阵(dfs)

嗯...

 

题目链接:https://www.luogu.org/problem/P1101

 

这道题与普通的方向dfs只需确定它dfs的方向,然后用一个结构体记录路径,用一个数组确定它搜索的方向。

 

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 int n;
 7 char mp[105][105];
 8 int vis[105][105];
 9 string a = "yizhong";
10 int dir[8][2] = {{1, 0}, {-1, 0}, {1, -1}, {-1, 1}, {1, 1}, {-1, -1}, {0, 1}, {0, -1}};
11 
12 struct node{
13     int x, y;
14 } c[105];
15 
16 inline int dfs(int x, int y, int cur, int d1, int d2){
17     if(cur == 7){
18         for(int i = 0; i <= 6; i++){
19             vis[c[i].x][c[i].y] = 1;
20         }
21     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
22     else{
23         int nx = x + d1;
24         int ny = y + d2;
25         if(mp[nx][ny] == a[cur+1] || cur == 6){
26             c[cur].x = x;
27             c[cur].y = y;
28             dfs(nx, ny, cur+1, d1, d2);
29         }
30     }
31 }
32 
33 int main(){
34     scanf("%d", &n);
35     for(int i = 0; i < n; i++){
36         for(int j = 0; j < n; j++){
37             cin >> mp[i][j];
38         }
39     }
40     for(int i = 0; i < n; i++){
41         for(int j = 0; j < n; j++){
42             if(mp[i][j] == 'y'){
43                 for(int x = 0; x < 8; x++){
44                     int nx = i + dir[x][0];
45                     int ny = j + dir[x][1];
46                     if(mp[nx][ny] == 'i'){
47                         c[0].x = i; c[0].y = j; c[1].x = nx; c[1].y = ny;
48                         //vis[nx][ny] = 1;
49                         dfs(nx, ny, 1, dir[x][0], dir[x][1]);
50                     }
51                 }
52             }
53         }
54     }
55     for(int i = 0; i < n; i++){
56         for(int j = 0; j < n; j++){
57             if(vis[i][j]) cout << mp[i][j];
58             else printf("*");
59         }
60         cout << endl;
61     }
62     return 0;
63 }
AC代码

 

posted @ 2019-11-22 21:12  dfydn  阅读(132)  评论(0编辑  收藏  举报