hdu1026 Ignatius and the Princess I
2015-11-08 21:08
351 查看
#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string.h>
#include <ctype.h>
using namespace std;
int visit[205][205], fight[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;
}
};
struct cmap
{
char c;
int nx, ny;
}map[205][205];
int go(int x, int y)
{
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y].c != 'X')
return 1;
return 0;
}
int BFS(int x, int y)
{
int i;
node st, ed;
priority_queue<node>que;
while(!que.empty()) que.pop();
st.x = x;
st.y = y;
st.time = 0;
if(isdigit(map[st.x][st.y].c))
{
st.time = map[st.x][st.y].c - '0';
fight[st.x][st.y] = map[st.x][st.y].c - '0';
}
else st.time = 0;
visit[st.x][st.y] = 1;
que.push(st);
while(!que.empty())
{
st = que.top();
que.pop();
if(st.x == 0 && st.y == 0) return st.time;
// printf("fuck\n");
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(isdigit(map[ed.x][ed.y].c))
{
ed.time = st.time + (map[ed.x][ed.y].c - '0') + 1;
fight[ed.x][ed.y] = map[ed.x][ed.y].c - '0';
}
else ed.time = st.time + 1;
map[ed.x][ed.y].nx = st.x;
map[ed.x][ed.y].ny = st.y;
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].c);
}
}
memset(visit,0,sizeof(visit));
memset(fight,0,sizeof(fight));
x = n - 1; y = m - 1;
ans = BFS(x, y);
if(ans == -1) printf("God please help our poor hero.\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
int cnt = 1, x0 = 0, y0 = 0, fx, fy;
while(cnt <= ans)
{
fx = map[x0][y0].nx;
fy = map[x0][y0].ny;
printf("%ds:(%d,%d)->(%d,%d)\n", cnt++, x0, y0, fx, fy);
for(int k = 1; k <= fight[fx][fy]; k++)
printf("%ds:FIGHT AT (%d,%d)\n", cnt++, fx, fy);
x0 = fx;
y0 = fy;
}
}
printf("FINISH\n");
}
return 0;
}
心得:还是看着题解写的T T,http://www.cnblogs.com/ACMan/archive/2012/05/21/2512295.html,有用到路径表示,为了方便要在造一个结构体。此外上面注意点的pop必须加,虽然我也不知道为什么。。。学习了。。。
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string.h>
#include <ctype.h>
using namespace std;
int visit[205][205], fight[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;
}
};
struct cmap
{
char c;
int nx, ny;
}map[205][205];
int go(int x, int y)
{
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y].c != 'X')
return 1;
return 0;
}
int BFS(int x, int y)
{
int i;
node st, ed;
priority_queue<node>que;
while(!que.empty()) que.pop();
st.x = x;
st.y = y;
st.time = 0;
if(isdigit(map[st.x][st.y].c))
{
st.time = map[st.x][st.y].c - '0';
fight[st.x][st.y] = map[st.x][st.y].c - '0';
}
else st.time = 0;
visit[st.x][st.y] = 1;
que.push(st);
while(!que.empty())
{
st = que.top();
que.pop();
if(st.x == 0 && st.y == 0) return st.time;
// printf("fuck\n");
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(isdigit(map[ed.x][ed.y].c))
{
ed.time = st.time + (map[ed.x][ed.y].c - '0') + 1;
fight[ed.x][ed.y] = map[ed.x][ed.y].c - '0';
}
else ed.time = st.time + 1;
map[ed.x][ed.y].nx = st.x;
map[ed.x][ed.y].ny = st.y;
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].c);
}
}
memset(visit,0,sizeof(visit));
memset(fight,0,sizeof(fight));
x = n - 1; y = m - 1;
ans = BFS(x, y);
if(ans == -1) printf("God please help our poor hero.\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
int cnt = 1, x0 = 0, y0 = 0, fx, fy;
while(cnt <= ans)
{
fx = map[x0][y0].nx;
fy = map[x0][y0].ny;
printf("%ds:(%d,%d)->(%d,%d)\n", cnt++, x0, y0, fx, fy);
for(int k = 1; k <= fight[fx][fy]; k++)
printf("%ds:FIGHT AT (%d,%d)\n", cnt++, fx, fy);
x0 = fx;
y0 = fy;
}
}
printf("FINISH\n");
}
return 0;
}
心得:还是看着题解写的T T,http://www.cnblogs.com/ACMan/archive/2012/05/21/2512295.html,有用到路径表示,为了方便要在造一个结构体。此外上面注意点的pop必须加,虽然我也不知道为什么。。。学习了。。。
相关文章推荐
- 文件操作
- C语言实现链表之单向链表(五)头结点前插入结点
- Android 系统信息获取(CPU,RAM,ROM,Battery,SD-card,版本等)
- LESS 原理及使用方式
- C语言实现链表之单向链表(四)清空链表
- NOIP2015酱油记
- POJ3292Semi-prime H-numbers(筛选法)
- array [].Getlength与array[].length
- HDU 4460 SPFA
- Linux /Dev 常见特殊设备介绍与应用
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- 多核mips异常分析(1)
- UINavigationController 总结
- zoj3652Maze(广搜+优先队列+移位+与,或运算)
- HDU 4462 枚举
- 【转】ubuntu的防火墙安装、开启和关闭
- svn不支持中文路径问题的解决
- C语言实现链表之单向链表(三)创建链表
- Jetbrains的产品对在校学生免费
- 编程之美-一摞烙饼的排序方法整理