sdutoj 1269 走迷宫
2013-10-04 13:20
316 查看
题目链接:
sdutoj 1269 走迷宫
sdutoj 1269 走迷宫
/* 题目描述 有一个m*n格的迷宫(表示有m行、n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,输入这m*n个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-1表示无路)。 输入 第一行是两个数m,n(1< m, n< 15),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。 输出 所有可行的路径,输出时按照左上右下的顺序。描述一个点时用(x,y)的形式,除开始点外,其他的都要用“->”表示。如果没有一条可行的路则输出-1。 示例输入 5 4 1 1 0 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 5 4 示例输出 (1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) (1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) 【思路】 DFS */ #include <iostream> #include <string> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> #include <stack> using namespace std; const int SIZE = 102; //边界数组,四个方向,按照下、右、上、左的顺序 int coordinate[4][2] = {0,-1, -1,0, 0,1, 1,0}; stack<int> sx; stack<int> sy; stack<int> sxCopy; stack<int> syCopy; int mazeDfs[SIZE][SIZE]; //深搜用的迷宫 int n; //迷宫行数 int m; //迷宫列数 int p, q; //小鼠a的行号和列号 int r, s; //小鼠b的行号和列号 int pathNumber; //深搜求最短路径条数 void DFS(int x, int y); int main() { scanf("%d%d", &n, &m); { for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { scanf("%d", &mazeDfs[i][j]); } } scanf("%d%d", &p, &q); //起点坐标 scanf("%d%d", &r, &s); //终点坐标 //输出所有的路径 pathNumber = 0; sx.push(p); sy.push(q); mazeDfs[p][q] = 0; DFS(p, q); if (pathNumber == 0) { printf("-1\n"); } } return 0; } void DFS(int x, int y) { if ((x==r) && (y==s)) //找到一条最短路径 { pathNumber++; //输出路径 int j = sx.size(); for (int i=1; i<=j; i++) { sxCopy.push(sx.top()); sx.pop(); syCopy.push(sy.top()); sy.pop(); } for (int i=1; i<j; i++) { printf("(%d,%d)->", sxCopy.top(), syCopy.top()); sx.push(sxCopy.top()); sxCopy.pop(); sy.push(syCopy.top()); syCopy.pop(); } printf("(%d,%d)", sxCopy.top(), syCopy.top()); sx.push(sxCopy.top()); sxCopy.pop(); sy.push(syCopy.top()); syCopy.pop(); printf("\n"); return ; } for (int i=0; i<4; i++) { int xx, yy; xx = x + coordinate[i][0]; yy = y + coordinate[i][1]; if ((xx>=1) && (xx<=n) && (yy>=1) && (yy<=m) && (mazeDfs[xx][yy]==1)) { sx.push(xx); sy.push(yy); mazeDfs[xx][yy] = 0; DFS(xx, yy); //回溯 sx.pop(); sy.pop(); mazeDfs[xx][yy] = 1; } } }
相关文章推荐
- Leetcode: Combinations
- emeditor的快捷键
- ssh 免密码登录
- 拦截器的写法及配置
- 对svm支持向量机的理解
- csu 10月 月赛 D 题 CX and girls
- 出现错误:TextView cannot be resolved to a type
- lmctfy:让我用集装箱为你的程式打包
- lmctfy:让我用集装箱为你的程式打包
- C++ protected的用法
- 快速排序
- 重载箭头操作符
- 五大常用算法之五:分支限界法
- 动态规划总专题
- 类与类之间的关系图(Class Diagram,UML图)
- linux同步-条件变量
- 五大常用算法之三:贪心算法
- [转]Ubuntu 12.04 使用Eclipse搭建C/C++编译环境 2012-06-13 22:07:18
- HDU 1538 A Puzzle for Pirates (博弈&海盗分赃问题)
- 如何实现内存隔离和内存共享