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

dfs

时间:2017-02-09 12:56:49      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:blog   函数   png   log   style   can   printf   http   ems   

技术分享

技术分享

技术分享

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char mat[11][11]; // 地图信息
 5 int ans, n, m; // ans 用来记录最后答案 n, m 表示行列
 6 int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};  //对应的上下左右4各方向
 7 int vis[11][11]; // 用来标记是否访问
 8 void dfs(int x, int y) {
 9    // 完成 dfs 函数最后的方案数量保存在 ans 变量中   
10     if(mat[x][y]==T)
11     {
12         ans++;
13         return;
14     }
15     else
16     {
17         for(int i=0;i<4;i++)
18         {
19             int next_x=x+dir[i][0];
20             int next_y=y+dir[i][1];
21             if(next_x>n-1||next_y>m-1||next_x<0||next_y<0) continue;
22             if(!vis[next_x][next_y]&&mat[next_x][next_y]!=#)
23             {
24                 vis[x][y]=1;
25                 dfs(next_x,next_y);
26                 vis[x][y]=0;
27             }
28         }
29         return;
30     }
31 }
32 int main() {
33     scanf("%d %d", &n, &m);
34     for (int i = 0; i < n; ++i) {
35         scanf("%s", mat[i]);
36     }
37     int x, y;
38     for (int i = 0; i < n; ++i) {
39         for (int j = 0; j < m; ++j) {
40             if (mat[i][j] == S) {
41                 x = i, y = j;
42             }
43         }
44     }
45     memset(vis, 0, sizeof vis);
46     ans = 0;
47     dfs(x, y);
48     printf("%d\n", ans);
49     return 0;
50 }

 

dfs

标签:blog   函数   png   log   style   can   printf   http   ems   

原文地址:http://www.cnblogs.com/wangkaipeng/p/6381594.html

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