zoukankan      html  css  js  c++  java
  • HDU 2102 A计划 (BFS或DFS)

    题意:中文题。

    析:是一个简单的搜索,BFS 和 DFS都可行, 主要是这个题有一个坑点,那就是如果有一层是#,另一个层是#或者*,都是过不去的,就可以直接跳过,

    剩下的就是一个简单的搜索,只不过是两层而已,再加一维表示是哪一层就好,可能一个就是在#必须传送,不能再上下左右走,这个题目已经说的很清楚了,不算坑。

    对于BFS,如果找到P就可以结束,如果超过了T,那么就是找不到了,返回找不到。

    对于DFS,同样是找到就结束。

    总体来说不难。。。。

    代码如下:

    BFS:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 10 + 10;
    const int mod = 1000000007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    char s[2][maxn][maxn];
    int vis[2][maxn][maxn];
    int t;
    struct Node{
      int pos, x, y;
      Node() { }
      Node(int p, int xx, int yy) : pos(p), x(xx), y(yy) { }
      bool operator == (const Node &p) const{
        return pos == p.pos && x == p.x && y == p.y;
      }
    };
    
    bool bfs(){
      memset(vis, -1, sizeof vis);
      Node goal;
      for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
          if(s[0][i][j] == 'P')  goal = Node(0, i, j);
          else if(s[1][i][j] == 'P')  goal = Node(1, i, j);
      queue<Node> q;
      vis[0][0][0] = 0;
      q.push(Node(0, 0, 0));
    
      while(!q.empty()){
        Node u = q.front();  q.pop();
        if(vis[u.pos][u.x][u.y] > t)  return false;
        if(u == goal)  return true;
        for(int i = 0; i < 4; ++i){
          int x = u.x + dr[i];
          int y = u.y + dc[i];
          if(!is_in(x, y) || vis[u.pos][x][y] != -1 || s[u.pos][x][y] == '*')  continue;
          vis[u.pos][x][y] = vis[u.pos][u.x][u.y] + 1;
          if(s[u.pos][x][y] == '#'){
            vis[u.pos^1][x][y] = vis[u.pos][x][y];
            if(s[u.pos^1][x][y] == '#' || s[u.pos^1][x][y] == '*')  continue;
            q.push(Node(u.pos^1, x, y));
            continue;
          }
          q.push(Node(u.pos, x, y));
        }
      }
      return false;
    }
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d %d %d", &n, &m, &t);
        for(int i = 0; i < n; ++i)  scanf("%s", s[0]+i);
        for(int i = 0; i < n; ++i)  scanf("%s", s[1]+i);
        bool ans = bfs();
        printf("%s
    ", ans ? "YES" : "NO");
      }
      return 0;
    }
    

     DFS:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 10 + 10;
    const int mod = 1000000007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    char s[2][maxn][maxn];
    bool vis[2][maxn][maxn];
    int t;
    
    bool dfs(int pos, int r, int c, int d){
      if(d > t)  return false;
      if(s[pos][r][c] == 'P')  return true;
      for(int i = 0; i < 4; ++i){
        int x = r + dr[i];
        int y = c + dc[i];
        if(!is_in(x, y) || vis[pos][x][y] || s[pos][x][y] == '*')  continue;
        vis[pos][x][y] = 1;
        if(s[pos][x][y] == '#'){
          vis[pos^1][x][y] = 1;
          if(dfs(pos^1, x, y, d+1))  return true;
          vis[pos^1][x][y] = 0;
        }
        else if(dfs(pos, x, y, d+1))  return true;
        vis[pos][x][y] = 0;
      }
      return false;
    }
    
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d %d %d", &n, &m, &t);
        for(int i = 0; i < n; ++i)  scanf("%s", s[0]+i);
        for(int i = 0; i < n; ++i)  scanf("%s", s[1]+i);
        for(int i = 0; i < n; ++i)
          for(int j = 0; j < m; ++j)
            if(s[0][i][j] == '#' && (s[1][i][j] == '*' || s[1][i][j] == '#'))  s[0][i][j] = s[1][i][j] = '*';
            else if(s[0][i][j] == '*' && s[1][i][j] == '#')  s[1][i][j] = '*';
        memset(vis, 0, sizeof vis);
        vis[0][0][0] = 1;
        bool ans = dfs(0, 0, 0, 0);
        printf("%s
    ", ans ? "YES" : "NO");
      }
      return 0;
    }
    

      

  • 相关阅读:
    IPFS学习-DNS链接
    IPFS学习-内容标识符(CIDs)
    IPFS学习-分布式哈希表DHT
    CURL命令学习一
    Hyperledger Fabric 动态增加组织到网络中
    Fabric-Ca使用
    Hyperledger Fabric手动生成CA证书搭建Fabric网络
    Hyperledger Fabric私有数据
    Idea 第一个Struts2程序
    jQuery总结
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7152646.html
Copyright © 2011-2022 走看看