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

hdu1242 Rescue

2015-11-08 08:44 507 查看
#include <iostream>

#include <queue>

#include <cstdio>

#include <algorithm>

#include <cstdlib>

#include <string.h>

using namespace std;

char map[205][205];

int visit[205][205];

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

int n, m;

struct node

{

int x, y;

int time;

friend bool operator < (const node &a, const node &b)

{

return a.time > b.time;

}

};

int go(int x, int y)

{

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

return 1;

return 0;

}

int BFS(int x, int y)

{

int i;

node st, ed;

priority_queue<node>que;

memset(visit, 0, sizeof(visit));

st.x = x;

st.y = y;

st.time = 0;

visit[st.x][st.y] = 1;

que.push(st);

while(!que.empty())

{

st = que.top();

que.pop();

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

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

{

ed.x = st.x + dir[i][0];

ed.y = st.y + dir[i][1];

if(go(ed.x, ed.y) && !visit[ed.x][ed.y])

{

visit[ed.x][ed.y] = 1;

if(map[ed.x][ed.y] == 'x')

ed.time = st.time + 2;

else ed.time = st.time + 1;

que.push(ed);

}

}

}

return -1;

}

int main()

{

// freopen("in.txt","r",stdin);

int x, y, i, j, ans;

while(~scanf("%d%d", &n, &m))

{

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

{

getchar();

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

{

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

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

{

x = i;

y = j;

}

}

}

ans = BFS(x, y);

if(ans == -1) printf("Poor ANGEL has to stay in the prison all his life.\n");

else printf("%d\n", ans);

}

return 0;

}

心得:有思路但是自己手打不出来T_T,看的题解http://blog.csdn.net/cambridgeacm/article/details/7725146,本题是bfs和优先队列的运用。前面重载<号的时候上下符号相反。还有第一次用freopen,真方便啊= =,不过提交时需要注释掉。还有这题是昨天落下的,最近花了大量时间干六级T_T。。。不过最让我火大的是,现在学生会的人居然盛气临人地让我做事(虽然好像我去年也这么干过= =,惭愧)。以为自己当部长拿一等奖学金就了不起吗,非要找理由好像也说不大清楚,不过我死也不会输给这种人的!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: