zoukankan      html  css  js  c++  java
  • hdu

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

    感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了。

    但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的?

    当然用优先队列+ a去搜索r是最稳妥的。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <iostream>
     5 using namespace std;
     6 
     7 struct maze
     8 {
     9     int x,y,t;
    10     bool operator < (const maze a) const
    11     {
    12         return t>a.t;
    13     }
    14 };
    15 int n,m,time;
    16 bool flag;
    17 char field[210][210];
    18 int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
    19 void bfs(maze s)
    20 {
    21     priority_queue<maze>que;
    22     que.push(s);
    23     while(!que.empty())
    24     {
    25         maze e=que.top();
    26         que.pop();
    27         for(int i=0;i<4;i++)
    28         {
    29             s.x=e.x+dir[i][0];
    30             s.y=e.y+dir[i][1];
    31             s.t=e.t+1;
    32             if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&field[s.x][s.y]!='#')
    33             {
    34                 if(field[s.x][s.y]=='r')
    35                 {
    36                     flag=1;time=s.t;return;
    37                 }
    38                 else if(field[s.x][s.y]=='x') s.t++;
    39 
    40                 //printf("%d %d %d
    ",s.x,s.y,s.t);
    41                 field[s.x][s.y]='#';
    42                 que.push(s);
    43             }
    44         }
    45     }
    46 }
    47 
    48 int main()
    49 {
    50     //freopen("a.txt","r",stdin);
    51     maze s;
    52     while(~scanf("%d%d",&n,&m))
    53     {
    54         getchar();
    55         for(int i=0;i<n;i++)
    56         {
    57             scanf("%s",field[i]);
    58             for(int j=0;j<m;j++)
    59             {
    60                 if(field[i][j]=='a')
    61                 {
    62                     s.x=i;
    63                     s.y=j;
    64                     s.t=0;
    65                 }
    66             }
    67         }
    68         //printf("%d %d
    ",s.x,s.y);
    69         flag=0;
    70         field[s.x][s.y]='#';
    71         bfs(s);
    72         if(flag) printf("%d
    ",time);
    73         else printf("Poor ANGEL has to stay in the prison all his life.
    ");
    74     }
    75 }
     
    http://acm.hdu.edu.cn/showproblem.php?pid=2425
    这题是给定了起始点和目标点,让你求起始点到目标点的最少花费。总共有3种点,每一种点的花费都不同。跟上面那体没有很大区别。
    这题简直悲剧,提交错了5,6次,就是一点小错误还害得我对拍了很多次。以后一定要细心。
    判断到达目标点的时候不要再循环里面判断,可能出错。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 using namespace std;
     5 struct maze
     6 {
     7     int x,y,cost;
     8     bool operator < (const maze a) const
     9     {
    10         return cost>a.cost;
    11     }
    12 };
    13 int r,c;
    14 int vp,vs,vt;
    15 int sr,sc,tr,tc;
    16 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    17 char field[100][100];
    18 int bfs()
    19 {
    20     priority_queue<maze>que;
    21     maze s;
    22     s.x=sr;s.y=sc;s.cost=0;
    23     field[s.x][s.y]='@';
    24     que.push(s);
    25     while(!que.empty())
    26     {
    27         maze e=que.top();
    28         que.pop();
    29        // printf("%d %d %d
    ",s.x,s.y,s.cost);
    30         if(e.x==tr&&e.y==tc) return e.cost;
    31         for(int i=0;i<4;i++)
    32         {
    33             s=e;
    34             s.x=e.x+dir[i][0];
    35             s.y=e.y+dir[i][1];
    36             if(s.x<0||s.x>=r||s.y<0||s.y>=c||field[s.x][s.y]=='@') continue;
    37             if(field[s.x][s.y]=='#') s.cost+=vp;
    38             else if(field[s.x][s.y]=='.') s.cost+=vs;
    39             else if(field[s.x][s.y]=='T') s.cost+=vt;
    40             field[s.x][s.y]='@';
    41             que.push(s);
    42         }
    43     }
    44     return -1;
    45 }
    46 int main()
    47 {
    48     //freopen("data.txt","r",stdin);
    49     //freopen("b.txt","w",stdout);
    50     int j=1;
    51     while(~scanf("%d%d",&r,&c))
    52     {
    53         //printf("%d %d
    ",r,c);
    54         scanf("%d%d%d",&vp,&vs,&vt);
    55         //printf("%d %d %d
    ",vr,vs,vt);
    56         getchar();
    57         for(int i=0;i<r;i++) scanf("%s",field[i]);
    58         scanf("%d%d%d%d",&sr,&sc,&tr,&tc);
    59         printf("Case %d: %d
    ",j++,bfs());
    60     }
    61     return 0;
    62 }
     
  • 相关阅读:
    BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防
    BZOJ1912或洛谷3629 [APIO2010]巡逻
    CH6202 黑暗城堡
    POJ2728 Desert King
    JoyOI1391 走廊泼水节
    洛谷1073 最优贸易
    POJ3662或洛谷1948 Telephone Lines
    BZOJ1106 [POI2007]立方体大作战tet
    ubuntu 16.04 安装genymotion
    ubuntu下搭建android开发环境核心篇安装AndroidStudio、sdk、jdk
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4523729.html
Copyright © 2011-2022 走看看