zoukankan      html  css  js  c++  java
  • 优先队列----坦克大战

    第一次 做的  优先队列      发现了 不少 问题  基础 不是  很熟练 常常  犯 基础性错误    写完就去 回顾基础      !!!!!!!!

       这种 有权图  以后  就应该用  优先队列 去做了    不然 就 上午的想法   一塌糊涂      还是这种  优先队列    求 有权图最短路径方便一点 

        题也挺简单的 , 就是基础不是很牢固 . 下面附上水货 代码  

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 using namespace std;
     9 struct node
    10 {
    11     int x,y,step;
    12     friend bool operator <(node s1,node s2)      //   定义结构体   的时候   这个 就是  用于 优先队列的   基准排序 
    13     {
    14         return s1.step>s2.step;
    15     }
    16 };
    17 char a[305][305];
    18 int bx,by,n,m,visited[305][305],b[4][2]={0,-1,0,1,-1,0,1,0};
    19 int ans=1000000;
    20 priority_queue<node>Q;
    21 void BFS(int x,int y)
    22 {
    23     node q;
    24     q={x,y,0};
    25     Q.push(q);
    26     while(!Q.empty())
    27     {
    28         node e=Q.top();         //  现在的  压迫 已经预示了   在未来某个对称时间的   爆发  .
    29         for(int i=0;i<4;i++)
    30         {
    31             node w;
    32             w.x=e.x+b[i][0];
    33             w.y=e.y+b[i][1];
    34             if(w.x>=0&&w.x<m&&w.y>=0&&w.y<n&&a[w.y][w.x]!='R'&&a[w.y][w.x]!='S'&&!visited[w.y][w.x])
    35             {
    36                 visited[w.y][w.x]=1;
    37                 if(a[w.y][w.x]=='B')
    38                 {
    39                     w.step=e.step+2;
    40                     Q.push(w);
    41                 }
    42                 else
    43                 {
    44                     w.step=e.step+1;
    45                     Q.push(w);
    46                 }
    47                 if(a[w.y][w.x]=='T')
    48                 {
    49                     ans=w.step;
    50                     break;
    51                 }
    52             }
    53             if(ans!=1000000)
    54                 break;
    55         }
    56         Q.pop();
    57     }
    58     while(!Q.empty())
    59         Q.pop();
    60 }
    61 int main()
    62 {
    63     while(scanf("%d%d",&n,&m),(n||m))
    64     {
    65         memset(a,'',sizeof(a));
    66         memset(visited,0,sizeof(visited));
    67         for(int i=0;i<n;i++)
    68             for(int j=0;j<m;j++)
    69         {
    70             scanf(" %c",&a[i][j]);
    71             if(a[i][j]=='Y')
    72             {
    73                 bx=j;
    74                 by=i;
    75             }
    76         }
    77         ans=1000000;
    78         visited[by][bx]=1;
    79         BFS(bx,by);
    80         if(ans==1000000)
    81             printf("-1
    ");
    82         else
    83             printf("%d
    ",ans);
    84     }
    85 }
  • 相关阅读:
    2020.10.23 19级training 补题报告
    2020.10.17 天梯赛练习 补题报告
    2020.10.16 19级training 补题报告
    2020.10.9 19级training 补题报告
    2020.10.10 天梯赛练习 补题报告
    2020.10.3 天梯赛练习 补题报告
    2020.10.2 19级training 补题报告
    第十届山东省ACM省赛复现补题报告
    VVDI Key Tool Plus Adds VW Passat 2015 Key via OBD
    Xhorse VVDI Prog Software V5.0.3 Adds Many MCUs
  • 原文地址:https://www.cnblogs.com/A-FM/p/5302493.html
Copyright © 2011-2022 走看看