1. 程式人生 > >【BFS】CODE[VS] 2059 逃出克隆島(奴隸島)

【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; }