hdu1180 诡异的楼梯

来源:互联网 发布:python ip欺骗 编辑:程序博客网 时间:2024/05/19 01:07

这道题目我第二次写,根据当前步数判断楼梯的朝向,但是一直没有AC,后来想到广搜期间的点优先级不同,因为等待楼梯转向和直接过楼梯是同时入队的,因此普通队列不能区分则会导致错误,故要用到优先队列(之前AC的代码没有用优先队列,不知道是怎么办到的)

附代码如下:

#include <iostream>

#include <cstring>

#include <cstdio>

#include <queue>

using namespace std;

const int Go[4][2] = {0,-1,0,1,-1,0,1,0};//右上下左  

 

struct Node

{

       int x, y;

       int step;

       bool operator <(const Node t)const

    {

        return step > t.step;

    } 

}start;

 

int n, m;

 

char map[21][21];

 

bool visited[21][21];

 

int ex, ey;

 

bool check(int x, int y)

{

     return (x>=0&&x<n&&y>=0&&y<m);     

}

 

bool judge(int step, int xx, int yy, int i)

{

     if (step%2){

        if (map[xx][yy] == '|'){

           if (i == 2||i==3){

              return true;      

        }                        

     }

     if (map[xx][yy] == '-'){

        if (i == 0||i==1){

           return true;      

        }                        

     }

     }

     else {

          if (map[xx][yy] == '-'){

             if (i == 2||i== 3){

                return true;      

             }                        

          }

          if (map[xx][yy] == '|'){

             if (i == 0||i==1){

                return true;      

             }                        

          }

     }

     return false;                 

 

void Bfs()

{

     priority_queue  <Node>Que;

     memset(visited, 0, sizeof(visited));

     Que.push(start);

     visited[start.x][start.y] = 1;

     while (!Que.empty()){

           Node pre = Que.top();

           Que.pop();

           //printf("%d %d/n", pre.x, pre.y); 

           if (map[pre.x][pre.y] == 'T'){

              printf("%d/n", pre.step);

              return ;

           }

           for (int i = 0; i < 4; i ++){

               Node next;

               next. x = pre.x + Go[i][0];

               next. y = pre.y + Go[i][1];

               next. step = pre.step; 

               if (check(next.x, next.y)&&map[next.x][next.y] != '*'&&!visited[next.x][next.y]){

                  if (map[next.x][next.y] == '.' || map[next.x][next.y] == 'T'){

                     next.step ++;

                     visited[next.x][next.y] = 1;

                     Que.push(next);                        

                  }

                  else {

                       if (judge(next.step, next.x, next.y, i)){

                          next.step += 2;                     

                       }     

                       else next.step ++;

                       next.x += Go[i][0];

                       next.y += Go[i][1];

                       if (check(next.x, next.y)&&map[next.x][next.y] != '*'&&!visited[next.x][next.y]){

                          visited[next.x][next.y] = 1;

                          Que.push(next);                 

                       }

                  }                  

               }    

           }      

     }

}

 

int main()

{

 

    while (scanf("%d %d", &n, &m) != EOF ){

          for (int i = 0; i < n; i ++){

              scanf("%s", map[i]);

              for (int j = 0; j < m; j ++){

                  if (map[i][j] == 'S'){

                     start.x = i;

                     start.y = j;

                     start.step = 0;       

                  }               

              }    

          } 

          Bfs();      

    }

    return 0;    

}

原创粉丝点击