Question link http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1180
Find the shortest time from the start point to the end point. The question is not mentioned and there should be a shortest time.
The difference between general search and general search is the judgment of the stairs, which can be discussed in four cases.
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;int dir[][2] = {1,0,-1,0,0,1,0,-1};int row, col, startx, starty, endx, endy;char map[21][21];bool vis[21][21];struct Node{int x, y, step;friend bool operator < (const Node &a, const Node &b){return a.step > b.step;}};inline bool cango(int x, int y){if (x >= 0 && x < row && y >= 0 && y < col && !vis[x][y] && map[x][y] != '*')return true;return false;}int bfs(){memset(vis, 0, sizeof(vis));Node now, next;now.x = startx, now.y = starty, now.step = 0;vis[now.x][now.y] = 1;priority_queue<Node> q;q.push(now);while (!q.empty()){now = q.top();q.pop();if (now.x == endx && now.y == endy)return now.step;for (int i = 0; i < 4; ++i){next.x = now.x + dir[i][0], next.y = now.y + dir[i][1];bool isvertical = 0, iseven = 0;if (cango(next.x, next.y)){if (map[next.x][next.y] != '|' && map[next.x][next.y] != '-'){vis[next.x][next.y] = 1;next.step = now.step + 1;q.push(next);continue;}if (map[next.x][next.y] == '|')isvertical = 1;if (now.step % 2 == 0)iseven = 1;if (cango(next.x+dir[i][0], next.y+dir[i][1])){Node temp;temp.x = next.x + dir[i][0], temp.y = next.y + dir[i][1];vis[temp.x][temp.y] = 1;if (i == 0 || i == 1){if ((isvertical && iseven) || (!isvertical && !iseven))temp.step = now.step + 1;elsetemp.step = now.step + 2;}if (i == 2 || i == 3){if ((isvertical && !iseven) || (!isvertical && iseven))temp.step = now.step + 1;elsetemp.step = now.step + 2;}q.push(temp);}}}}}int main(){while (scanf("%d %d", &row, &col) != EOF){for (int i = 0; i < row; ++i){scanf("%s", map[i]);for (int j = 0; j < col; ++j){if (map[i][j] == 'S')startx = i, starty = j;else if (map[i][j] == 'T')endx = i, endy = j;}}printf("%d\n", bfs());}return 0;}