码迷,mamicode.com
首页 > 其他好文 > 详细

落谷P1101 单词方阵 //参考代码

时间:2017-08-11 23:07:58      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:表示   搜索   eof   using   ios   div   turn   style   nbsp   

 1 #include<iostream> 
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int N = 105;
 6 char s[N][N], a[] = "yizhong";
 7 bool vis[N][N];
 8 int n;
 9 int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
10     dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
11 void dfs(int x,int y,int w,int i)//x,y,表示坐标,w表示方向,i表示第几个字符。
12 {
13     if(w==-1)//如果是第一次找
14     {
15         for(int j=0;j<8;j++)
16         {
17             int xx=x+dx[j];
18             int yy=y+dy[j];
19             if(s[xx][yy]==a[i]) 
20               dfs(xx,yy,j,i+1);//扩展搜索;
21         }
22         return;
23     }
24     if(i==7)//如果已经找到这个字符串,回溯标记。
25     {
26         int xx=x,yy=y;//目前的位置给xx,yy 
27         for(int j=1;j<=7;j++)
28          {
29                vis[xx][yy]=1;//标记找完的位置 
30                xx=xx-dx[w];
31                yy=yy-dy[w];
32          }
33          return;
34     }
35     int xx=x+dx[w];
36     int yy=y+dy[w];
37     if(s[xx][yy]==a[i])
38       dfs(xx,yy,w,i+1);
39 }
40 int main()
41 {
42     memset(s,*,sizeof s);
43     scanf("%d",&n);
44     for(int i=1;i<=n;i++)
45       scanf("%s",s[i]+1);
46     for(int i=1;i<=n;i++)//搜索到的开始字母 
47     {
48         for(int j=1;j<=n;j++)
49           if(s[i][j]==y) dfs(i,j,-1,1);
50     }
51     for(int i=1;i<=n;i++)//输出 
52  {
53        cout<<endl;
54       for(int j=1;j<=n;j++) 
55       {
56          if(vis[i][j]) cout<<s[i][j];
57          else cout<<*;
58       }}
59  return 0;    
60 }

 

落谷P1101 单词方阵 //参考代码

标签:表示   搜索   eof   using   ios   div   turn   style   nbsp   

原文地址:http://www.cnblogs.com/zzy-ak/p/7348214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!