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

hdu 1242 Rescue

2016-07-20 15:43 549 查看
#include<stdio.h>

#include<queue>

#include<string.h>

using namespace std;

int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

char map[210][210];

struct node

{

int x, y, time;

friend bool operator < (node a, node b)

{

return a.time > b.time;

}

};

int bfs()

{

node now, next;

priority_queue<node>q;

while(!q.empty())

q.pop();

now.x = sx;

now.y = sy;

now.time = 0;

q.push(now);

judge[now.x][now.y] = 1;

while(!q.empty())

{

now = q.top();

q.pop();

if(map[now.x][now.y] == 'r') return now.time;

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

{

next.x = now.x + d[i][0];

next.y = now.y + d[i][1];

if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')

{

if(map[next.x][next.y] == 'x') next.time = now.time + 2;

else

next.time = now.time + 1;

judge[next.x][next.y] = 1;

q.push(next);

}

}

}

return -1;

}

int main()

{

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

{

if(n==0&&m==0) break;

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

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

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

{

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

{

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

{

sx = i;

sy = j;

}

}

}


memset(judge, 0, sizeof(judge));

int f = bfs();

if(f == -1)

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

else

printf("%d\n", f);

}

return 0;

}

区别

#include<stdio.h>

#include<queue>

#include<string.h>

using namespace std;

int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

char map[210][210];

struct node

{

int x, y, time;

friend bool operator < (node a, node b)

{

return a.time > b.time;

}

};

int bfs()

{

node now, next;

priority_queue<node>q;

while(!q.empty())

q.pop();

now.x = sx;

now.y = sy;

now.time = 0;

q.push(now);

judge[now.x][now.y] = 1;

while(!q.empty())

{

now = q.top();

q.pop();

if(map[now.x][now.y] == 'r') return now.time;

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

{

next.x = now.x + d[i][0];

next.y = now.y + d[i][1];

if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')

{

if(map[next.x][next.y] == 'x') next.time = now.time + 2;

else

next.time = now.time + 1;

judge[next.x][next.y] = 1;

q.push(next);

}

}

}

return -1;

}

int main()

{

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

{

if(n==0&&m==0) break;

getchar();

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

{

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

{

scanf("%c", &map[i][j]);

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

{

sx = i;

sy = j;

}

}

getchar();

}


memset(judge, 0, sizeof(judge));

int f = bfs();

if(f == -1)

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

else

printf("%d\n", f);

}

return 0;

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