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

迷宫最短路径 问题

时间:2017-11-07 12:23:44      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:nod   最短路   size   end   ext   clu   front   str   this   

/*
迷宫最短路径 问题 
*/
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct Node{
  int x, y;
  Node(int x,int y) {
    this->x = x;
    this->y = y;
  }
};
const int N = 15;
int map[9][9]= {1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,0,1,
1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1};
int d[N][N], book[N][N];
int next[][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int endx, endy;
int n, m;
void bfs(int x,int y) ;
int main() {
    int beginx, beginy;
    while (cin >> beginx >> beginy) {
      cin>> endx >> endy;
      memset(book,0,sizeof(book));
      memset(d,0,sizeof(d)); 
    n = 8, m = 8;
      bfs(beginx,beginy);
      cout << d[endx][endy] << endl;
  }
}
void bfs(int x,int y) {
  queue<Node> p;
  Node q(x,y);
  p.push(q);
  d[x][y] = 0;
  book[x][y] = 1;
  while (!p.empty()) {
    q = p.front();
    p.pop();
    if (q.x == endx && q.y == endy) return ;
    int v, u;
    for (int i = 0; i < 4; i++) {
      v = q.x + next[i][0];
      u = q.y + next[i][1];
      if (v >= 0 && v <= n && u >= 0 && u <= m && book[v][u] == 0 && map[v][u] == 0) {
        book[v][u] = 1;
        d[v][u] = d[q.x][q.y] + 1;
//        q = Node(v,u);
        p.push(Node(v,u));
      }
    }
  }
  return ;
}

 

迷宫最短路径 问题

标签:nod   最短路   size   end   ext   clu   front   str   this   

原文地址:http://www.cnblogs.com/dream-it-possible/p/7798271.html

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