《挑战程序设计竞赛》P34迷宫的最短路径
2014-08-12 22:31
267 查看
/* 注意pair的使用和定义,注意4个方向向量的处理,注意标记的处理 BFS(宽度优先搜索)按照距开始状态由近及远的顺序进行搜索,因此可以很容易地用来求最短路径、 最少操作之类的答案 */
/*
(转)
pair的类型: pair 是 一种模版类型。每个pair 可以存储两个值。这两种值无限制。也可以将自己写的struct的对象放进去。。
pair<string,int> p; pair<int ,int > p; pair<double,int> p; 都可以。。。
<span style="font-family: Arial, Helvetica, sans-serif;">应用:如果一个函数有两个返回值 的话,如果是相同类型,就可以用数组返回,如果是不同类型,就可以自己写个struct ,但为了方便就可以使用 c++ 自带的pair ,返回一个pair,其中带有两个值。除了返回值的应用,在一个对象有多个属性的时候 ,一般自己写一个struct ,如果就是两个属性的话,就可以用pair 进行操作。。。</span>
应用pair 可以省的自己写一个struct 。。。如果有三个属性的话,其实也是可以用的pair 的 ,极端的写法 pair <int ,pair<int ,int > > 写法极端。(后边的两个 > > 要有空格,否则就会是 >> 位移运算符) makr_pair: pair<int ,int >p (5,6); pair<int ,int > p1= make_pair(5,6); pair<string,double> p2 ("aa",5.0); pair <string ,double> p3 = make_pair("aa",5.0); 有这两种写法来生成一个pair。 如何取得pair的值呢。。 每个pair 都有两个属性值 first 和second cout<<p1.first<<p1.second; 注意是属性值而不是方法。 template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair() : first(T1()), second(T2()) {} pair(const T1& x, const T2& y) : first(x), second(y) {} template <class U, class V> pair (const pair<U,V> &p) : first(p.first), second(p.second) { } } 由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明: typedef pair<string, string> author; author pro("May", "Lily"); author joye("James", "Joyce");
*/ #include <cstdio> #include <algorithm> #include <iostream> #include <string> #include <map> #include <vector> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstring> #include <utility> using namespace std; #define ll long long #define llu unsigned long long #define INF 100000000 //使用pair表示状态时,使用typedef会更加方便一些 const int maxn = 100+10; typedef pair<int, int> P; char maze[maxn][maxn];//表示迷宫的字符串数组 int n,m; int sx,sy;//起点坐标 int gx,gy;//重点坐标 int d[maxn][maxn]; //到各个位置的最短距离的数组 //四个方向移动的向量 int dx[4] = {1,0,-3 2 1 4 5 7 6 3 1 2 5 6 7 4 7 8 11 3 5 16 12 18 8 3 11 7 16 18 12 5 255 2551,0}, dy[4] = {0,1,0,-1}; //求从(sx,sy)到(gx,gy)的最短距离 //如果无法到达,则是INF int bfs() { queue<P > que; // 把所有的位置都初始化为INF for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) d[i][j] = INF; //将起点加入队列,并把这一地点的距离设置为0 que.push(P(sx,sy)); d[sx][sy] = 0; //不断循环直到队列的长度为0 while(que.size()) //while(!que.empty()) { //从队列的最前端取出元素 P p = que.front(); // cout << p.first << " " << p.second << endl; que.pop(); //如果取出的状态已经是终点,则结束搜索 if(p.first == gx && p.second == gy) break; //四个方向的循环 for(int i = 0; i < 4; ++i) { //移动之后的位置记为(nx,ny) int nx = p.first + dx[i],ny = p.second + dy[i]; //判断是否可以移动以及是否已经访问过(d[nx][ny] != INF即为已经访问过) if(0 <= nx && nx < n && 0 <= ny && ny < m && maze[nx][ny] != '#' && d[nx][ny] == INF) { //可以移动的话则加入队列,并且到该位置的距离确定为到p的距离+1 que.push(P(nx,ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } return d[gx][gy]; } void solve() { int res = bfs(); printf("%d\n",res); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif // ONLINE_JUDGE while(cin >> n >> m) { memset(maze,0,sizeof(maze)); for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) { cin >> maze[i][j]; if(maze[i][j] == 'S') {sx = i; sy = j;} if(maze[i][j] == 'G') {gx = i; gy = j;} } // cout << sx << " " << sy << endl; // for(int i = 0; i < n; ++i) // { // for(int j = 0; j < n; ++j) // cout << maze[i][j]; // cout << endl; // } solve(); } return 0; } /* Sample Input: 10 10 #S######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .####.###. ....#...G# Sample Output: 22 */
相关文章推荐
- 迷宫最短路径问题-BFS
- hdu4230 Robot Navigation 迷宫最短路径的条数 BFS 很霸气!!!!!
- 用栈寻找迷宫的最短路径
- BFS 迷宫的最短路径
- 广度优先搜索--迷宫最短路径--队列
- 5X5迷宫,求起点和终点的最短路径以及最短路径个数(百分数)
- 迷宫的最短路径(bfs)
- 围住神经猫,迷宫最短路径算法分析
- 迷宫的最短路径(深度bfs)
- 数据结构学习记录-迷宫设计(最短路径)
- 迷宫的最短路径
- cdoj1088-王之迷宫 (三维迷宫最短路径)【BFS】
- 迷宫最短路径dfs和bfs代码分析
- 广搜走迷宫最短路径
- 迷宫最短路径-货郎担问题的解决思路
- 迷宫的最短路径(局限版)
- 迷宫——BFS(最短路径和所有路径)
- 迷宫的最短路径(BFS的简单应用)
- 回溯法找迷宫最短路径
- 迷宫的最短路径