# 百度2016/2017秋招部分题目解析

## 格子距离

<题目来源: 百度2017秋招 原题链接-可在线提交(赛码网)>

#### 问题描述

_ 123456
1 ######
2 ######
3 ######
4 ######

dirs = [[-1, 0, 0], [1, 0, 1], [0, -1, 2], [0, 1, 3]]

``````const_x = 0
const_y = 1
const_wall_bit = 2
dirs = [[-1, 0, 0], [1, 0, 1], [0, -1, 2], [0, 1, 3]]

def bfs(map_info, dist, n, m, s, t):
que = [(s, t)]
dist[s][t] = 0

while len(que):
cur_x, cur_y = que.pop(0)
for d in dirs:
if 1 <= cur_x + d[const_x] <= n and 1 <= cur_y + d[const_y] <= m and \
not map_info[cur_x][cur_y] & (1 << d[const_wall_bit]) and \
dist[cur_x + d[const_x]][cur_y + d[const_y]] == -1:
dist[cur_x + d[const_x]][cur_y + d[const_y]] = dist[cur_x][cur_y] + 1
que.append((cur_x + d[const_x], cur_y + d[const_y]))

def main():
t_cases = int(raw_input())

for t_case in range(1, t_cases + 1):
temp = raw_input().split(' ')
n, m, s, t = int(temp[0]), int(temp[1]), int(temp[2]), int(temp[3])
map_info = [[0 for i in range(m + 2)] for i in range(n + 2)]

for i in range(1, n + 1):
line = raw_input().split(' ')
for j in range(1, m + 1):
map_info[i][j] = int(line[j - 1])

dist = [[-1 for i in range(m + 2)] for i in range(n + 2)]
bfs(map_info, dist, n, m, s, t)

print 'Case {}:'.format(t_case)
for i in range(1, n + 1):
for j in range(1, m + 1):
print dist[i][j],
print

if __name__ == '__main__':
main()
``````

## Search in XML

<题目来源：百度2017秋招 原题链接-可在线提交(赛码网)>

#### 问题描述

XML 设计用来传送及携带数据信息，不用来表现或展示数据，HTML语言则用来表现数据，所以 XML 用途的焦点是它说明数据是什么，以及携带数据信息。

``````＜recipe＞
＜recipename＞Ice Cream Sundae＜/recipename＞
＜ingredlist＞
＜listitem＞
＜quantity＞3＜/quantity＞
＜itemdescription＞chocolate syrup or chocolate fudge＜/itemdescription＞
＜/listitem＞
＜listitem＞
＜quantity＞1＜/quantity＞
＜itemdescription＞nuts＜/itemdescription＞
＜/listitem＞
＜listitem＞
＜quantity＞1＜/quantity＞
＜itemdescription＞cherry＜/itemdescription＞
＜/listitem＞
＜/ingredlist＞
＜preptime＞5 minutes＜/preptime＞
＜/recipe＞``````

``````class TreeType:
def __init__(self, lab='', father=None, num=0):
self.num = num
self.lab = lab
self.father = father
self.son_list = []``````

``````<a>
<b>
<c></c>
<c></c>
<c></c>
</b>
</a>``````

``<b><c></c></c>，``

``````import sys

const_self = 1

class TreeType:
def __init__(self, lab='', father=None, num=0):
self.num = num
self.lab = lab
self.father = father
self.son_list = []

def parse_line(line):
labs = []
t_lab = []
lab_f = False

for ch in line:
if ch == '<':
lab_f = True

if lab_f:
t_lab.append(ch)

if ch == '>':
lab_f = False
labs.append(list(t_lab))
t_lab[:] = []

return labs

def new_lab_to_tree(cn, r, cnt):
node = TreeType(r, cn, cnt)
cn.son_list.append(node)
return node

def finish_lab(cn, r):
return cn.father

def dfs(cn, mode, state, first_p, matched_p, fi):
lab = ''.join(cn.lab)

next_s = state
if lab == ''.join(mode[state]):
next_s += 1
else:
next_s = 0

if next_s == 1:
first_p = cn.num

if next_s >= len(mode) / 2 and not fi:
if not len(matched_p) or first_p != matched_p[-1]:
matched_p.append(first_p)
fi = True

