您的位置:首页 > 产品设计 > UI/UE

hdu1242 rescue

2011-04-30 20:50 309 查看
同诡异的楼梯,也是优先队列,注意这里从天使姐姐开始搜起

#include <iostream>

#include <cstring>

#include <cstdio>

#include <queue>

using namespace std;

struct Node

{

int x, y;

int step;

bool operator <(const Node t)const{

return step > t.step;

}

};



const int Go[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};



char map[210][210];



int n, m;

Node start;

int ans;

int check(int x, int y)

{

return (x>=0 && x<n && y>=0 &&y < m);

}

int visited[210][210];

void bfs()

{

priority_queue <Node>Que;

//while (!Que.empty())Que.pop();

Que.push(start);

memset(visited, 0, sizeof(visited));

visited[start.x][start.y] = 1;

//Node pre, next;

while (!Que.empty()){

Node pre = Que.top();

Que.pop();

for (int i = 0; i < 4; i ++){

int xx = pre.x+Go[i][0];

int yy = pre.y+Go[i][1];//cout<<"asdasd"<<endl;

if (check(xx, yy)&&map[xx][yy] != '#'&&!visited[xx][yy]){

visited[xx][yy] = 1;

if (map[xx][yy]=='r'){

ans = pre.step+1;

return ;

}

if (map[xx][yy]=='x'){

Node next;

next.x = xx, next.y = yy;

next.step = pre.step+2;

Que.push(next);

}

else {

Node next;

next.x = xx, next.y = yy;

next.step = pre.step+1;

Que.push(next);

}

//visited[next.x][next.y] = 1;

}

}

}

}

int main()

{



while (scanf("%d %d", &n, &m)!=EOF ){

for (int i = 0; i < n; i ++){

getchar();

scanf("%s", map[i]);

//cin>>map[i];

for (int j = 0; j < m; j ++){

//cin >> map[i][j];

if (map[i][j] == 'a'){

start.x = i;

start.y = j;

}

}

}

start.step = 0;

ans = -1;

bfs();

if (ans != -1){

printf("%d/n", ans);

}

else printf("Poor ANGEL has to stay in the prison all his life./n");

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: