• HDU


    HDU - 3085 Nightmare Ⅱ 

    双向BFS,建立两个队列,让男孩女孩一起走

    鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖

      1 #include <cstdio>
      2 #include <queue>
      3 #include <algorithm>
      4 #include <cmath>
      5 #include <cctype>
      6 #include <cstring>
      7 using namespace std;
      8 
      9 #define res register int
     10 const int N=800+5;
     11 const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
     12 int n,m;
     13 char s[N][N];
     14 bool vis[N][N][2];
     15 struct node{
     16     int x,y;
     17     node() {};
     18     node(int x,int y) : x(x),y(y) {}
     19 };
     20 
     21 queue<node> q[2];
     22 node mm,gg,gho[2];
     23 
     24 inline int _dis(int a,int b,int x,int y)
     25 {
     26     return abs(a-x)+abs(b-y);
     27 }
     28 inline bool check(int x,int y) {
     29     if(x<1 || x>n || y<1 || y>m || s[x][y]=='X') return false;
     30     else return true;
     31 }
     32 inline bool check2(int x,int y,int stp)
     33 {
     34     for(res i=0 ; i<=1 ; i++)
     35         if(_dis(x,y,gho[i].x,gho[i].y)<=2*stp) return false;
     36     return true;
     37 }
     38 
     39 inline bool bfs(int typ,int stp)
     40 {
     41     int len=q[typ].size();
     42     while(len--)
     43     {
     44         node u=q[typ].front(); q[typ].pop();    
     45         if(!check(u.x,u.y) || !check2(u.x,u.y,stp)) continue;
     46         for(res i=0 ; i<4 ; i++)
     47         {
     48             int nx=u.x+dx[i],ny=u.y+dy[i];
     49             if(!check(nx,ny) || !check2(nx,ny,stp)) continue;
     50             if(vis[nx][ny][typ^1]) return true;
     51             if(vis[nx][ny][typ]) continue;
     52             vis[nx][ny][typ]=1;
     53             q[typ].push(node(nx,ny));
     54         }
     55     }
     56     return false;
     57     
     58 }
     59 
     60 inline void init()
     61 {
     62     while(q[0].size()) q[0].pop();
     63     while(q[1].size()) q[1].pop();    
     64     memset(vis,0,sizeof(vis));
     65 }
     66 
     67 inline int run()
     68 {
     69     init();
     70     vis[mm.x][mm.y][0]=1;
     71     vis[gg.x][gg.y][1]=1;
     72     q[0].push(node(mm.x,mm.y)); 
     73     q[1].push(node(gg.x,gg.y));
     74     int stp(0);
     75     while(q[0].size() || q[1].size())
     76     {
     77         stp++;
     78         for(res i=1 ; i<=3 ; i++)
     79             if(bfs(0,stp)) return stp;
     80         if(bfs(1,stp)) return stp;
     81     }
     82     return -1;
     83 }
     84 
     85 int main()
     86 {
     87     int T ; scanf("%d",&T);
     88     while(T--)
     89     {
     90         scanf("%d %d",&n,&m);
     91         for(res i=1 ; i<=n ; i++) scanf("%s",s[i]+1);
     92         int t(0);
     93         for(res i=1 ; i<=n ; i++)
     94             for(res j=1 ; j<=m ; j++)
     95                 if(s[i][j]=='G') {
     96                     gg.x=i,gg.y=j;
     97                 } 
     98                 else if(s[i][j]=='M') {
     99                     mm.x=i,mm.y=j;
    100                 }
    101                 else if(s[i][j]=='Z') {
    102                     gho[t].x=i,gho[t++].y=j;                
    103                 }
    104                 
    105         printf("%d
    ",run());
    106     }
    107     return 0;
    108 }
    my code
  • 相关阅读:
    可执行程序的装载
    stdafx.h的作用
    AI调色板
    3ds max输出图片
    3ds max移除几何体的线段
    3ds max删除了对象后,还是将原来所有对象输出的原因
    vs win32 & MFC 指针默认位置
    3ds max 分离对象
    PDF
    endnote设置文献第二行悬挂缩进办法
  • 原文地址:https://www.cnblogs.com/wmq12138/p/10391414.html
Copyright © 2020-2023  润新知