魔兽系列题目第一弹
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;
}