zoukankan      html  css  js  c++  java
  • zoj 1649 Rescue

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

    解题思路:BFS搜索

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: zoj 1649
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 typedef long long LL;
     26 const double PI=acos(-1.0);
     27 
     28 const int x4[]={-1,0,1,0};
     29 const int y4[]={0,1,0,-1};
     30 const int x8[]={-1,-1,0,1,1,1,0,-1};
     31 const int y8[]={0,1,1,1,0,-1,-1,-1};
     32 
     33 typedef int T;
     34 T max(T a,T b){ return a>b? a:b; }
     35 T min(T a,T b){ return a<b? a:b; }
     36 ///////////////////////////////////////////////////////////////////////////
     37 
     38 ///////////////////////////////////////////////////////////////////////////
     39 //Add Code:
     40 const int INF=1000000;
     41 int n,m,Ax,Ay,Min[205][205];
     42 char prison[205][205];
     43 
     44 struct Node{
     45     int x,y,time;
     46 };
     47 
     48 queue<Node> q;
     49 
     50 void BFS(Node s){
     51     q.push(s);
     52     while(!q.empty()){
     53         Node temp=q.front();
     54         q.pop();
     55         for(int i=0;i<4;i++){
     56             int x=temp.x+x4[i],y=temp.y+y4[i];
     57             if(x<1 || x>n || y<1 || y>m || prison[x][y]=='#') continue;
     58             Node res=Node{x,y,temp.time+1};
     59             if(prison[x][y]=='x') res.time++;
     60             if(x==Ax && y==Ay){
     61                 if(res.time<Min[x][y]) Min[x][y]=res.time;
     62             }
     63             else{
     64                 if(res.time<Min[x][y]){
     65                     Min[x][y]=res.time;
     66                     q.push(res);
     67                 }
     68             }
     69         }
     70     }
     71 }
     72 ///////////////////////////////////////////////////////////////////////////
     73 
     74 int main(){
     75     ///////////////////////////////////////////////////////////////////////
     76     //Add code:
     77     while(scanf("%d%d",&n,&m)!=EOF){
     78         int i,j,Rx,Ry;
     79         char c;
     80         scanf("%c",&c);
     81         for(i=1;i<=n;i++){
     82             for(j=1;j<=m;j++){
     83                 scanf("%c",&prison[i][j]);
     84                 Min[i][j]=INF;
     85                 if(prison[i][j]=='a'){
     86                     Ax=i;
     87                     Ay=j;
     88                 }
     89                 else if(prison[i][j]=='r'){
     90                     Rx=i;
     91                     Ry=j;
     92                     Min[i][j]=0;
     93                 }
     94             }
     95             scanf("%c",&c);
     96         }
     97         Node node=Node{Rx,Ry,0};
     98         BFS(node);
     99         if(Min[Ax][Ay]<INF) printf("%d
    ",Min[Ax][Ay]);
    100         else printf("Poor ANGEL has to stay in the prison all his life.
    ");
    101     }
    102     ///////////////////////////////////////////////////////////////////////
    103     return 0;
    104 }
    105 
    106 ///////////////////////////////////////////////////////////////////////////
    107 /*
    108 Testcase:
    109 Input:
    110 7 8
    111 #.#####.
    112 #.a#..r.
    113 #..#x...
    114 ..#..#.#
    115 #...##..
    116 .#......
    117 ........
    118 Output:
    119 13
    120 */
    121 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    Time Zone 【模拟时区转换】(HDU暑假2018多校第一场)
    HDU 1281 棋盘游戏 【二分图最大匹配】
    Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
    Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】
    Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】
    Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C. Connect Three 【模拟】
    Avito Cool Challenge 2018 E. Missing Numbers 【枚举】
    Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】
    005 如何分析问题框架
    004 如何定义和澄清问题
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3270830.html
Copyright © 2011-2022 走看看