【BFS】CODE[VS] 2059 逃出克隆岛(奴隶岛)

点击进入冲绳奴隶岛


魔兽系列题目第一弹

BFS典型例题,遇到传送门只走一次且将走每一个传送门的情况都搜一下,碰到终点直接退出输出结果即可

PS:最近超喜欢压行!我也不知道为什么

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>

const int maxn = 5010;

using namespace std;

int n,m,cc;
int tot; 
int sx,sy,ex,ey;
bool used[maxn][maxn];
char map[maxn][maxn];
int dx[5] = {0,0,1,0,-1};
int dy[5] = {0,1,0,-1,0};

struct node{
    int x,y,cost;
};

struct data{
    int x,y; 
}tp[maxn]; 

queue<node >q; 

inline bool check(int xx,int yy)
{
    if(xx <= 0||xx > n||yy <= 0||yy > m||used[xx][yy] == 1||map[xx][yy] == '#')return false;
    return true; 
}

inline void movein(node now)
{
    node ttt;
    for(int i = 1;i <= tot;i++)
    {
        if(tp[i].x != now.x||tp[i].y != now.y)
        {
            ttt.x = tp[i].x;
            ttt.y = tp[i].y;
            ttt.cost = now.cost;
            q.push(ttt);
            map[ttt.x][ttt.y] = '#';
        }
    }
} 

inline void bfs()
{
    node start;
    start.x = sx;
    start.y = sy;
    start.cost = 0;
    used[sx][sy] = 1;
    q.push(start);
    while(!q.empty())
    {
        node head = q.front();
        q.pop();
        for(int i = 1;i <= 4;i++)
        {
            node now;
            now.x = head.x + dx[i];
            now.y = head.y + dy[i];
            now.cost = head.cost;
            if(map[now.x][now.y] == 'C'){printf("%d\n",now.cost); return;}
            if(check(now.x,now.y) == true)
            {
                used[now.x][now.y] = 1;
                //printf("%d\n",now.cost);
                //cout<<head.cost<<endl;
                //cout<<map[now.x][now.y]<<endl;
                if(map[now.x][now.y] == '*'){now.cost = head.cost + cc; q.push(now); continue;}
                //cout<<now.x<<" "<<now.y<<" "<<now.cost<<endl; 
                if(map[now.x][now.y] == 'P'){movein(now); continue;}    
            }
        }
    }
printf("screw you!\n");
return;
} 

int main()
{
    scanf("%d%d%d",&n,&m,&cc);
    //cout<<cc<<endl;
    for(int i = 1; i <= n;i++)
        for(int j = 1;j <= m;j++)
        {
            cin>>map[i][j];
            if(map[i][j] == 'Y') {sx = i; sy = j;}  
            if(map[i][j] == 'C') {ex = i; ey = j;}
            if(map[i][j] == 'P') {tot++;tp[tot].x = i;tp[tot].y = j;}
        }
        bfs();
return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值