zoukankan      html  css  js  c++  java
  • HDU 1026 Ignatius and the Princess I (搜索)

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

    很明显宽搜,有点像dijkstra算法,使用优先队列每次取一个最小的节点然后更新相邻节点。

    到达终点直接退出,之后寻找路径-记录父节点或者从后往前找(应该都可以吧,我用的后者)

    刚开始用的深搜直接超时,这几天写深搜写习惯了。脑残了。

    代码:

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include <functional>
      3 #include <algorithm>
      4 #include <iostream>
      5 #include <cstring>
      6 #include <cassert>
      7 #include <cstdio>
      8 #include <cctype>
      9 #include <vector>
     10 #include <string>
     11 #include <queue>
     12 #include <stack>
     13 #include <cmath>
     14 #include <map>
     15 #include <set>
     16 using namespace std;
     17 #define rep(i,a,n) for (int i=a;i<n;i++)
     18 #define per(i,a,n) for (int i=n-1;i>=a;i--)
     19 #define pb push_back
     20 #define mp make_pair
     21 #define all(x) (x).begin(),(x).end()
     22 #define fi first
     23 #define se second
     24 #define SZ(x) ((int)(x).size())
     25 typedef vector<int> VI;
     26 typedef long long ll;
     27 typedef pair<int, int> PII;
     28 const ll mod = 1000000007;
     29 ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res*a%mod; a = a*a%mod; }return res; }
     30 // head
     31 const int inf = 0x3f3f3f3f;
     32 #define maxn 205
     33 int iadd[] = {0, 1, 0, -1}, jadd[] = {1, 0, -1, 0};
     34 struct rtnode{
     35     int i, j;
     36     int fight;
     37     rtnode(): i(0), j(0), fight(0) {}
     38     rtnode(int f, int t, int fi): i(f), j(t), fight(fi) {}
     39     bool operator < (const rtnode & t)const {
     40         return fight > t.fight;
     41     }
     42 }bestrt[maxn * maxn];
     43 int mv[maxn][maxn];
     44 char maze[maxn][maxn];
     45 int n, m;
     46 bool flag;
     47 priority_queue<rtnode> q;
     48 stack<rtnode> s;
     49 
     50 int bfs(){
     51     flag = false;
     52     memset(mv, -1, sizeof(mv));
     53     while(!s.empty())
     54         s.pop();
     55     while(!q.empty())
     56         q.pop();
     57     q.push(rtnode(0, 0, 0));
     58     mv[0][0] = 0;
     59     while(!q.empty()){
     60         rtnode tmnode = q.top();
     61         q.pop();
     62         
     63         if(tmnode.i == n - 1 && tmnode.j == m - 1){
     64             flag = true;
     65             break;
     66         }
     67         
     68         for(int i = 0; i < 4; i++){
     69             int nexti, nextj;
     70             nexti = tmnode.i + iadd[i]; nextj = tmnode.j + jadd[i];
     71             if(nexti < 0 || nextj < 0 || nexti >= n || nextj >= m)
     72                 continue;
     73             if(mv[nexti][nextj] != -1 || maze[nexti][nextj] == 'X')
     74                 continue;
     75             int tmpl = 1 + tmnode.fight;
     76             if(isdigit(maze[nexti][nextj])){
     77                 tmpl += maze[nexti][nextj] - '0';
     78             }
     79             mv[nexti][nextj] = tmpl;
     80             q.push(rtnode(nexti, nextj, tmpl));
     81         }
     82     }
     83     if(!flag)
     84         return 0;
     85     s.push(rtnode(n - 1, m - 1, mv[n - 1][m - 1]));
     86     while(true){
     87         rtnode tmn = s.top();
     88         if(tmn.i == 0 && tmn.j == 0)
     89             break;
     90         for(int i = 0; i < 4; i++){
     91             int nexti, nextj;
     92             nexti = tmn.i + iadd[i]; nextj = tmn.j + jadd[i];
     93             if(nexti < 0 || nextj < 0 || nexti >= n || nextj >= m)
     94                 continue;
     95             int tms = 1;
     96             if(isdigit(maze[tmn.i][tmn.j])){
     97                 tms += maze[tmn.i][tmn.j] - '0';
     98             }
     99             if(mv[nexti][nextj] == tmn.fight - tms){
    100                 s.push(rtnode(nexti, nextj, mv[nexti][nextj]));
    101                 break;
    102             }
    103         }
    104     }
    105     for(int i = 0; !s.empty(); i++){
    106         bestrt[i] = s.top();
    107         bestrt[i].fight = 0;
    108         int tmi = bestrt[i].i, tmj = bestrt[i].j;
    109         if(isdigit(maze[tmi][tmj]))
    110             bestrt[i].fight = maze[tmi][tmj] - '0';
    111         s.pop();
    112     }
    113     return mv[n - 1][m - 1];
    114 }
    115 
    116 int main(){
    117     while(scanf("%d %d", &n, &m) == 2){
    118         
    119         for(int i = 0; i < n; i++){
    120             for(int j = 0; j < m; j++){
    121                 scanf(" %c", &maze[i][j]);
    122             }
    123         }
    124         
    125         int minstp = bfs();
    126         
    127         if(!flag){
    128             puts("God please help our poor hero.
    FINISH");
    129         }
    130         else{
    131             printf("It takes %d seconds to reach the target position, let me show you the way.
    ", minstp);
    132             int cnt = 1;
    133             for(int i = 1; cnt <= minstp; i++){
    134                 printf("%ds:(%d,%d)->(%d,%d)
    ", cnt, bestrt[i - 1].i, bestrt[i - 1].j, bestrt[i].i, bestrt[i].j);
    135                 if(bestrt[i].fight != 0){
    136                     for(int k = 1; k <= bestrt[i].fight; k++){
    137                         printf("%ds:FIGHT AT (%d,%d)
    ", cnt + k, bestrt[i].i, bestrt[i].j);
    138                     }
    139                     cnt += bestrt[i].fight + 1;
    140                 }
    141                 else{
    142                     cnt++;
    143                 }
    144             }
    145             printf("FINISH
    ");
    146             
    147         }
    148     }
    149 }
    150 /*
    151 5 6
    152 .XX.1.
    153 ..X.2.
    154 2...X.
    155 ...XX.
    156 XXXXX.
    157 5 6
    158 .XX.1.
    159 ..X.2.
    160 2...X.
    161 ...XX.
    162 XXXXX1
    163 5 6
    164 .XX...
    165 ..XX1.
    166 2...X.
    167 ...XX.
    168 XXXXX.
    169 */

    题目:

    Ignatius and the Princess I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18947    Accepted Submission(s): 6131
    Special Judge


    Problem Description
    The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

    1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
    2.The array is marked with some characters and numbers. We define them like this:
    . : The place where Ignatius can walk on.
    X : The place is a trap, Ignatius should not walk on it.
    n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

    Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
     
    Input
    The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
     
    Output
    For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
     
    Sample Input
    5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX. 5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX1 5 6 .XX... ..XX1. 2...X. ...XX. XXXXX.
     
    Sample Output
    It takes 13 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) FINISH It takes 14 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) 14s:FIGHT AT (4,5) FINISH God please help our poor hero. FINISH
  • 相关阅读:
    20145328 《信息安全系统设计基础》第6周学习总结
    20145328 《信息安全系统设计基础》第5周学习总结
    2017-2018-2 《网络对抗技术》 20155322 第二周 Exp1 PC平台逆向破解(5)M
    2017-2018-1 《信息安全系统设计基础》 20155322 十六周 课上实践
    2017-2018-1 《信息安全系统设计基础》 20155322 十六周 课下实践
    20155322 2017-2018-1《信息安全系统设计基础》课程总结
    20155322 2017-2018-1《信息安全系统设计基础》第十四周学习总结
    20155322 2017-2018-1 《信息安全系统设计基础》 第十三周学习总结
    20155322 2017-2018-1《信息安全系统设计基础》实验五-通信协议设计
    20155322 2017-2018-1《信息安全系统设计基础》实验四-外设驱动程序设计
  • 原文地址:https://www.cnblogs.com/bolderic/p/6861998.html
Copyright © 2011-2022 走看看