hdu1242

链接:点击打开链接

题意:r为起点,a为终点,没走一步需要花费一个单位时间,x为怪,遇到怪可以考虑打怪,打怪会花费一个单位时间,问走到终点最少花费时间为多少

代码:

#include <queue>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int n,m,enx,eny;
int xx[]={-1,0,1,0};
int yy[]={0,-1,0,1};
char s[205][205];
struct node{
    int x,y,sum;
};
struct cmp{
    bool operator()(node a,node b){
        return a.sum>b.sum;
    }
};
int bfs(node st){
    priority_queue<node,vector<node>,cmp>q; //时间最少要用优先队列
    node cur,temp;
    int i,j;
    q.push(st);
    while(q.size()){
        cur=q.top();q.pop();
        if(cur.x==enx&&cur.y==eny)          //这里要用坐标,因为所有走过的路都变为了#
        return cur.sum;
        for(i=0;i<4;i++){
            temp.x=cur.x+xx[i];
            temp.y=cur.y+yy[i];
            if(temp.x>=1&&temp.x<=n&&temp.y>=1&&temp.y<=m)
            if(s[temp.x][temp.y]!='#'){
                if(s[temp.x][temp.y]=='.')
                temp.sum=cur.sum+1;
                else if(s[temp.x][temp.y]=='x')
                temp.sum=cur.sum+2;
                q.push(temp);
                s[temp.x][temp.y]='#';      //走过的就变为#
            }
        }
    }
    return 0;
}                                            //bfs模板
int main(){
    int i,j,ans;
    node st;
    while(cin>>n>>m){
        for(i=1;i<=n;i++)
        for(j=1;j<=m;j++){
            cin>>s[i][j];
            if(s[i][j]=='r')
            st.x=i,st.y=j,st.sum=0;
            if(s[i][j]=='a')
            enx=i,eny=j;
        }
        ans=bfs(st);
        if(ans!=0)
        printf("%d\n",ans);
        else
        printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-28 22:52:23

hdu1242的相关文章

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU1242 (BFS搜索中使用优先队列)

一道用到优先队列的BFS题目 #include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue> #define N 201 using namespace std; char maze[N][N]; int a,b,anw; bool visit[N][N]; int dir[4][2]={{0,1},{1,0},{-1,0},{

HDU1242 Rescue

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 参考博文1:http://blog.csdn.net/iaccepted/article/details/23101875 参考博文2:http://blog.csdn.net/libin56842/article/details/9039351 该题类似于走迷宫,但不完全是,因为在走的过程中会遇到警察,可以选择杀死警察,但要付出相应的代价,时间加1.用走迷宫的算法只是能找到angle到队友最

hdu1242 优先队列+bfs

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22034    Accepted Submission(s): 7843 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

hdu1242(Rescue)

点击打开链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

[hdu1242]优先队列

题意:给一个地图,'x'走一步代价为2,'.'走一步代价为1,求从s到t的最小代价.裸优先队列. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #

hdu1242 bfs

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24608    Accepted Submission(s): 8686 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

hdu1242 Rescue(BFS)

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21701    Accepted Submission(s): 7745 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

hdu1242 DFS基础(回溯的重要性)

题目大意:在迷宫里从a出发走到r,每走一格时间+1,但是遇到x时间还要额外+1,求最短的时间. 题解:直接dfs把每一个格子都走一遍,设置一个时间参数,走一格就+1,还要注意回溯和剪枝. 很多新手都会疑惑,回溯有什么用呢?回溯的作用就是在分叉口时你选择了这一条路,往这条路一直走不可回头(用访问数组标记走过的路),走到尽头时,你重新回到那个分叉口(访问数组取消标记),你上一次走的路对于你现在来说是没有走过的,dfs过程中有许多分叉口,所以要用回溯才能走遍每一条路. 具体思路看代码以及注释 1 #i