for son in cn.son_list:
dfs(son, mode, next_s, first_p, matched_p, fi)

def main():
node_cnt = 0
current_node = TreeType('', None, node_cnt)
xml = []

while True:
labs = parse_line(line)
xml.append(labs)
for lab in labs:
if ''.join(lab).startswith('</'):
current_node = finish_lab(current_node, lab)  # r is used for verify
else:
node_cnt += 1
current_node = new_lab_to_tree(current_node, lab, node_cnt)

if current_node.num == 0:
break

matched_p = []

dfs(current_node, mode, 0, 0, matched_p, False)

print len(matched_p)
for p in matched_p:
print p,

if __name__ == '__main__':
main()
``````

## 内存检查

<题目来源：百度2017秋招 原题链接-可在线提交(赛码网)>

#### 问题描述

``````import sys

def test_scan(l_segment, i_segment_len, i_len, i_lim):
i_cur_lf = 0
i_use = 0
b_allzero = True
for i in range(i_segment_len):
if '1' == l_segment[i]:
b_allzero = False
if not i_cur_lf:
i_use += 1
i_cur_lf = i_len

if i_cur_lf:
i_cur_lf -= 1

if i_use <= i_lim:
if i_len > 0 or (0 == i_len and b_allzero):
return True
return False

if __name__ == '__main__':
i_test_case = int(raw_input())

for scr in range(i_test_case):
l_line = raw_input().split()
i_segment_len = int(l_line[0])
i_exe_lim = int(l_line[1])
l_segment = list(raw_input())

i_top = i_segment_len
i_bot = 0
while i_top >= i_bot:
i_mid = (i_top + i_bot) / 2

if test_scan(l_segment, i_segment_len, i_mid, i_exe_lim):
i_res = i_mid
i_top = i_mid - 1
else:
i_bot = i_mid + 1

print 'Case ' + str(scr + 1) + ': ' + str(i_res)``````

## 时钟

<题目来源: 百度2017秋招 原题链接-可在线提交(赛码网)>

#### 问题描述

``````import sys

const_board_row = 7
const_board_col = 100

template = [['******',
'*    *',
'*    *',
'*    *',
'*    *',
'*    *',
'******'],

['     *',
'     *',
'     *',
'     *',
'     *',
'     *',
'     *'],

['******',
'     *',
'     *',
'******',
'*     ',
'*     ',
'******'],

['******',
'     *',
'     *',
'******',
'     *',
'     *',
'******'],

['*    *',
'*    *',
'*    *',
'******',
'     *',
'     *',
'     *'],

['******',
'*     ',
'*     ',
'******',
'     *',
'     *',
'******'],

['******',
'*     ',
'*     ',
'******',
'*    *',
'*    *',
'******'],

['******',
'     *',
'     *',
'     *',
'     *',
'     *',
'     *'],

['******',
'*    *',
'*    *',
'******',
'*    *',
'*    *',
'******'],

['******',
'*    *',
'*    *',
'******',
'     *',
'     *',
'******']]

def compare_dig(board, col_begin, col_end):
for i in range(10):
match_f = True
for row in range(7):
# print ''.join(board[row][col_begin:col_end + 1])
if ''.join(board[row][col_begin:col_end + 1]) != ''.join(template[i][row]):
match_f = False
break
if match_f:
return i
return -1

def main():

for test_case in range(1, test_cases + 1):
board = [['' for i in range(const_board_col)] for j in range(const_board_row)]
for i in range(const_board_row):

# local divide tag
for i in range(const_board_col):
cnt = 0
for j in range(const_board_row):
if board[j][i] == '*':
cnt += 1
if cnt == 2 and board[2][i] == '*' and board[4][i] == '*':
dv_tag_col = i
break

dg_local = [-14, -7, 2, 9]
rs = []
for i in range(4):
rs.append(compare_dig(board, dv_tag_col + dg_local[i], dv_tag_col + dg_local[i] + 5))

hh = rs[0] * 10 + rs[1]
mm = rs[2] * 10 + rs[3]
print ('Case #{}: {}:{}').format(test_case, hh, mm)

if __name__ == '__main__':
main()``````

1 人关注
12 篇文章