You are given a campus map with the Google buildings, roads and Google 
bikes. You have to help the employee find the nearest Google bike.

Campus map:

. - Free path/road
# - Building
B - Google bike Employee location - (x, y) - (1, 2) . . . . . #
. . E . . #
# # # # . #
. B . . . .
. . . . . B

https://www.careercup.com/question?id=5687196447670272

解法:BFS

Python:

from collections import deque

# This function returns the minimum cost
def bfs(googleMap, employeeLocation):
if not googleMap or not googleMap[0] or not employeeLocation:
return 0 minCost = 0
pathToBuilding = []
rows, cols = len(googleMap), len(googleMap[0])
# Perform a BFS here
startX, startY = employeeLocation
queue = deque([(startX, startY, 0, [])])
visited = set([(employeeLocation)]) while queue:
x, y, currCost, path = queue.popleft() if googleMap[x][y] == 'B': # Destination Reached
minCost = currCost
pathToBuilding = path
break for nextX, nextY, dir in [(x, y+1, 'R'), (x+1, y, 'D'), (x, y-1,'L'), (x-1, y, 'U')]:
if 0 <= nextX < rows and 0 <= nextY < cols \
and googleMap[nextX][nextY] != '#'\
and (nextX, nextY) not in visited: visited.add((nextX, nextY))
queue.append((nextX, nextY, currCost + 1, path + [dir])) return (minCost, pathToBuilding)

TestCase:

# Test Case 1
googleMap = [
['.', '.', '.', '.', '.', '#'],
['.', '.', 'E', '.', '.', '#'],
['#', '#', '#', '#', '.', '#'],
['.', 'B', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', 'B']
]
print(bfs(googleMap, (1, 2)))
# OUTPUTS: (6, ['R', 'R', 'D', 'D', 'R', 'D']) # Test Case 2
googleMap = [
['.', '.', '.', '.', '.', '#'],
['.', '.', 'E', '.', '.', '#'],
['#', '#', '#', '#', '.', '#'],
['B', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']
]
print(bfs(googleMap, (1, 2)))
# OUTPUTS: (8, ['R', 'R', 'D', 'D', 'L', 'L', 'L', 'L'])

  

  

[Google] Help employee find the nearest gbike的更多相关文章

  1. The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550

    Game Rooms Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  2. The 2015 China Collegiate Programming Contest Game Rooms

    Game Rooms Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. 从K近邻算法、距离度量谈到KD树、SIFT+BBF算法

    转载自:http://blog.csdn.net/v_july_v/article/details/8203674/ 从K近邻算法.距离度量谈到KD树.SIFT+BBF算法 前言 前两日,在微博上说: ...

  4. CDOJ 1225 Game Rooms

    Game Rooms Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Your ...

  5. ML二:NNSearch数据结构--二叉树

    wiki百科:http://zh.wikipedia.org/wiki/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AD%A6%E4%B9%A0 opencv学习笔记--二杈决策树: ...

  6. hdu5550 Game Rooms

    Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission ...

  7. Google Maps API V3 之绘图库 信息窗口

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  8. How Google TestsSoftware - Part Five

    Instead of distinguishingbetween code, integration and system testing, Google uses the language ofsm ...

  9. 美国政府关于Google公司2013年度的财务报表红头文件

    请管理员移至新闻版块,谢谢! 来源:http://www.sec.gov/ 财务报表下载↓ 此文仅作参考分析. 10-K 1 goog2013123110-k.htm FORM 10-K   UNIT ...

随机推荐

  1. 第二章--MYSQL体系结构和管理

    体系结构 MySQL C/S模型 Server : mysqld Client : socket:仅本地连接使用 tcp/ip:应用连接使用(远程和本地) #TCP/IP方式(远程.本地) mysql ...

  2. (尚017)Vue插件

    1.如何开发插件? 2.编写自己的vue-myPlugin.js插件库,代码如下: /** * vue的插件库 * 最好使用匿名函数包裹起来,这样代码会更加规范 * 里面的实现被隐藏了 */(func ...

  3. AtCoder Grand Contest 038 题解

    传送门 这场表现的宛如一个\(zz\) \(A\) 先直接把前\(b\)行全写成\(1\),再把前\(a\)列取反就行 const int N=1005; char mp[N][N];int n,m, ...

  4. 《挑战30天C++入门极限》C++中利用构造函数与无名对象简化运算符重载函数

        C++中利用构造函数与无名对象简化运算符重载函数 在完整描述思想之前,我们先看一下如下的例子,这个例子中的加运算符重载是以非成员函数的方式出现的: //程序作者:管宁  //站点:www.cn ...

  5. 常用命令备忘 xargs

    xargs 作为使用率很高的命令,但是长久不用就会模糊了记忆,所以要记录下来. 获取所有的cobbler相关的布尔值然后全部设置为真 getsebool -a|grep cobbler|awk '{p ...

  6. Mac OS 上安装 PostgreSQL

    PostgreSQL Database Download https://www.enterprisedb.com/downloads/postgres-postgresql-downloads 下载 ...

  7. nginx+keepalived高可用实战

    1.整体架构图如下 2.环境准备 今天所配置的是keepalived+nginx 的负载均衡 下载keepalived软件 [root@LB01 tools]# wget http://www.kee ...

  8. python定制后处理云图

    用后处理软件处理的云图会出现这样或那样的不满意,其实我们可以将求解数据导出以后,借助python定制云图. 我们以fluent为例 求解完成之后,我们将我们需要做云图的物理量以ASCII导出 如下的p ...

  9. 3、vueJs基础知识03

    vue过渡(动画) 本质走的css3: transtion ,animation <div id="div1" v-show="bSign" transi ...

  10. css,js 学习记录

    记录一些自己曾经阅读,值得收藏的网址 --(css3新特性) https://segmentfault.com/a/1190000010780991#articleHeader41 --CSS3 3D ...