HDU 5040

来源:互联网 发布:交互式课件制作软件 编辑:程序博客网 时间:2024/05/22 15:49

 

本来是很好的一道题,题目描述不太清楚,导致比赛的时候死都过不了。

 

当前点(x, y)时间t  被hit
或者
当前下一点(nx, ny)时间t  被hit
+3

其他 + 1

int    n   ;const  int  maxn = 502 ;char   g[maxn][maxn] ;int   can(int x , int y){      return 1 <= x && x <= n && 1 <= y && y <= n ;}struct state{       int x , y , t  ;       state(){}       state(int x , int y , int t){            this->x = x ;            this->y = y ;            this->t = t ;       }       friend bool operator < (const state &a , const state &b){            return a.t > b.t  ;       }};char    rote(char  c , int t){        t %= 4 ;        if(c == 'N'){               if(t == 0) return 'N' ;               else if(t == 1) return 'E' ;               else if(t == 2) return 'S' ;               else return 'W' ;        }        if(c == 'E'){               if(t == 0) return 'E' ;               else if(t == 1) return 'S' ;               else if(t == 2) return 'W' ;               else return 'N' ;        }        if(c == 'S'){               if(t == 0) return 'S' ;               else if(t == 1) return 'W' ;               else if(t == 2) return 'N' ;               else return 'E' ;        }        if(c == 'W'){               if(t == 0) return 'W' ;               else if(t == 1) return 'N' ;               else if(t == 2) return 'E' ;               else return 'S' ;        }}#define  Nn 0#define  Ee 1#define  Ss 2#define  Ww 3int    d[4][2] = {{-1,0} , {0 , 1} , {1 ,0} ,{0, -1}} ;int    hit(state c){       if(g[c.x][c.y] == 'N' || g[c.x][c.y] == 'W'          || g[c.x][c.y] == 'S' || g[c.x][c.y] == 'E') return 1 ;       for(int k = 1 ; k <= 1 ; k++){           for(int i = 0 ; i < 4 ; i++){                int x = c.x + d[i][0]*k ;                int y = c.y + d[i][1]*k ;                if(! can(x , y)) continue ;                char ch = rote( g[x][y] , c.t ) ;                if(i == Nn && ch == 'S') return 1 ;                if(i == Ee && ch == 'W') return 1 ;                if(i == Ss && ch == 'N') return 1 ;                if(i == Ww && ch == 'E') return 1 ;           }       }       return 0 ;}int    dist[maxn][maxn][4] ;const  int  inf = 1000000000 ;std::priority_queue< state > q ;int    spfa(state s){       while( ! q.empty() ) q.pop()  ;       for(int i = 1 ; i <= n ; i++){           for(int j = 1 ; j <= n ; j++){               for(int k = 0 ; k < 4 ; k++) dist[i][j][k] = inf ;           }       }       state  now , next , nx ;       q.push(s) ;       dist[s.x][s.y][0] = 0 ;       while(! q.empty()){            now = q.top() ; q.pop() ;            if(g[now.x][now.y] == 'T')  return now.t ;            next.x = now.x ;            next.y = now.y ;            next.t = now.t + 1 ;            if(dist[next.x][next.y][next.t % 4] > next.t){                    dist[next.x][next.y][next.t % 4] = next.t ;                    q.push(next) ;            }            for(int i = 0 ; i < 4 ; i++){                  next.x = nx.x = now.x + d[i][0] ;                  next.y = nx.y = now.y + d[i][1] ;                  next.t = now.t ;                  if(! can(next.x , next.y)) continue ;                  if( g[next.x][next.y] == '#') continue ;                  if(hit(now) || hit(next)) next.t += 3 ;                  else  next.t += 1 ;                  if(dist[next.x][next.y][next.t % 4] > next.t){                            dist[next.x][next.y][next.t % 4] = next.t ;                            q.push(next) ;                  }            }       }       return -1  ;}int   main(){      int  i  , j , t  , T = 1  ;      state s ;      cin>>t ;      while(t--){           scanf("%d" , &n) ;           for(i = 1 ; i <= n ; i++) scanf("%s" , g[i] + 1) ;           for(i = 1 ; i <= n ; i++){               for(j = 1 ; j <= n ; j++){                    if(g[i][j] == 'M'){                         s = state(i , j , 0) ;                    }               }           }           i = spfa(s) ;           printf("Case #%d: %d\n" , T++ , i) ;      }      return 0 ;}


 

0 0
原创粉丝点击