您的位置:首页 > 其它

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必须加,虽然我也不知道为什么。。。学习了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: