HDU2612 find a way
2016-01-29 21:01
316 查看
题目链接:Find a way
bfs水题。
View Code
bfs水题。
/* 只有两个人啊。分别以两个人为起点bfs,计算出每个人到每个KFC 的时间。两个人都能到达的KFC的较大时间的最小值、就是ans。好水。T_T */ #include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <queue> #define maxn 1000000 using namespace std; char mp[210][210]; int vis[210][210]; int step[210][210], step1[210][210], step2[210][210]; int n, m; struct Node{ int x, y; }now, temp, a, b; queue<Node>que; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; bool check(Node a) { if (a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && !vis[temp.x][temp.y] && mp[temp.x][temp.y] != '#') return true; return false; } void bfs1(Node t) { while(!que.empty()) { que.pop(); } memset(vis, 0, sizeof(vis)); que.push(t); vis[t.x][t.y] = 1; step1[t.x][t.y] = 0; while(!que.empty()) { now = que.front(); que.pop(); for (int i=0; i<4; ++i) { temp.x = now.x + dir[i][0]; temp.y = now.y + dir[i][1]; if (check(temp)) { vis[temp.x][temp.y] = 1; step1[temp.x][temp.y] = step1[now.x][now.y] + 1; que.push(temp); } } } } void bfs2(Node t) { while(!que.empty()) { que.pop(); } memset(vis, 0, sizeof(vis)); que.push(t); vis[t.x][t.y] = 1; step2[t.x][t.y] = 0; while(!que.empty()) { now = que.front(); que.pop(); for (int i=0; i<4; ++i) { temp.x = now.x + dir[i][0]; temp.y = now.y + dir[i][1]; if (check(temp)) { vis[temp.x][temp.y] = 1; step2[temp.x][temp.y] = step2[now.x][now.y] + 1; que.push(temp); } } } } int main() { while(cin >> n >> m) { memset(vis, 0, sizeof(vis)); for (int i=0; i<n; ++i) { for (int j=0; j<m; ++j) { step[i][j] = maxn; step1[i][j] = maxn; step2[i][j] = maxn; } } for (int i=0; i<n; ++i) { for (int j=0; j<m; ++j) { cin >> mp[i][j]; if (mp[i][j] == 'M') { a.x = i, a.y = j; } else if (mp[i][j] == 'Y') { b.x = i, b.y = j; } } } bfs1(a); bfs2(b); int ans = maxn; for (int i=0; i<n; ++i) { for (int j=0; j<m; ++j) { if (mp[i][j] == '@') { step[i][j] = step1[i][j] + step2[i][j]; } ans = min(ans, step[i][j]); } } cout << ans*11 << endl; } return 0; }
View Code
相关文章推荐
- Ubuntu静态网络配置
- 教研室 ftp 文件服务器使用教程
- iOS开发流程
- About memories in ASIC FPGA
- 前端工具--spritepad
- Android中实现静态的默认安装和卸载应用
- Asp.net Mvc Action重定向总结
- 20145327寒假第一周学习总结
- 前端笔记
- LEETCODE--Odd Even Linked List
- 机械学习-深度学习(ReinforcementLernen)
- Cocos2dx3.4 VS2013无法打开包括文件extensions/ExtensionExport.h解决的方法
- 单链表找中间数
- 字典树
- Python 8.3 ThreadLocal
- Oracle管理磁盘空间和资源
- LiveScript 字面量
- 【React Native开发】React Native控件之ScrollView组件讲解(14)
- HTML5学习笔记
- 递归算法