kuangbin专题 专题一 简单搜索 迷宫问题 POJ - 3984

 

题目链接:https://vjudge.net/problem/POJ-3984

这个题目,emm,上代码,看的估计应该是刚开始接触搜索的,我带点注释,你能慢慢理解。


  1 #include <iostream>
  2 #include <cstring>
  3 #include<vector>
  4 #include<string>
  5 #include <cmath>
  6 #include <map>
  7 #include <queue>
  8 #include <algorithm>
  9 using namespace std;
 10 
 11 #define inf (1LL << 31) - 1
 12 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
 13 #define rep__(i,j,k) for(int i = (j); i < (k); i++)
 14 #define per(i,j,k) for(int i = (j); i >= (k); i--)
 15 #define per__(i,j,k) for(int i = (j); i > (k); i--)
 16 
 17 const int N = 10;
 18 int mv_x[] = { 0, 0, -1, 1 };   //(1)
 19 int mv_y[] = { 1, -1, 0, 0 };   //(2)       (1) + (2) 可以表示人的上下左右移动
 20 char mp[N][N];  //地图
 21 bool vis[N][N];  //有没有访问过了
 22 
 23 struct node{
 24     int x, y;
 25     vector<string> vec;
 26     node(){}
 27     node(int a, int b){
 28         x = a;
 29         y = b;
 30     }
 31 };
 32 
 33 inline void input(){
 34 
 35     rep__(i, 0, 5) rep__(j, 0, 5) cin >> mp[i][j];
 36 }
 37 
 38 //检查有无越界的函数
 39 inline bool check(int x, int y){
 40     return x >= 0 && x <= 4 && y >= 0 && y <= 4;
 41 }
 42 
 43 void fun(string& p){
 44     cout << p << endl;
 45 }
 46 
 47 void work(){
 48     
 49     //开始的处理,起点标记
 50     vis[0][0] = true;
 51     node T(0, 0);
 52     T.vec.push_back("(0, 0)");
 53 
 54     queue<node> que;
 55     que.push(T);
 56 
 57     while (!que.empty()){
 58 
 59         node tmp = que.front();
 60         que.pop();
 61 
 62         rep__(p, 0, 4){
 63 
 64             //人的移动
 65             int dx = tmp.x + mv_x[p];
 66             int dy = tmp.y + mv_y[p];
 67             
 68             //没越界  是可以走的   没访问过
 69             if (check(dx, dy) && mp[dx][dy] == '0' && !vis[dx][dy]){
 70                 vis[dx][dy] = true; //标记
 71 
 72                 node in = tmp;
 73                 in.x = dx;
 74                 in.y = dy;
 75                 string t = "(x, x)";
 76                 t[1] = '0' + dx;
 77                 t[4] = '0' + dy;
 78                 in.vec.push_back(t); //把新的点压入
 79 
 80                 if (dx == 4 && dy == 4){ //遇到了出口,输出路线
 81                     
 82                     for_each(in.vec.begin(), in.vec.end(), fun);
 83 
 84                     return;
 85                 }
 86                 que.push(in); //把新的状态压入队列
 87             }
 88         }
 89     }
 90 }
 91 
 92 int main(){
 93 
 94     ios::sync_with_stdio(false);
 95     cin.tie(0);
 96 
 97     input();
 98     work();
 99 
100     return 0;
101 }

猜你喜欢

转载自www.cnblogs.com/SSummerZzz/p/11164245.html