HDU 2612.Find a way
2017-12-21 13:01
288 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2612
AC代码(C++):
总结: 广搜, 先搜出Y和M到所有点的距离, 然后比较哪个kfc的距离最小.
AC代码(C++):
#include <iostream> #include <queue> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; struct NODE { short x, y, step; }; int n, m; char map[205][205]; bool vis[205][205]; int dist1[205][205]; int dist2[205][205]; int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} }; void bfs(NODE s, int dist[][205]) { queue<NODE> q; q.push(s); NODE node, tmp; while (!q.empty()) { node = q.front(); q.pop(); for (int i = 0; i < 4; i++) { tmp.x = node.x + dir[i][0]; tmp.y = node.y + dir[i][1]; tmp.step = node.step + 1; if (map[tmp.x][tmp.y] != '#'&&!vis[tmp.x][tmp.y]) { vis[tmp.x][tmp.y] = true; dist[tmp.x][tmp.y] = tmp.step; q.push(tmp); } } } } int main() { while (cin >> n >> m) { NODE s1, s2; s1.step = 0; s2.step = 0; for (int i = 0; i <= n + 1; i++)for (int j = 0; j <= m + 1; j++)map[i][j] = '#'; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> map[i][j]; if (map[i][j] == 'Y') { s1.x = i; s1.y = j; } else if (map[i][j] == 'M') { s2.x = i; s2.y = j; } } } memset(vis, false, sizeof(vis)); vis[s1.x][s1.y] = true; bfs(s1, dist1); memset(vis, false, sizeof(vis)); vis[s2.x][s2.y] = true; bfs(s2, dist2); int MIN = INF; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (map[i][j] == '@')MIN = min(MIN, dist1[i][j] + dist2[i][j]); } } cout << MIN * 11 << endl; } //system("pause"); }
总结: 广搜, 先搜出Y和M到所有点的距离, 然后比较哪个kfc的距离最小.
相关文章推荐
- HDU 2612 Find a way【双重BFS】
- BFS(最短路) HDU 2612 Find a way
- [kuangbin带你飞]专题1 简单搜索 N - Find a way HDU - 2612
- HDU 2612 Find a way
- HDU 2612 Find a way bfs 难度:1
- Find a way(HDU-2612)
- HDU 2612 Find a way
- HDU 2612 Find a way(bfs)
- Find a way HDU - 2612
- HDU 2612 Find a way
- [HDU-2612] Find a Way
- hdu-2612-Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way (广搜)
- hdu 2612 find a way
- HDU 2612 Find a way 多起点 多终点 BFS
- HDU 2612 Find a way(BFS)
- hdu 2612 Find a way(双向bfs)
- hdu 2612 Find a way(BFS + 最短路)
- 【HDU 2612】Find a way(BFS)