2008杭州电子网络预赛 1008 Hiking Trip

来源:互联网 发布:永磁电机设计软件 编辑:程序博客网 时间:2024/04/29 20:20

        前几天18号下午的杭电的ACM网络预赛,看了1008题比较顺眼,呵呵.想了下用A*可以做,恰好前段时间老师讲过.实现了下就过了.为了做题写的有些地方现在看来不完善.打算这几天弄下.

  1. //附带道路成本的A*
  2. #include <iostream>
  3. #include <list>
  4. #include <math.h>
  5. #include <algorithm>
  6. using namespace std;
  7. const int MAX=0xfffffff;
  8. char rmap[21][21];
  9. int vp,vs,vt;
  10. int R,C,sr,sc,tr,tc;
  11. int Count,res;
  12. struct Node 
  13. {
  14.     int f,g,h;
  15.     int r,c;
  16.     Node *parent;
  17. };
  18. list<Node> openList;
  19. list<Node> closeList;
  20. Node child;
  21. bool dayu(Node & l,Node & r)
  22. {
  23.     return l.f>r.f;
  24. }
  25. bool check(Node value)
  26. {
  27.     if (value.r==child.r&&value.c==child.c)
  28.     {
  29.         return true;
  30.     }
  31.     return false;
  32. }
  33. bool Reach(int x,int y)
  34. {
  35.     if (x<0||y<0||x==R||y==C||rmap[x][y]=='@')
  36.         return false;
  37.     return true;
  38. }
  39. int GetCost(int x,int y)
  40. {
  41.     int r;
  42.     if (rmap[x][y]=='T')
  43.     {
  44.         r=vt;
  45.     }
  46.     if (rmap[x][y]=='.')
  47.     {
  48.         r=vs;
  49.     }
  50.     if (rmap[x][y]=='#')
  51.     {
  52.         r=vp;
  53.     }
  54.     return r;
  55. }
  56. void Generate(Node &node,int r,int c)
  57. {
  58.     //不再open和close中
  59.     child.r=r;
  60.     child.c=c;
  61.     list<Node>::iterator posOP=find_if(openList.begin(),openList.end(),check);
  62.     list<Node>::iterator posCL=find_if(closeList.begin(),closeList.end(),check);
  63.     if (posOP==openList.end()&&posCL==closeList.end())
  64.     {
  65.         child.parent=&node;
  66.         child.g=GetCost(r,c)+node.g;    //g+1
  67.         child.h=abs(tr-r)+abs(tc-c);    //(tr-r)*(tr-r)+(tc-c)*(tc-c)
  68.         child.f=child.g+child.h;
  69.         openList.push_back(child);
  70.         openList.sort(dayu);
  71.     }
  72. }
  73. void kuozhan(Node &node)
  74. {
  75.     if (Reach(node.r,node.c+1))//右
  76.     {
  77.         Generate(node,node.r,node.c+1);
  78.     }
  79.     if (Reach(node.r,node.c-1))// 左 
  80.     {
  81.         Generate(node,node.r,node.c-1);
  82.     }
  83.     if (Reach(node.r-1,node.c))// 上 
  84.     {
  85.         Generate(node,node.r-1,node.c);
  86.     }
  87.     if (Reach(node.r+1,node.c))// 下
  88.     {
  89.         Generate(node,node.r+1,node.c);
  90.     }
  91. }
  92. int main()
  93. {
  94.     freopen("in.txt","r",stdin);
  95.     int i,j;
  96.     Node temp,best;
  97.     Count=1;
  98.     while (cin>>R>>C)
  99.     {
  100.         res=-1;
  101.         cin>>vp>>vs>>vt;
  102.         for (i=0;i<R;i++)
  103.             for (j=0;j<C;j++)
  104.             {
  105.                 cin>>rmap[i][j];
  106.             }
  107.         cin>>sr>>sc>>tr>>tc;
  108.         temp.r=sr;
  109.         temp.c=sc;
  110.         temp.g=0;
  111.         temp.h=abs(tr-sr)+abs(tc-sc); //4反向 //(tr-r)*(tr-r)+(tc-c)*(tc-c)八方向
  112.         temp.f=temp.g+temp.h;
  113.         temp.parent=NULL;
  114.         if (!openList.empty())
  115.         {
  116.             openList.clear();           
  117.         }
  118.         openList.push_back(temp);
  119.         closeList.clear();
  120.         while (!openList.empty())
  121.         {
  122.             best=openList.back();
  123.             openList.pop_back();
  124.             if (best.r==tr&&best.c==tc)
  125.             {
  126.                 res=best.f;
  127.                 break;
  128.             }
  129.             kuozhan(best);
  130.             closeList.push_back(best);
  131.         }
  132.         cout<<"Case "<<Count++<<": "<<res<<endl;
  133.     }
  134.     return 0;
  135. }
  136. 输入:
  137. 4 6
    1 2 10
    T...TT
    TTT###
    TT.@#T
    ..###@
    0 1 3 0
  138. 4 6
    1 2 2
    T...TT
    TTT###
    TT.@#T
    ..###@
    0 1 3 0
  139. 2 2
    5 1 3
    T@
    @.
    0 0 1 1
  140. 输出:
  141. Case 1: 14
    Case 2: 8
    Case 3: -1

"T"是树,"."是沙,"#"是路,"@"是石头不能通过,因为最后需要输出的是通过的时间,所以没保存路径.这样parent中保存的也是有错的,呵呵

原创粉丝点击