Rescue

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

题意:给你一个n*m的矩阵,代表n*m个方格,每个方格可能是a,你的目的地,r你的起点,x警卫,#墙壁‘。’道路。每次你只能走一步,上下左右,且每一步花费一分钟,如果有路可以直接走,如果是x就必须杀死他才能走,且杀死他也要花费一分钟,遇到墙壁就不能走问你从起点到终点,最短的时间,如果不能就输出那么一句话。
题解:用bfs,广收。

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #include<queue>
  6 using namespace std;
  7 char map[202][202];// 储存原来的地图
  8 int mit[202][202]; //储存任何一点到起点的最短距离
  9 int n,m,ans,sx,sy,qx,qy;
 10 struct Node{
 11    int x;
 12    int y;
 13    int tim;
 14 
 15 };//储存当前节点的坐标以及到 起点的距离(花费的时间)
 16 queue<Node> Q;
 17 void  bfs(int i,int j){
 18    Node temp;
 19    temp.x=i;
 20    temp.y=j;
 21    temp.tim=0;
 22    Q.push(temp);//压入起点
 23    while(!Q.empty()){
 24    Node temp=Q.front();//队列没有top()方法,堆栈才有,别再犯错
 25    Node temp2;
 26        Q.pop();//取出队首元素
 27        int ax=temp.x;
 28        int b=temp.y;
 29        int l=temp.tim;
 30       if(ax>1){ //防止越界
 31 
 32           if(map[ax-1][b]=='.'||map[ax-1][b]=='a'){//注意这里也可能是a,一开始就忘了这条,所以结果是1
 33              temp2.x=ax-1;
 34              temp2.y=b;
 35              temp2.tim=l+1;
 36              if(temp2.tim<mit[ax-1][b]){
 37                mit[ax-1][b]=temp2.tim; //这里很重要,我要把当前点到起点距离最少的额那个距离放到队列里,否则就不是最短的花费
 38              Q.push(temp2);
 39              }
 40              }
 41            if(map[ax-1][b]=='x'){ //相似的处理啊
 42              temp2.x=ax-1;
 43              temp2.y=b;
 44              temp2.tim=l+2;
 45              if(temp2.tim<mit[ax-1][b]){
 46                mit[ax-1][b]=temp2.tim;
 47              Q.push(temp2);
 48              }
 49            }
 50       }
 51        if(ax<n){
 52 
 53           if(map[ax+1][b]=='.'||map[ax+1][b]=='a'){
 54              temp2.x=ax+1;
 55              temp2.y=b;
 56              temp2.tim=l+1;
 57           if(temp2.tim<mit[ax+1][b]){
 58             mit[ax+1][b]=temp2.tim;
 59              Q.push(temp2);
 60              }
 61              }
 62            if(map[ax+1][b]=='x'){
 63              temp2.x=ax+1;
 64              temp2.y=b;
 65              temp2.tim=l+2;
 66              if(temp2.tim<mit[ax+1][b]){
 67                mit[ax+1][b]=temp2.tim;
 68              Q.push(temp2);
 69              }
 70            }
 71       }
 72       if(b>1){
 73 
 74           if(map[ax][b-1]=='.'||map[ax][b-1]=='a'){
 75              temp2.x=ax;
 76              temp2.y=b-1;
 77              temp2.tim=l+1;
 78              if(temp2.tim<mit[ax][b-1]){
 79                mit[ax][b-1]=temp2.tim;
 80              Q.push(temp2);
 81              }
 82              }
 83            if(map[ax][b-1]=='x'){
 84              temp2.x=ax;
 85              temp2.y=b-1;
 86              temp2.tim=l+2;
 87              if(temp2.tim<mit[ax][b-1]){
 88                mit[ax][b-1]=temp2.tim;
 89              Q.push(temp2);
 90              }
 91            }
 92       }
 93        if(b<m){
 94 
 95           if(map[ax][b+1]=='.'||map[ax][b+1]=='a'){
 96              temp2.x=ax;
 97              temp2.y=b+1;
 98              temp2.tim=l+1;
 99              if(temp2.tim<mit[ax][b+1]){
100                mit[ax][b+1]=temp2.tim;
101              Q.push(temp2);
102              }
103              }
104            if(map[ax][b+1]=='x'){
105              temp2.x=ax;
106              temp2.y=b+1;
107              temp2.tim=l+2;
108           if(temp2.tim<mit[ax][b+1]){
109                mit[ax][b+1]=temp2.tim;
110              Q.push(temp2);
111            }
112       }
113    }
114 
115    }}
116 int main(){
117  while(~scanf("%d%d",&n,&m)){
118 
119 
120    for(int i=1;i<=n;i++){
121       for(int j=1;j<=m;j++){
122          cin>>map[i][j];
123          mit[i][j]=100000 ;//初始化为最大值
124          if(map[i][j]=='r'){
125              qx=i;
126              qy=j;
127              }   //标记起点
128           if(map[i][j]=='a'){
129             sx=i;
130             sy=j;  //标记终点
131         }
132        }
133 
134       }
135 
136       bfs(qx,qy);
137        if(mit[sx][sy]!=100000)
138        printf("%d
",mit[sx][sy]);
139        else
140         printf("Poor ANGEL has to stay in the prison all his life.
");
141 
142 }
143 }
View Code
原文地址:https://www.cnblogs.com/chujian123/p/3366452.html