• HDOJ 1180


    我的code: 超时,不知道怎么优化,望大神指点

      1 #include<iostream>
      2 #include <string>
      3 #include <queue>
      4 using namespace std;
      5 
      6 struct StepPoint  
      7 {
      8     int X,Y,Val;
      9     const bool operator<(const StepPoint &x)const { return x.Val<Val; }
     10 };
     11 StepPoint beginP,endP;
     12 static int moves[4][2]={-1,0,0,-1,1,0,0,1};
     13 char maps[22][22];
     14 
     15 int StepBFS()
     16 {
     17     priority_queue<StepPoint> q;
     18     q.push(beginP);
     19     while (!q.empty())
     20     {
     21         StepPoint p=q.top();
     22         q.pop();
     23         StepPoint p1=p;
     24         p1.Val++;
     25         q.push(p1);
     26 
     27         for (int i=0;i<4;i++)
     28         {
     29             int x=p.X+moves[i][0];
     30             int y=p.Y+moves[i][1];
     31             if(maps[x][y]=='.')
     32             {
     33                 StepPoint pNew;
     34                 pNew.X=x;pNew.Y=y;pNew.Val=p.Val+1;
     35                 q.push(pNew);
     36                 maps[x][y]=='*';
     37             }
     38             else if(maps[x][y]=='|'&&(
     39                 ( (p.Val+1)%2==0&&(i==0||i==2)) ||
     40                 ( (p.Val+1)%2!=0&&(i==1||i==3)) ))
     41             {
     42                 StepPoint pNews;
     43                 pNews.X=x+moves[i][0];
     44                 pNews.Y=y+moves[i][1];
     45                 pNews.Val=p.Val+1;
     46                 if(maps[pNews.X][pNews.Y]=='.')
     47                 {
     48                     q.push(pNews);
     49                     maps[pNews.X][pNews.Y]=='*';
     50                 }
     51             }
     52             else if(maps[x][y]=='-'&&(
     53                 ( (p.Val+1)%2==0&&(i==1||i==3)) ||
     54                 ( (p.Val+1)%2!=0&&(i==0||i==2)) ))
     55             {
     56                 StepPoint pNews;
     57                 pNews.X=x+moves[i][0];
     58                 pNews.Y=y+moves[i][1];
     59                 pNews.Val=p.Val+1;
     60                 if(maps[pNews.X][pNews.Y]=='.')
     61                 {
     62                     q.push(pNews);
     63                     maps[pNews.X][pNews.Y]=='*';
     64                 }
     65             }
     66             else if(maps[x][y]=='T')
     67                 return p.Val+1;
     68         }
     69     }
     70 }
     71 
     72 int main()
     73 {
     74     int n,m,i,j;
     75     
     76     while(cin>>n>>m)
     77     {
     78         for (i=0;i<22;i++)
     79         {
     80             memset(maps[i],'*',sizeof(maps[i]));
     81         }
     82         
     83         for (i=1;i<=n;i++)
     84         {
     85             for (j=1;j<=m;j++)
     86             {
     87                 cin>>maps[i][j];
     88                 if(maps[i][j]=='S')
     89                 {
     90                     beginP.X=i;
     91                     beginP.Y=j;
     92                     beginP.Val=0;
     93                 }
     94                 if(maps[i][j]=='T')
     95                 {
     96                     endP.X=i;
     97                     endP.Y=j;
     98                 }
     99             }
    100         }
    101         cout<<StepBFS()<<endl;
    102     }
    103     return 0;
    104 }
    View Code
  • 相关阅读:
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
    @RequestParam设置默认可以传空值
    python清空列表的方法
    Git分支管理的基本操作
    判断一个坐标点是否在不规则多边形内部的算法
    Flask 解析 Web 端 请求 数组
    SQL server 获取异常
    SQL server 大量数据导入和系统运行慢的问题
    SQL SERVER 跨服务器连接
    #pragma once
  • 原文地址:https://www.cnblogs.com/ouweiqideblog/p/3286113.html
Copyright © 2020-2023  润新知