ZOJ 3291 Never End

来源:互联网 发布:北京握奇数据 编辑:程序博客网 时间:2024/05/20 16:40

题目:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3291

代码:

#include<stdio.h>#include<queue>#include<string.h>using namespace std;const int maxn = 500 + 10;int N, M, ans, flag_ans, ex, ey;int map[maxn][maxn], dis[maxn][maxn][4], visit[maxn][maxn][4];struct man{int x, y;int dir;};int dir[4][3][2] ={1, 0, -1, 0, 0, 1,0, -1, 0, 1, 1, 0,-1, 0, 1, 0, 0, -1,0, 1, 0, -1, -1, 0};int change[4][2] ={1, 3,2, 0,3, 1,0, 2};struct man down(struct man q_ru){int tx, ty;tx = q_ru.x;ty = q_ru.y;struct man ret;while (map[tx][ty] != '#'&&map[tx][ty] != 'E'){tx = tx + dir[q_ru.dir][2][0];ty = ty + dir[q_ru.dir][2][1];}if (map[tx][ty] == 'E'){ret.x = tx;ret.y = ty;ret.dir = q_ru.dir;return ret;}if (map[tx][ty] == '#'){tx = tx - dir[q_ru.dir][2][0];ty = ty - dir[q_ru.dir][2][1];ret.x = tx; ret.y = ty;ret.dir = q_ru.dir;return ret;}}int get_e(int x, int y){if (x == ex&&y == ey){return 1;}else return 0;}struct man init(int sx, int sy){struct man q_ru;q_ru.dir = 1;q_ru.x = sx; q_ru.y = sy;flag_ans = 0;memset(visit, 0, sizeof(visit));memset(dis, 0, sizeof(dis));visit[sx][sy][1] = 1;return q_ru;}struct man move_to(struct man q_chu, int i){struct man ret;if (i == 0){//往左走一步ret.x = q_chu.x + dir[q_chu.dir][0][0];ret.y = q_chu.y + dir[q_chu.dir][0][1];ret.dir = q_chu.dir;}if (i == 1){ret.x = q_chu.x + dir[q_chu.dir][1][0];ret.y = q_chu.y + dir[q_chu.dir][1][1];ret.dir = q_chu.dir;}if (i == 1 || i == 0){if (map[ret.x][ret.y] == '#'){return q_chu;}ret = down(ret);return ret;}if (i == 2){//往左走到尽头int tx, ty;tx = q_chu.x + dir[q_chu.dir][0][0];ty = q_chu.y + dir[q_chu.dir][0][1];while (map[tx][ty] != '#'&&map[tx][ty] != 'E'){tx = tx + dir[q_chu.dir][0][0];ty = ty + dir[q_chu.dir][0][1];}if (get_e(tx, ty)){ret.x = tx; ret.y = ty;return ret;}tx = tx - dir[q_chu.dir][0][0];ty = ty - dir[q_chu.dir][0][1];ret.x = tx; ret.y = ty;ret.dir = change[q_chu.dir][0];return ret;}if (i == 3){int tx, ty;tx = q_chu.x + dir[q_chu.dir][1][0];ty = q_chu.y + dir[q_chu.dir][1][1];while (map[tx][ty] != '#'&&map[tx][ty] != 'E'){tx = tx + dir[q_chu.dir][1][0];ty = ty + dir[q_chu.dir][1][1];}if (get_e(tx, ty)){ret.x = tx; ret.y = ty;return ret;}tx = tx - dir[q_chu.dir][1][0];ty = ty - dir[q_chu.dir][1][1];ret.x = tx; ret.y = ty;ret.dir = change[q_chu.dir][1];return ret;}}void BFS(int sx, int sy){queue<struct man>q;struct man q_ru;/*初始化:q_ru,ans,ans_flag,dis,visit*/q_ru = init(sx, sy);q.push(q_ru);while (!q.empty()){struct man q_chu;q_chu = q.front();q.pop();int i;//test//printf("test:%d %d %d\n", q_chu.x, q_chu.y, q_chu.dir);for (i = 0; i < 4; i++){/*1.临墙,返回q_chu,2.到达则返回终点3.走一步,会掉落则返回掉落后的点*/q_ru = move_to(q_chu, i);if (!visit[q_ru.x][q_ru.y][q_ru.dir]){if (get_e(q_ru.x, q_ru.y)){flag_ans = 1;ans = dis[q_chu.x][q_chu.y][q_chu.dir] + 1;return;}visit[q_ru.x][q_ru.y][q_ru.dir] = 1;dis[q_ru.x][q_ru.y][q_ru.dir] = dis[q_chu.x][q_chu.y][q_chu.dir] + 1;q.push(q_ru);}}}}int main(){while (~scanf("%d%d", &N, &M)){int i, j;int sx, sy;//inputfor (i = 0; i < N; i++){getchar();for (j = 0; j < M; j++){scanf("%c", &map[i][j]);if (map[i][j] == '|'){sx = i; sy = j;}if (map[i][j] == 'E'){ex = i; ey = j;}}}BFS(sx, sy);if (flag_ans == 0) printf("Can not escape!\n");else printf("%d\n", ans);}return 0;}



0 0