• hdu Rescue (bfs)


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

    简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz

    代码:

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <math.h>
      4 #include <algorithm>
      5 #include <iostream>
      6 #include <ctype.h>
      7 #include <iomanip>
      8 #include <queue>
      9 #include <stdlib.h>
     10 using namespace std;
     11 
     12 int N,M,ans;
     13 char mp[201][201];
     14 int vis[201][201];
     15 
     16 int dx[4]={0,0,-1,1};
     17 int dy[4]={-1,1,0,0};
     18 
     19 int sx,sy;
     20 int ex,ey;
     21 
     22 struct Node
     23 {
     24     int x,y;
     25     int step;
     26 };
     27 
     28 bool operator<(Node a,Node b)//定义结构体类型的优先队列的优先级,从小到大
     29 {  
     30     return a.step>b.step; 
     31 }
     32 
     33 void getMap(int n,int m)  
     34 {  
     35     for(int i=0;i<n;i++)  
     36         for(int j=0;j<m;j++)  
     37         {  
     38             cin>>mp[i][j];  
     39             if(mp[i][j]=='r')  
     40             {  
     41                 sx=i;  
     42                 sy=j;  
     43             }  
     44             if(mp[i][j]=='a')  
     45             {  
     46                 ex=i;  
     47                 ey=j;  
     48             }  
     49         }  
     50 }  
     51 
     52 bool pd(int x,int y)
     53 {  
     54     if(x>=0&&x<N&&y>=0&&y<M&&!vis[x][y]&&mp[x][y]!='#')  
     55         return true;  
     56     return false;  
     57 } 
     58 
     59 int bfs(int x,int y)
     60 {
     61     memset(vis,0,sizeof(vis));  
     62     priority_queue<Node>q;  
     63     Node a,b;  
     64     a.x=x,a.y=y,a.step=0;  
     65     q.push(a); 
     66     while(!q.empty()){
     67         b=q.top();
     68         q.pop();
     69         for(int i=0; i<4; i++){
     70             int px=b.x+dx[i];
     71             int py=b.y+dy[i];
     72             if(pd(px,py)){
     73                 vis[px][py]=1;
     74                 a.x=px;
     75                 a.y=py;
     76                 if(mp[px][py]=='x'){
     77                     a.step=b.step+2;
     78                     q.push(a);
     79                 }
     80                 else
     81                 {
     82                     a.step=b.step+1;
     83                     q.push(a);
     84                     if(px==ex && py==ey)
     85                         return a.step;
     86                 }
     87             }
     88         }
     89     }
     90     return -1; 
     91 }
     92 
     93 int main()
     94 {
     95     
     96     while(cin>>N>>M){
     97     getMap(N,M);
     98     int ans=bfs(sx,sy);
     99     if(ans==-1)
    100         printf("Poor ANGEL has to stay in the prison all his life.
    ");
    101     else
    102         printf("%d
    ",ans);
    103     }
    104 }

    优先队列搜索:

     1 ///优先队列是默认int从大到小priority_queue<int>q1,也可以定义为从小到大priority_queue<int,vector<int>,greater<int> >q2;
     2 也可以自定义优先级,重载<
     3 
     4 #include <iostream>
     5 #include <functional>
     6 #include <queue>
     7 using namespace std;
     8 
     9 ///自定义优先级,两种写法,按照优先级从大到小的顺序
    10 /*
    11 struct node
    12 {
    13     friend bool operator<(node n1,node n2)
    14     {
    15         return n1.priority<n2.priority;
    16     }
    17     int priority;
    18     int value;
    19 };*/
    20 
    21 struct node
    22 {
    23     int priority;
    24     int value;
    25 };
    26 bool operator<(node a,node b)
    27 {
    28     return a.priority<b.priority;
    29 }
    30 
    31 
    32 int main()
    33 {
    34     const int len=5;
    35     int i;
    36     int a[len]={3,5,9,6,2};
    37     //优先队列中从大到小输出
    38     priority_queue<int>q1;
    39     for(i=0;i<len;i++)
    40         q1.push(a[i]);
    41     for(int i=0;i<len;i++)
    42     {
    43         cout<<q1.top();
    44         q1.pop();
    45     }
    46     cout<<endl;
    47     //优先队列中从小到大输出
    48 
    49     /**
    50     priority_queue<int,vector<int>,greater<int> >q2;
    51     for(i=0;i<len;i++)
    52         q2.push(a[i]);
    53     for(i=0;i<len;i++)
    54     {
    55         cout<<q2.top();
    56         q2.pop();
    57     }*/
    58 
    59     priority_queue<int,vector<int>,greater<int> >q;
    60     for(int i=0;i<len;i++)
    61         q.push(a[i]);
    62     while(!q.empty())
    63     {
    64         cout<<q.top();
    65         q.pop();
    66     }
    67     cout<<endl;
    68     //按照某个优先级输出,该代码中为priority值大的先输出
    69     priority_queue<node>q3;
    70     node b[len];
    71     b[0].priority=6;b[0].value=1;
    72     b[1].priority=9;b[1].value=5;
    73     b[2].priority=2;b[2].value=3;
    74     b[3].priority=8;b[3].value=2;
    75     b[4].priority=1;b[4].value=4;
    76     for(i=0;i<len;i++)
    77         q3.push(b[i]);
    78     cout<<"优先级"<<'	'<<""<<endl;
    79     for(i=0;i<len;i++)
    80     {
    81         cout<<q3.top().priority<<'	'<<q3.top().value<<endl;
    82         q3.pop();
    83     }
    84     return 0;
    85 }

    可见链接:http://blog.csdn.net/sr_19930829/article/details/42706469

    运行:

    96532
    23569

    优先级  值
    9       5
    8       2
    6       1
    2       3
    1       4

  • 相关阅读:
    Mac eclipse导入项目中文乱码问题解决
    初识Freemarker
    Mac 导入maven项目详解
    Mac iTerm2使用总结
    HTML学习笔记——标签
    仿QQ大战—界面篇
    Java之类的构造器(反射)
    JAVA之IO流(字符流)
    JAVA之IO流(字节流)
    仿QQ大战—服务器的搭建(ServerSocket)
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/5236410.html
Copyright © 2020-2023  润新知