hdu 1180 诡异的楼梯 BFS+优先队列

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1180

昨天晚上开始做这道题。。。以前打算做做这题但是不知道这优先队列应该怎么用。。。昨晚这道题才有点深入的了解些。。。

这道题因为楼梯会变换方向会变成不同的路,所以一不能标记。。。而且要注意边界

这个是我纠结了4歌小时候都最后经大神鉴定改错后的代码。。。

-

#include <string.h>
#include <stdio.h>
char map[30][30];

int dis[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
    int x,y,num;
}q[100005];
int f,re;
void sort(int n)
{
    int i,j;
    struct node t;
    for(i = re;i > f;i--)
    {
        if(q[i].num < q[i-1].num)
        {
            t = q[i];
            q[i] =q[i-1];
            q[i-1] = t;
        }
        else
        break;
    }
}
int main()
{
    int r,c,i,j;
    char ch;
    while(~scanf("%d%d",&r,&c))
    {
        int x,y;

        for(i = 0;i < r;i++)
        {
            scanf("%s",map[i]);
            for(j = 0;j < c;j++)
            {
                if(map[i][j] == 'S')
                {
                    x = i,y = j;
                }
                
            }
        }
        if(r&&c){
        f = re = 0;
        int num;
        num = 0;
        int vis[30][30] = {0};
        q[re].num = 0;
        q[re].x = x;
        q[re].y = y;
        vis[x][y] = 1;
        re++;
        while(f < re)
        {
            struct node t;
            t = q[f++];
            int leap = 0;
            for(i = 0;i < 4;i++)
            {
                num = t.num;
                x = t.x + dis[i][0];//下一个位置
                y = t.y + dis[i][1];

                if(x >= 0 && x < r && y < c  && y >= 0 && !vis[x][y])//判断是否越界
                {
                    ch = map[x][y];
                    if(ch == '-' || ch == '|')//如果是楼梯应该怎样改变它
                    {
                        if(num%2)
                        {
                            if(ch == '-')
                            ch = '|';
                            else
                            ch = '-';
                        }

                        x += dis[i][0];
                        y += dis[i][1];

                        if(x >= 0 && x < r && y < c  && y >= 0 && !vis[x][y])//改变后的位置是否越界
                        {

                            if(ch == '-')
                            {
                                if(i%2 == 0)//属于上下移动的时候
                                {
                                    num++;
                                }
                                ch = map[x][y];
                            }
                            else
                            {
                                if(i%2)
                                {
                                    num++;
                                }
                                ch = map[x][y];
                            }

                        }

                    }
                    if(ch == '.' || ch == 'S' || ch == 'T' && !vis[x][y] &&x >= 0 && x < r && y < c  && y >= 0)
                    {
                        if(ch == 'T' || ch == '.' )
                        {
                        q[re].x = x;
                        q[re].y = y;
                        q[re].num = num+1;
                        vis[x][y] = 1;
                        }
                        if(ch == 'T')
                        {
                            leap = 1;
                            break;
                        }

                        sort(re);
                        re++;
                    }
                }

            }
            if(leap)
                break;
        }
        printf("%d\n",q[re].num);}
    }
    return 0;
}

这个是改写后的,简洁点。。。比较容易看懂,思路清晰些~

