HDU 1026(bfs)
来源:互联网 发布:电视软件破解论坛 编辑:程序博客网 时间:2024/05/17 03:31
题意:如题。
#include <iostream>#include <queue>#include <cstring>#include <cstdio>#include <stack>using namespace std;int N, M;const int step[4][3] = { {0, 1, 0}, {1, -1, 0}, {2, 0, 1}, {3, 0, -1} };int time[101][101];int LD[101][101]; // last direction 上一个方向char maze[102][102];struct Point{ int x, y, s, ld; Point() : s(0), ld(-1) {} Point(int _x, int _y, int _s, int _ld) : x(_x), y(_y), s(_s), ld(_ld) {} bool operator <(const Point& rhs) const { return s > rhs.s; } bool operator ==(const Point& rhs) const { return x == rhs.x && y == rhs.y; } Point operator +(const int S[]) const { return Point(x + S[1], y + S[2], s + 1, S[0]); } Point operator -(const int S[]) const { return Point(x - S[1], y - S[2], 0, -1); } bool legal() { if (maze[x][y] == 'X') return false; else { if (maze[x][y] != '.') s += maze[x][y] - '0'; if (time[x][y] == 0 || time[x][y] > s) { time[x][y] = s; LD[x][y] = ld; return true; } else return false; } } friend ostream& operator <<(ostream& os, const Point& rhs) { return os << "(" << rhs.x-1 << "," << rhs.y-1 << ")"; }} start(1, 1, 0, -1), end;void backtrack(Point& p){ if (p == start) return; Point n = p - step[LD[p.x][p.y]]; backtrack(n); int t = 0; cout << (time[p.x][p.y] - t) << "s:" << n << "->" << p << endl; if (maze[p.x][p.y] != '.') { t = maze[p.x][p.y] - '0'; while (t--) cout << (time[p.x][p.y] - t) << "s:FIGHT AT " << p << endl; }}void backtrack(){ stack<Point> s; Point p = end, first, second; s.push(p); int t; while (!(p == start)) { p = p - step[LD[p.x][p.y]]; s.push(p); } first = s.top(), s.pop(); while (!s.empty()) { second = s.top(), s.pop(); if (maze[second.x][second.y] != '.') t = maze[second.x][second.y] - '0'; else t = 0; //printf("%ds:(%d,%d)->(%d,%d)\n", time[second.x][second.y] - t, first.x-1, first.y-1, second.x-1, second.y-1); cout << (time[second.x][second.y] - t) << "s:" << first << "->" << second << endl; while (t--) //printf("%ds:(%d,%d)->(%d,%d)\n", time[second.x][second.y] - t, first.x-1, first.y-1, second.x-1, second.y-1); cout << (time[second.x][second.y] - t) << "s:FIGHT AT " << second << endl; first = second; }}void BFS(){ time[1][1] = -1; priority_queue<Point> q; q.push(start); Point p, n; while (!q.empty()) { p = q.top(), q.pop(); for (int i = 0; i < 4; ++i) { n = p + step[i]; if (!n.legal()) continue; if (n == end) { while (!q.empty()) q.pop(); //printf("It takes %d seconds to reach the target position, let me show you the way.\n", n.s); cout << "It takes " << n.s << " seconds to reach the target position, let me show you the way." << endl; backtrack(); //backtrack(n); return; } q.push(n); } } cout << "God please help our poor hero." << endl; //printf("God please help our poor hero.\n");}int main(){ memset(maze[0], 'X', sizeof (maze[0])); while (scanf("%d%d", &N, &M) != EOF) { end.x = N, end.y = M; memset(time, 0, sizeof (time)); memset(LD, -1, sizeof (LD)); getchar(); for (int i = 1; i <= N; ++i) { gets(&maze[i][1]); maze[i][0] = maze[i][M+1] = 'X'; } memset(maze[N+1], 'X', sizeof (maze[N+1])); BFS(); cout << "FINISH" << endl; //printf("FINISH\n"); }}
0 0
- hdu 1026(BFS)
- bfs hdu 1026
- hdu 1026 BFS
- HDU 1026(bfs)
- HDU 1026 BFS
- hdu 1026 BFS 记录路径
- BFS hdu 1026 输出路径
- hdu 1026 bfs+优先级队列
- hdu 1026 BFS+优先队列
- HDU bfs
- HDU 1026 (BFS + 优先队列)
- hdu 1026(优先队列+BFS)
- hdu 1026 (优先队列+bfs)
- hdu 1026 反向 bfs() + 前驱保存
- HDU 1026 (BFS+记录路径)
- HDU 1026 (BFS+优先队列)
- hdu 1026 简单的bfs
- HDU 1026(优先队列+BFS+前驱记录)
- 武汉信运通信息产业公司又开始招人了,亲你中招了么,亲你不会像我一样又被忽悠了么?
- 单片机的硬件结构
- C# DataGridView控件清空数据完美解决方法
- svn 分支的创建及合并
- mac 下 sublime text 2 主要快捷键列表
- HDU 1026(bfs)
- 转载:GreenPlum的并行查询优化策略
- CSS禁止选择文本功能(兼容IE,火狐等浏览器)
- git学习笔记
- poj2486解题报告
- HDU 1032(水题)
- USB Phy/ULPI (2-读写USB Phy寄存器)
- 随便写了个堆排序程序
- iOS 第三方控件GIFView的学习记录