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. git中常用命令

    1.全局安装git Git-2.11.1-64-bit() //配置gitgit config --global user.name "您的git账号名"git config -- ...

  2. Xshell5 安装JDK

    .执行命令yum -y list java*查看可安装java版本.执行成功后可以看见如下的结果 安装java-1.8全部相关 yum install -y java-1.8.0-openjdk* 使 ...

  3. Java 基础:Queue

    下面几个关于Java里queue的说法哪些是正确的()? 正确答案: A C A.LinkedBlockingQueue是一个可选有界队列,不允许null值 B.PriorityQueue,Linke ...

  4. 大数据|linux权限chmod和chown

    一.基础概念 1)rwx含义 示例如下 r:读权限read 4 w:写权限write 2 x:操作权限execute  1 -:无权限 2)drwxr - xr -x 与 - rw - r - - r ...

  5. Hutool工具类之HttpUtil使用Https

    关于Hutool工具类之HttpUtil如何使用可以参考官方文档Hutool之HttpUtil 其实使用Http和Https使用的方式是一样的. 建议大家可以看看HttpUtil的源码,感觉设计的挺不 ...

  6. html5的figure/figcaption讲解及实例

    html5的figure/figcaption讲解及实例 一.总结 一句话总结: figure元素:用来包着媒体资源,比如图片图表:是一个[媒体组合元素],也就是对其他的媒体元素进行组合,比如:图像. ...

  7. Jar hell问题以及解放方法

    当一个类或一个资源文件存在多个jar中,就好存在jar hell问题 可以通过以下代码来诊断问题:

  8. 使用Git GUI,上传项目到github,并实现预览功能

    一.使用GUI,上传项目到GitHub (GUI是啥,不做过多赘述,可百度了解) 步骤: 1.打开GUI,新建一个仓库,demo 2.在编辑器中,编写相关代码,比如添加1.html文件,文件内容为“h ...

  9. REDIS中加锁和解锁问题

    使用lua+redis的方法.之所以使用lua是为了保证原子性 问题: 1. redis发现锁失败了要怎么办?中断请求还是循环请求?2. 循环请求的话,如果有一个获取了锁,其它的在去获取锁的时候,是不 ...

  10. 【转】IDEA新建项目时,没有Spring Initializr选项(亲测有效)

    最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot.看着博客来进行操作上手spring boot,很多都是说创建一个新项目(Create New Project) 选择 ...