HDU 1312 DFS
2016-07-22 21:37
302 查看
算法基础 DFS 深度搜索hdu1312 Red and Black下面是题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1312
这道题的题意大致是你在@点处,’.’是能走的,’#’是不能走的,然后问你能走过的最多的’.’的步数。典型的DFS题目。
“`
http://acm.hdu.edu.cn/showproblem.php?pid=1312
这道题的题意大致是你在@点处,’.’是能走的,’#’是不能走的,然后问你能走过的最多的’.’的步数。典型的DFS题目。
``` #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> using namespace std ; int sx, sy; char maze[100][100]; int n, m; //迷宫的长和宽 int ans; // 最终走的步数 int dirt[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; void dfs(int x, int y) { if(x < 0 || x >= n || y < 0 || y >= m) return ; if(maze[x][y] == '#') return ; ans++; for(int i = 0 ; i < 4 ; i++){ int tx = x + dirt[i][0]; int ty = y + dirt[i][1]; maze[x][y] = '#';//每走过一个'.'就吧它标记为不能走的'#'然后ans就会加一 dfs(tx, ty); } } int main() { while(cin >> m >> n && (n||m)){ ans = 0 ; for(int i = 0 ; i < n ; i++) cin >> maze[i]; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < m ; j++){ //scanf("%s", &maze[i][j]); if(maze[i][j] == '@') sx = i , sy = j ; } } dfs(sx, sy); cout << ans << endl; } system("pause"); return 0 ; }
“`
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- 书评:《算法之美( Algorithms to Live By )》
- IE:使用搜索助手
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解