View Code
  1 #include <string.h>
  2 #include <stdio.h>
  3 char map[30][30];
  4 
  5 int dis[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
  6 struct node
  7 {
  8     int x,y,num;
  9 }q[100005];
 10 int f,re,r,c;
 11 void sort()//优先队列的排序,由于楼梯转动会造成转动费时,所以之后加入队列的值不一定就一定比队里的全部都要大。
 12 {
 13     int i,j;
 14     struct node t;
 15     for(i = re;i > f;i--)
 16     {
 17         if(q[i].num < q[i-1].num)
 18         {
 19             t = q[i];
 20             q[i] =q[i-1];
 21             q[i-1] = t;
 22         }
 23         else
 24         break;
 25     }
 26 }
 27 int BFS()
 28 {
 29     int vis[30][30] = {0};
 30     int i,j,x,y;
 31     char ch;
 32     f = re = 0;
 33     x = q[f].x;
 34     y = q[f].y;
 35     vis[x][y] = 1;
 36     re++;
 37     while(f != re)
 38     {
 39 
 40         struct node t;
 41         t = q[f++];
 42         int num;
 43         num = t.num;
 44         for(i = 0;i < 4;i++)
 45         {
 46             x = t.x+dis[i][0];
 47             y = t.y+dis[i][1];
 48             num = t.num;
 49             if(x >= 0&& x < r && y >= 0&& y < c && !vis[x][y])
 50             {
 51                 ch = map[x][y];
 52                 if(ch == '|' || ch == '-')//对楼梯的处理
 53                 {
 54                     if(num%2)//时间为单数的时候一定会发生改变
 55                     {
 56                         if(ch == '-')
 57                             ch = '|';
 58                         else
 59                         ch = '-';
 60                     }
 61 
 62                     x += dis[i][0];
 63                     y += dis[i][1];//遇到楼梯直接把楼梯跨过去。。不需要考虑楼梯上的时间
 64 
 65                 }
 66                 if(x >= 0&& x < r && y >= 0&& y < c && !vis[x][y])//楼梯扩过之后是否越界
 67                 {
 68                     if(i%2)
 69                     {
 70                         if(ch == '|')//看现在是否能够跨过楼梯,不能时间加1
 71                         num++;
 72                     }
 73                     else
 74                     {
 75                         if(ch == '-')
 76                         num++;
 77                     }
 78                     ch = map[x][y];
 79                 }
 80                 else
 81                 continue;
 82                 if((ch == '.' || ch == 'T'))
 83                 {
 84                     q[re].num = num+1;
 85                     q[re].x = x;
 86                     q[re].y = y;
 87                     if(ch == 'T')
 88                     {
 89                         return q[re].num;
 90                     }
 91                     sort();
 92                     vis[x][y] = 1;
 93                     re++;
 94                 }
 95 
 96             }
 97         }
 98     }
 99 }
100 int main()
101 {
102     int i,j,x,y;
103     while(~scanf("%d %d",&r,&c))
104     {
105         for(i = 0;i < r;i++)
106         {
107             scanf("%s",map[i]);
108             for(j = 0;j < c;j++)
109             if(map[i][j] == 'S')
110             x = i,y = j;
111         }
112 
113         q[0].x = x;
114         q[0].y = y;
115         q[0].num = 0;
116         int ans;
117         ans = BFS();
118         printf("%d\n",ans);
119     }
120 
121     return 0;
122 }

 


  1 #include <string.h>
  2 #include <stdio.h>
  3 char map[30][30];
  4 
  5 int dis[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
  6 struct node
  7 {
  8     int x,y,num;
  9 }q[100005];
 10 int f,re,r,c;
 11 void sort()
 12 {
 13     int i,j;
 14     struct node t;
 15     for(i = re;i > f;i--)
 16     {
 17         if(q[i].num < q[i-1].num)
 18         {
 19             t = q[i];
 20             q[i] =q[i-1];
 21             q[i-1] = t;
 22         }
 23         else
 24         break;
 25     }
 26 }
 27 int BFS()
 28 {
 29     int vis[30][30] = {0};
 30     int i,j,x,y;
 31     char ch;
 32     f = re = 0;
 33     x = q[f].x;
 34     y = q[f].y;
 35     vis[x][y] = 1;
 36     re++;
 37     while(f != re)
 38     {
 39 
 40         struct node t;
 41         t = q[f++];
 42         int num;
 43         num = t.num;
 44         for(i = 0;i < 4;i++)
 45         {
 46             x = t.x+dis[i][0];
 47             y = t.y+dis[i][1];
 48             num = t.num;
 49             if(x >= 0&& x < r && y >= 0&& y < c && !vis[x][y])
 50             {
 51                 ch = map[x][y];
 52                 if(ch == '|' || ch == '-')
 53                 {
 54                     if(num%2)
 55                     {
 56                         if(ch == '-')
 57                             ch = '|';
 58                         else
 59                         ch = '-';
 60                     }
 61 
 62                     x += dis[i][0];
 63                     y += dis[i][1];
 64 
 65 
 66                 }
 67                 if(x >= 0&& x < r && y >= 0&& y < c && !vis[x][y])
 68                 {
 69                     if(i%2)
 70                     {
 71                         if(ch == '|')
 72                         num++;
 73                     }
 74                     else
 75                     {
 76                         if(ch == '-')
 77                         num++;
 78                     }
 79                     ch = map[x][y];
 80                 }
 81                 else
 82                 continue;
 83                 if((ch == '.' || ch == 'T'))
 84                 {
 85                     q[re].num = num+1;
 86                     q[re].x = x;
 87                     q[re].y = y;
 88                     if(ch == 'T')
 89                     {
 90                         return q[re].num;
 91                     }
 92                     sort();
 93                     vis[x][y] = 1;
 94                     re++;
 95 
 96                 }
 97 
 98             }
 99         }
100     }
101 }
102 int main()
103 {
104     int i,j,x,y;
105     while(~scanf("%d %d",&r,&c))
106     {
107         for(i = 0;i < r;i++)
108         {
109             scanf("%s",map[i]);
110             for(j = 0;j < c;j++)
111             if(map[i][j] == 'S')
112             x = i,y = j;
113         }
114 
115         q[0].x = x;
116         q[0].y = y;
117         q[0].num = 0;
118         int ans;
119         ans = BFS();
120         printf("%d\n",ans);
121     }
122 
123     return 0;
124 }

 

posted @ 2012-08-21 20:02  某某。  阅读(200)  评论(0编辑  收藏  举报