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. Session的应用——三天免登录

    1.使用Cookie实现的登录的不足: protected void doGet(HttpServletRequest request, HttpServletResponse response) t ...

  2. D. Vasya and Triangle(思维, 三角形)

    传送门 题意: 给你 n, m, k, 问你是否存在一个三角形, 满足三角形的面积等于 n * m / k: 若存在, 输出YES, 且输出满足条件的三角形的三个坐标(答案有多种,则输出任意一种)   ...

  3. 2017.10.6 国庆清北 D6T3 字符串

    题目描述 如果把一个字符串从头到尾翻转后和原字符串相等,我们称之为回文串,比如“aabaa”.“())(”.“2017102”. 如果一个字符串存在两个出现过的字母出现的次数相等,我们称之为好 的字符 ...

  4. 2440sd初始化(存储器控制器寄存器的设置)

    #define mem_contrl 0x48000000   //13个寄存器的基地址(看做一个内存块)init_sdram: ldr r0, =mem_contrl               / ...

  5. mysql设置主键自增长和自增长初始值

          本文主要向大家介绍MySQL数据库之Mysql创建表实现主键自增并且初始值为200,希望对大家学习MySQL数据库有所帮助.       假设已经创建表test,并且主键为id.Mysql ...

  6. 解决Spring Boot 拦截器注入service为空的问题

    问题:在自定义拦截器中,使用了@Autowaire注解注入了封装JPA方法的Service,结果发现无法注入,注入的service为空 0.原因分析 拦截器加载的时间点在springcontext之前 ...

  7. 主要排序算法(Python实现)

    1. 冒泡排序 算法描述:1. 比较相邻的两个数,对升序(/降序)而言,若当前数小于(大于)后一个数则交换两者的位置. 2.那么循环长度为L的列表,从第一个元素到倒数第(L-1)元素进行第1步操作,其 ...

  8. Spring boot Bean装配

    . Spring boot bean 默认创建的bean 为singleton模式 . @Component 注解 . @Value 为属性初始化 . @Value("${}") ...

  9. locust使用命令

    locust -f locust_demo.py --logfile=locusfile.log

  10. mysql查看数据库表数量

    1.查看数据库表数量SELECT count(TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_SCHEMA='dbname'; selec ...