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

仙岛求药

时间:2018-07-28 23:26:53      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:can   font   mat   size   题解   include   code   else   小姑娘   

我真菜,真的。

我不仅很菜,而且很矬;

所以写这篇题解的目的是想证明我很菜;

但怎么证明我很矬呢;

只要去我们班的群相册里看一下;

就会发现,我是众多小姑娘中最矬的那一个;

这道题在大家都轻松A掉的时候,我还一直WA;

我又菜又矬,真的。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 char s[23][23];
 8 int st[23][23];
 9 int i,j,m,n,sx,sy,tx,ty,ans = 0;
10 void bfs(int sx,int sy)
11 {
12     int t = st[sx][sy];
13     if(sx == tx && sy == ty)
14         ans = t;
15     t++;
16     if(sx + 1 < m && s[sx+1][sy] != # && st[sx+1][sy] > t)
17     {
18         st[sx + 1][sy] = t;
19         bfs(sx+1,sy);
20     }
21     if(sx - 1 >= 0 && s[sx-1][sy] != # && st[sx-1][sy] > t)
22     {
23         st[sx - 1][sy] = t;
24         bfs(sx-1,sy);
25     }
26     if(sy + 1 < n && s[sx][sy+1] != # && st[sx][sy+1] > t)
27     {
28         st[sx][sy+1] = t;
29         bfs(sx,sy+1);
30     }
31     if(sy-1 >= 0 && s[sx][sy-1] != # && st[sx][sy-1] > t)
32     {
33         st[sx][sy-1] = t;
34         bfs(sx,sy-1);
35     }
36 }
37 int main()
38 {
39     while(scanf("%d%d",&m,&n) != EOF)
40     {
41         if(m == 0 && n == 0) break;
42         for(i = 0;i < m;i++)
43             for(j = 0;j < n;j++)
44             {
45                 cin >> s[i][j];
46                 if(s[i][j] == @)
47                 {
48                     sx = i;
49                     sy = j;
50                 }
51                 if(s[i][j] == *)
52                 {
53                     tx = i;
54                     ty = j;
55                 }
56             }
57         memset(st,0x7f,sizeof(st));
58         st[sx][sy] = 0;
59         bfs(sx,sy);
60         if(ans != 0) printf("%d\n",ans);
61         else printf("-1\n");
62         ans = 0;
63     }
64     return 0;
65 }

这道题用广搜(深搜会T(吧)),然后思路是比当前解优就更新。

 

仙岛求药

标签:can   font   mat   size   题解   include   code   else   小姑娘   

原文地址:https://www.cnblogs.com/peppa/p/9383737.html

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