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。。。不过最让我火大的是,现在学生会的人居然盛气临人地让我做事(虽然好像我去年也这么干过= =,惭愧)。以为自己当部长拿一等奖学金就了不起吗,非要找理由好像也说不大清楚,不过我死也不会输给这种人的!
#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。。。不过最让我火大的是,现在学生会的人居然盛气临人地让我做事(虽然好像我去年也这么干过= =,惭愧)。以为自己当部长拿一等奖学金就了不起吗,非要找理由好像也说不大清楚,不过我死也不会输给这种人的!
相关文章推荐
- iOS UIView非常用方法及属性详解
- UIColor,CGColor,CIColor三者的区别和联系
- Android UI组件拖拽效果时间基本原理
- Lire-0.9.4-beta1初始化DocumentBuilder出错
- gentoo制作ebuild文件安装atom编缉器
- Android UI之TextView
- js解析json,js转换json成map,获取map的key,value,jsonmap
- iOS开发UINavigation系列二——UINavigationItem
- iOS之表视图控制器UITableViewController
- 关于UISearchBar
- - 通过 UIBezierPath 做一个中空的扫描器
- Arduino控制L298N
- LinkedList 、LinkedMap 、Queue
- Uva1594——Ducci Sequence
- hdu5301Buildings
- LeetCode题解——Unique Binary Search Trees II
- UEFI下装Ubuntu Win10 双系统
- 【译】RabbitMQ:工作队列(Work Queue)
- IOS UIFont
- IOS开发UI篇--使用UICollectionView实现一个无限轮播的案例