zoukankan      html  css  js  c++  java
  • 题目盲人摸象【dfs,bfs, 顺序遍历】

    FC87TSQP(~L]_R`}Z3H4IFI


    此题就是方向遍历,始终是想遍历左手所指得位置,或右手所指得位置。

    也就是一个遍历数组得顺序问题

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstring>
      4 #include <queue>
      5 using namespace std;
      6 
      7 constexpr size_t maxn = 110;
      8 
      9 int dx[] = {0, -1, 0, 1};
     10 int dy[] = {-1, 0, 1, 0};
     11 
     12 int dl[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
     13 int dr[][2] = {{0, 1}, {-1, 0 }, {0, -1}, {1, 0}};
     14 
     15 int sx, sy, ex, ey, n, m;
     16 
     17 char G[maxn][maxn];
     18 struct Pos{
     19     int x, y, s;
     20 };
     21 
     22 int dfs(int x, int y, int d, int step, int dir[][2]){
     23     for(int i = 0; i < 4; ++ i){
     24         int j = ((d - 1 + 4)% 4 + i) % 4;
     25         int nx = x + dir[j][0];
     26         int ny = y + dir[j][1];
     27 
     28         if(nx == ex && ny == ey)return step+1;
     29         if(nx < 0 || ny < 0 || nx > n || ny > m)continue;
     30         if(G[nx][ny] == '#') continue;
     31 
     32         return dfs(nx, ny, j, step + 1, dir);
     33     }
     34 }
     35 
     36 int BFS(int sx, int sy){
     37 	bool vis[maxn][maxn];
     38     memset(vis, false, sizeof(vis));
     39 
     40     queue<Pos> Q;
     41     Q.push({sx, sy, 1});
     42     vis[sx][sy] = true;
     43 
     44     while(!Q.empty()){
     45         Pos p = Q.front(); Q.pop();
     46         if(p.x == ex && p.y == ey) return p.s;
     47         Pos np;
     48         for(int i = 0; i < 4; ++ i){
     49             np.x = p.x + dx[i];
     50             np.y = p.y + dy[i];
     51             np.s = p.s + 1;
     52             if(np.x < 0 || np.x > n || np.y > m || np.y < 0)continue;
     53             if(vis[np.x][np.y])continue;
     54             if(G[np.x][np.y] != '#'){
     55                 vis[np.x][np.y] = true;
     56                 Q.push(np);
     57             }
     58         }
     59     }
     60 }
     61 
     62 
     63 int main(){
     64     int d1, d2;
     65     cin >> m >> n;
     66     for (int i = 0; i < n; ++ i){
     67         scanf("%s",  G[i]);
     68         for(int j = 0; j < m; ++ j){
     69             if(G[i][j] == 'S')sx = i, sy = j;
     70             else if(G[i][j] == 'E') ex = i, ey = j;
     71         }
     72     }
     73     if(sx == 0)d1 = 3, d2 = 3;
     74     else if(sx == n-1) d1 = 1, d2 = 1;
     75     else if(sy == 0)d1 = 2, d2 = 0;
     76     else if(sy == m-1)d1 = 0, d2 = 2;
     77     cout << dfs(sx, sy, d1, 1, dl) << endl;
     78     cout << dfs(sx, sy, d2, 1, dr) << endl;
     79     cout << BFS(sx, sy) << endl;
     80     return 0;
     81 
     82 }
     83 
    追求吾之所爱
  • 相关阅读:
    Play Framework框架安装指南 程序员
    Struts2.0标签学习 程序员
    一个给邮箱发送消息简单的例子 程序员
    CAGradientLayer 颜色渐变
    ObjectiveC 枚举类型和字符串互转方案
    添加本地通知(UILocalNotification)以及添加系统组件滚动视图(UIScrollView)!
    setNeedsDisplay setNeedDisplayInRect
    CAKeyframeAnimation 运行路径 速度控制
    How to pause or end a UIView animation via the CALayer
    NSTimer你真的会用了吗
  • 原文地址:https://www.cnblogs.com/rstz/p/12401508.html
Copyright © 2011-2022 走看看