洛谷P1101单词方阵

简简单单的搜索,是为了练基础才做的。

搜素不一定要按照dfs的格式来写,就像我这样也是很好的。不要被格式套住了。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int w[8],l[8];
 4 bool vis[101][101];
 5 char a[101][101],op[7];
 6 int main(){
 7     int n;
 8     cin>>n;
 9     w[1]=0;l[1]=1;op[1]='i';//这样子出理比较方便,也少编了很多
10     w[2]=0;l[2]=-1;op[2]='z';
11     w[3]=1;l[3]=0;op[3]='h';
12     w[4]=1;l[4]=-1;op[4]='o';
13     w[5]=1;l[5]=1;op[5]='n';
14     w[6]=-1;l[6]=0;op[6]='g';
15     w[7]=-1;l[7]=-1;
16     w[8]=-1;l[8]=1;
17     for (int i=1;i<=n;i++)
18         for (int j=1;j<=n;j++)
19             cin>>a[i][j];
20     for (int i=1;i<=n;i++)
21         for (int j=1;j<=n;j++)
22               if (a[i][j]=='y'){
23                   for (int k=1;k<=8;k++){
24                       int numi=i,numj=j,pp=0;
25                       bool ok=true;
26                       if (i+6*w[k]>=1&&i+6*w[k]<=n&&j+6*l[k]>=1&&j+6*l[k]<=n){
27                       while (pp<=5&&ok){
28                           numi=numi+w[k];
29                           numj=numj+l[k];
30                           ++pp;
31                           if (a[numi][numj]==op[pp]) continue;
32                           else ok=false;
33                     }
34                     if (ok){
35                         pp=0;numi=i;numj=j;
36                         while (pp<=6){
37                             vis[numi][numj]=true;
38                             numi+=w[k];
39                             numj+=l[k];
40                             ++pp;
41                         }
42                     }
43                     }
44                 }
45             }
46     for (int i=1;i<=n;i++){
47         for (int j=1;j<=n;j++)
48             if (vis[i][j]) cout<<a[i][j];
49             else cout<<"*";
50         cout<<""<<endl;//好像cout<<endl;是不允许的
51     }
52 }

猜你喜欢

转载自www.cnblogs.com/fnbk/p/9341866.html