查看: 1650| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

请教一道面经

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
请教一道题,是面经里的。顺便,感觉这种给定一个board和特定游戏规则的题目有很多,也很常见,都比较有固定套路,大多都是bfs/dfs/backtracking做,然而我每次遇到的时候思路还是比较乱,不知道哪里有这类问题的总结之类的呢?非常感谢!


You’re given a board game which is a row of squares, each labeled with an integer. This can be represented by a list, e.g. [1, 3, 2, 0, 5, 2, 8, 4, 1] Given a start position on the board, you “win” by landing on a zero, where you move by jumping from square to square either left or right the number of spaces specified on the square you’re currently on. Your task is to implement the function: def can_win(board, pos): returns True if you can win the board from that starting pos, False otherwise  





上一篇:找出无向图汇总的相连要素 求助
下一篇:用Python刷题会不会被鄙视?
🔗
dwl1222 2015-8-26 02:50:13 | 只看该作者
全局:
dfs把。
用一个visited array记录访问过的格子。
先check pos是不是out of bound
然后 check pos 是不是visited过。
然后 check array[pos]是不是等于0
然后 如果visited[pos] == 0 {
visted[pos] = 1      
res = dfs(array, pos - array[pos]) || dfs(array, pos + array[pos])
visited[pos] = 0

}

return res;
回复

使用道具 举报

🔗
dwl1222 2015-8-26 02:50:51 | 只看该作者
全局:
dfs把。
用一个visited array记录访问过的格子。
先check pos是不是out of bound
然后 check pos 是不是visited过。
然后 check array[pos]是不是等于0
然后 如果visited[pos] == 0 {
visted[pos] = 1      
res = dfs(array, pos - array[pos]) || dfs(array, pos + array[pos])
visited[pos] = 0

}

return res;
回复

使用道具 举报

🔗
 楼主| flamen 2015-8-26 10:24:37 | 只看该作者
全局:
dwl1222 发表于 2015-8-25 10:50
dfs把。
用一个visited array记录访问过的格子。
先check pos是不是out of bound

谢谢!我是用bfs做的,感觉你的方法用dfs做更好 :)
回复

使用道具 举报

🔗
dwl1222 2015-8-29 06:02:25 | 只看该作者
全局:
flamen 发表于 2015-8-26 10:24
谢谢!我是用bfs做的,感觉你的方法用dfs做更好 :)

不用客气
回复

使用道具 举报

🔗
mlfma 2015-10-22 12:54:34 | 只看该作者
全局:
bfs 和 dfs 做结果应该是一样的吧?DFS 有什么优势?
回复

使用道具 举报

🔗
lrn0304 2015-10-29 06:48:33 | 只看该作者
全局:
完全看不懂题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表