hdu 4308 Saving Princess claire_ (多校联合1)

//bfs或者最短路

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int move[4][2]={1,0,-1,0,0,1,0,-1};
int r, c, cost, num_p, flag, ans;
char maze[5005][5005];
bool visited[5005][5005];
int step[5005][5005];
struct node{int x, y;}a[5005], st, end;
void init(){
    int i, j;
    num_p=0;flag=0;
    memset(visited, 0 ,sizeof(visited));
    for(i=0; i<r; i++)
    scanf("%s", maze[i]);
    for(i=0; i<r; i++)
    for(j=0; j<c; j++){
        if(maze[i][j]=='P'){
            a[num_p].x=i;
            a[num_p++].y=j;
        }
        else if(maze[i][j]=='Y'){
            st.x=i;
            st.y=j;
        }
        else if(maze[i][j]=='C'){
            end.x=i;
            end.y=j;
        }
    }
}

bool check(node p){
    if(p.x<0||p.y<0||p.x>=r||p.y>=c||visited[p.x][p.y])
    return false;
    return true;
}
void bfs(){
    int i, j, k, flag_p=0;
    step[st.x][st.y]=0;
    visited[st.x][st.y]=true;
    node head, next;
    queue<node> q;
    q.push(st);
    while(!q.empty()){
        head=q.front();
        q.pop();
        for(i=0; i<4; i++){
            next.x=head.x+move[i][0];
            next.y=head.y+move[i][1];
            if(!check(next))continue;
            if(maze[next.x][next.y]=='C'){
                flag=1;ans=step[head.x][head.y];return;
            }
            else if(maze[next.x][next.y]=='#'){
                visited[next.x][next.y]=1;
                continue;
            }
            else if(maze[next.x][next.y]=='*'){
                visited[next.x][next.y]=1;
                step[next.x][next.y]=step[head.x][head.y]+1;
                q.push(next);
            }
            else if(!flag_p){
                flag_p=1;
                for(j=0; j<num_p; j++){
                    visited[a[j].x][a[j].y]=1;
                    for(k=0; k<4; k++){
                        next.x=a[j].x+move[k][0];
                        next.y=a[j].y+move[k][1];
                        if(!check(next))continue;
                        if(maze[next.x][next.y]=='C'){
                            flag=1;ans=step[head.x][head.y];return;
                        }
                        else if(maze[next.x][next.y]=='#'){
                            visited[next.x][next.y]=1;
                            continue;
                        }
                        else if(maze[next.x][next.y]=='*'){
                            visited[next.x][next.y]=1;
                            step[next.x][next.y]=step[head.x][head.y]+1;
                            q.push(next);
                        }
                    }
                }
            }
        }
    }

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值