hdu 4308 Saving Princess claire_ BFS

为了准备算法考试刷的,想明白一点就行,所有的传送门相当于一个点,当遇到一个传送门的时候,把所有的传送门都压入队列进行搜索

贴代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

const int MAXN = 5000+50;
int r,c,f,si,sj,ei,ej,ans;
int mat[MAXN][MAXN];

struct Node
{
    Node(int i=0,int j=0,int v=0){x=i;y=j;val=v;}
    int x,y;
    int val;
};

int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//left up down right

void init()
{
    char cc;
    ans=-1;
    getchar();
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            cc=getchar();   //putchar(cc);
            if(cc == 'Y'){mat[i][j]=2;si=i,sj=j;continue;}
            if(cc == 'C'){mat[i][j]=3;ei=i;ej=j;continue;}
            if(cc == '*'){mat[i][j]=0;continue;}
            if(cc == '#'){mat[i][j]=-1;continue;}
            if(cc == 'P'){mat[i][j]=1;continue;}
        }
        getchar();
    }
}

int legal(Node &s)
{
    if(s.x>=0 && s.y>=0 && s.x<r && s.y <c)
        return 1;
    return 0;
}

int bfs()
{
    queue<Node>q;
    Node st(si,sj),e;
    q.push(st);
    mat[si][sj]=-2;
    while(!q.empty())
    {
        st=q.front();
        //if(mat[st.x][st.y]==3)return st.val;  chao sha bi
        if(st.x==ei && st.y == ej)return st.val;
        q.pop();
        for(int k=0;k<4;k++)
        {
            e.x=st.x+dir[k][0];
            e.y=st.y+dir[k][1];
            e.val=st.val;
            if(legal(e) && mat[e.x][e.y]>=0 && mat[e.x][e.y]!=2)
            {
                if(!mat[e.x][e.y])//*
                {
                    e.val=st.val+f;
                    q.push(e);
                }
                if(mat[e.x][e.y]==1)
                {
                    for(int ii=0;ii<r;ii++)
                    {
                        for(int jj=0;jj<c;jj++)
                        {
                            if(mat[ii][jj]==1)
                            {
                                mat[ii][jj]=-2;
                                q.push(Node(ii,jj,e.val));
                            }

                        }
                    }
                }
               if(mat[e.x][e.y]>1)q.push(e);
                mat[e.x][e.y]=-2;
            }
        }

    }
    return -1;
}
int main()
{
    //freopen("hdu4038.txt","r",stdin);
    while(scanf("%d%d%d",&r,&c,&f)!=EOF)
    {
        init();
        ans=bfs();
        if(ans == -1)printf("Damn teoy!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

hdu 4308 Saving Princess claire_ BFS

时间: 2024-11-05 14:38:14

hdu 4308 Saving Princess claire_ BFS的相关文章

HDU 4308 Saving Princess claire_

BFS问题. 题意是问 王子救公主 需要花费多少钱.每路过一个 * 就要支付 cost 那么多的钱.求最短. 多个P 传送点就 多个点进队即可. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream&g

hdu 4308 Saving Princess claire(BFS)

Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2465    Accepted Submission(s): 877 Problem Description Princess claire_ was jailed in a maze by Grand Demon Monster(GDM)

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

2012 #1 Saving Princess claire_

Saving Princess claire_ Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4308 Description Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy. Out of anger, little Prince ykwd

HDU 4308 最短路或者bfs

Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2435    Accepted Submission(s): 864 Problem Description Princess claire_ was jailed in a maze by Grand Demon Monster(GDM)

hdu----(4308)Saving Princess claire_(搜索)

Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2354    Accepted Submission(s): 843 Problem Description Princess claire_ was jailed in a maze by Grand Demon Monster(GDM)

HDU 5025 Saving Tang Monk(BFS+状压)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel,

hdu 5025 Saving Tang Monk (bfs+状态压缩)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 41    Accepted Submission(s): 10 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi

hdu 5025 Saving Tang Monk(bfs)

题目链接 题意: 给出n*n的网格,有且只有一个K(孙悟空)和一个T(唐僧),最多有m把钥匙,最多5条蛇,每走一格的时间为1,走到蛇的格子(杀蛇时间为1)的时间为2, 取钥匙要按照顺序来,问能救到唐僧,如果可以输出最短时间. 分析: