hdu1026

来源:互联网 发布:幼儿网络教育前景 编辑:程序博客网 时间:2024/05/16 23:45

1:printf()在输出混合类型的方便。

2:bfs入队条件。

3:求路径,倒搜。

#include <iostream>using namespace std;const int mint = 0x3f3f3f3f;const int ma=100;struct no{int x, y, t, p;};no z, q[300000];char map[ma][ma];int mt[ma][ma];int n, m, qs, qe;short move[4][2] = {-1,0,0,1,1,0,0,-1};void bfs(){while (qs!=qe){no h = q[qs++];for (int i=0;i<=3;i++){int x = h.x + move[i][0];int y = h.y + move[i][1];if (x>=0 && y>=0 && x<=n && y<=m && map[x][y]!='X'){z.t = h.t + 1;if (map[x][y]!='.')z.t += map[x][y] - '0';if (z.t<mt[x][y]){mt[x][y] = z.t;z.x = x; z.y =y;z.p = qs - 1;q[qe++] = z;}}}}}int main(){int i, j;while (scanf("%d%d",&n,&m)!=EOF){for (i=0;i<n;i++)for (j=0;j<m;j++){scanf(" %c", &map[i][j]);}memset(mt, mint, sizeof(mt));n--, m--;q[0].x = n; q[0].y = m;q[0].t = 0; q[0].p = -1;if (map[n][m]!='X' && map[n][m]!='.')  mt[n][m] = q[0].t = map[n][m] - '0';qs = 0; qe = 1;bfs();if (mt[0][0] == mint)  cout<<"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", mt[0][0]);z = q[qs-1];while (z.x!=0||z.y!=0)z = q[--qs];int p, t = 1, x, y;while (z.p>=0){p = z.p;x = q[p].x, y = q[p].y;printf("%ds:(%d,%d)->(%d,%d)\n",t++,z.x,z.y,x,y);if (map[x][y]!='X' && map[x][y]!='.')for (int i=1;i<=map[x][y]-'0';i++)printf("%ds:FIGHT AT (%d,%d)\n",t++,x,y);z = q[p];}}cout<<"FINISH\n";}return 0;}