zoukankan      html  css  js  c++  java
  • hdu 1023 Train Problem II 双向广搜

    http://acm.hdu.edu.cn/showproblem.php?pid=1023

      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<queue>
      6 #include<math.h>
      7 #include<algorithm>
      8 using namespace std;
      9 
     10 char s[802][802];
     11 struct node
     12 {
     13     int x,y;
     14 } M,G,Z[3];
     15 
     16 bool vis[2][802][802],flag;
     17 int dis[4][2]= {{1,0},{-1,0},{0,1},{0,-1}},n,mm;
     18 queue<node> q[2];
     19 
     20 
     21 
     22 int ok(int xx,int yy,int step)
     23 {
     24     int a,b;
     25     a=(abs(xx-Z[0].x)+abs(yy-Z[0].y)+1)/2;
     26     b=(abs(xx-Z[1].x)+abs(yy-Z[1].y)+1)/2;
     27     a=min(a,b);//  ghost 在第几秒走到这里
     28     if(a>step)
     29         return 1;
     30     return 0;
     31 }
     32 
     33 void bfs(int x,int step)
     34 {
     35     if(flag==true)
     36         return ;
     37     int size1=q[x].size();
     38     while(size1--)
     39     {
     40         node temp,temp1; temp=q[x].front(); q[x].pop();
     41         if(!ok(temp.x,temp.y,step))
     42             continue;
     43         for(int i=0;i<4;i++)
     44         {
     45             int xx,yy;
     46             xx=temp.x+dis[i][0];  yy=temp.y+dis[i][1];
     47             if(xx>=0&&xx<n&&yy>=0&&yy<mm&&vis[x][xx][yy]==false&&s[xx][yy]!='X'&&ok(xx,yy,step))
     48             {
     49                 vis[x][xx][yy]=true;
     50                 if(vis[x^1][xx][yy])
     51                 {
     52                     flag=1;
     53                     return ;
     54                 }
     55                 temp1.x=xx;
     56                 temp1.y=yy;
     57                 q[x].push(temp1);
     58             }
     59         }
     60     }
     61 }
     62 
     63 void tbfs()
     64 {
     65     node temp;
     66     temp.x=M.x;    temp.y=M.y;
     67     q[0].push(temp);
     68     vis[0][temp.x][temp.y]=true;/////
     69 
     70     temp.x=G.x;    temp.y=G.y;
     71     q[1].push(temp);
     72     vis[1][temp.x][temp.y]=true;////
     73     int ans=0;
     74     flag=false;
     75     while(!q[0].empty()||!q[1].empty())
     76     {
     77         ans++;
     78         bfs(0,ans);
     79         bfs(0,ans);
     80         bfs(0,ans);
     81         bfs(1,ans);
     82         if(flag)
     83         {
     84             printf("%d
    ",ans);
     85             return ;
     86         }
     87     }
     88     printf("-1
    ");
     89 }
     90 
     91 void solve()
     92 {
     93     while(!q[0].empty())
     94         q[0].pop();
     95     while(!q[1].empty())
     96         q[1].pop();
     97     memset(vis,false,sizeof(vis));
     98     tbfs();
     99 }
    100 
    101 int main()
    102 {
    103     int t;
    104     scanf("%d",&t);
    105     while(t--)
    106     {
    107         scanf("%d%d",&n,&mm);
    108         int num=0;
    109         for(int i=0; i<n; i++)
    110         {
    111             scanf("%s",s[i]);
    112             for(int j=0; j<mm; j++)
    113             {
    114                 if(s[i][j]=='M')
    115                 {
    116                     M.x=i;
    117                     M.y=j;
    118                 }
    119                 if(s[i][j]=='G')
    120                 {
    121                     G.x=i;
    122                     G.y=j;
    123                 }
    124                 if(s[i][j]=='Z')
    125                 {
    126                     Z[num].x=i;
    127                     Z[num++].y=j;
    128                 }
    129             }
    130         }
    131         solve();
    132 
    133     }
    134     return 0;
    135 }
  • 相关阅读:
    Php开发学习---言简意赅,内含视频教程
    决策树剪枝的三种方法
    梯度弥散与梯度爆炸
    算法岗面试题积累一
    《转》从系统和代码实现角度解析TensorFlow的内部实现原理 | 深度
    算法复习-全排列的非递归和递归实现(含重复元素)
    xgboost使用细节
    pandas Series和dataframe
    python 字符词串和字符串的转换
    (转载)自然语言处理中的Attention Model:是什么及为什么
  • 原文地址:https://www.cnblogs.com/assult/p/3711571.html
Copyright © 2011-2022 走看看