其他分享
首页 > 其他分享> > P1101单词方阵

P1101单词方阵

作者:互联网

一.题目描述:

 

 

 

 二.解题思路:

可以先建立一个数组存储要匹配的字符串“yizhong",然后八个方向搜索就行了。

三.代码实现:

  1 #include "bits/stdc++.h"
  2 using namespace std;
  3 int mp[110][110];
  4 char cs[110][110];
  5 int road[10][2];
  6 char s[10]= {'y','i','z','h','o','n','g'};
  7 int n;
  8 void xdfs(int sx,int sy,int f)
  9 {
 10     if(cs[sx][sy] != s[f]) return;
 11     int dx = sx + 1;
 12     int dy = sy + 0;
 13     road[f][0] = sx;
 14     road[f][1] = sy;
 15     if(f == 6){
 16         for(int i = 0;i < 7;i++)
 17             mp[road[i][0]][road[i][1]] = 1;
 18         return;
 19     }
 20     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
 21     xdfs(dx,dy,f+1);
 22 }
 23 
 24 void sdfs(int sx,int sy,int f)
 25 {
 26     if(cs[sx][sy] != s[f]) return;
 27     road[f][0] = sx;
 28     road[f][1] = sy;
 29     int dx = sx - 1;
 30     int dy = sy + 0;
 31     if(f == 6){
 32         for(int i = 0;i < 7;i++)
 33             mp[road[i][0]][road[i][1]] = 1;
 34         return;
 35     }
 36     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
 37     sdfs(dx,dy,f+1);
 38 }
 39 void zdfs(int sx,int sy,int f)
 40 {
 41     if(cs[sx][sy] != s[f]) return;
 42     road[f][0] = sx;
 43     road[f][1] = sy;
 44     if(f == 6){
 45         for(int i = 0;i < 7;i++)
 46             mp[road[i][0]][road[i][1]] = 1;
 47         return;
 48     }
 49     int dx = sx + 0;
 50     int dy = sy - 1;
 51     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
 52     zdfs(dx,dy,f+1);
 53 }
 54 void ydfs(int sx,int sy,int f)
 55 {
 56     if(cs[sx][sy] != s[f]) return;
 57     road[f][0] = sx;
 58     road[f][1] = sy;
 59     if(f == 6){
 60         for(int i = 0;i < 7;i++)
 61             mp[road[i][0]][road[i][1]] = 1;
 62         return;
 63     }
 64     int dx = sx + 0;
 65     int dy = sy + 1;
 66     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
 67     ydfs(dx,dy,f+1);
 68 }
 69 void xsdfs(int sx,int sy,int f)
 70 {
 71     if(cs[sx][sy] != s[f]) return;
 72     road[f][0] = sx;
 73     road[f][1] = sy;
 74     int dx = sx - 1;
 75     if(f == 6){
 76         for(int i = 0;i < 7;i++)
 77             mp[road[i][0]][road[i][1]] = 1;
 78         return;
 79     }
 80     int dy = sy + 1;
 81     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
 82     xsdfs(dx,dy,f+1);
 83 }
 84 void xxdfs(int sx,int sy,int f)
 85 {
 86     if(cs[sx][sy] != s[f]) return;
 87     road[f][0] = sx;
 88     road[f][1] = sy;
 89     int dx = sx + 1;
 90     if(f == 6){
 91         for(int i = 0;i < 7;i++)
 92             mp[road[i][0]][road[i][1]] = 1;
 93         return;
 94     }
 95     int dy = sy - 1;
 96     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
 97     xxdfs(dx,dy,f+1);
 98 }
 99 void xzdfs(int sx,int sy,int f)
100 {
101     if(cs[sx][sy] != s[f]) return;
102     road[f][0] = sx;
103     road[f][1] = sy;
104     if(f == 6){
105         for(int i = 0;i < 7;i++)
106             mp[road[i][0]][road[i][1]] = 1;
107         return;
108     }
109     int dx = sx - 1;
110     int dy = sy - 1;
111     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
112     xzdfs(dx,dy,f+1);
113 }
114 void xydfs(int sx,int sy,int f)
115 {
116     if(cs[sx][sy] != s[f]) return;
117     road[f][0] = sx;
118     road[f][1] = sy;
119     if(f == 6){
120         for(int i = 0;i < 7;i++)
121             mp[road[i][0]][road[i][1]] = 1;
122         return;
123     }
124     int dx = sx + 1;
125     int dy = sy + 1;
126     if(dx < 0 || dx >= n || dy < 0 || dy >= n) return;
127     xydfs(dx,dy,f+1);
128 }
129 int main()
130 {
131     cin >> n;
132     getchar();
133     for(int i = 0;i < n;i++){
134         for(int j = 0;j < n;j++)
135             cin >> cs[i][j];
136         getchar();
137     }
138     for(int i = 0;i < n;i++)
139         for(int j = 0;j < n;j++){
140            xydfs(i,j,0);
141            xzdfs(i,j,0);
142            xsdfs(i,j,0);
143            xxdfs(i,j,0);
144            sdfs(i,j,0);
145            xdfs(i,j,0);
146            zdfs(i,j,0);
147            ydfs(i,j,0);
148         }
149     for(int i = 0;i < n;i++){
150         for(int j = 0;j < n;j++)
151             if(mp[i][j]) cout << cs[i][j];
152             else cout << '*';
153         cout << endl;
154     }
155 }

 

标签:sy,sx,P1101,int,单词,dx,dy,方阵,road
来源: https://www.cnblogs.com/scannerkk/p/15782201.html