之前纯粹是打酱油的心态报了软挑赛,没想到因为学长的指导而接触到了搜索。学长让我做了两三题,但每题对于基本没接触过算法的我来说都是登天难。。。好在看了网上其他一些大佬的思路后,总结出了自己的解决方案。现记录下来以分享。
No.1 城堡问题
链接:http://noi.openjudge.cn/ch0205/1817/
代码:
height = int(input())
width = int(input())
graph = {}
room = [-1 for i in range(height)]
for i in range(height):
room[i] = input().split()
for j in range(width):
string = str(i+1) + '-' + str(j+1)
graph.setdefault(string,[])
for i in range(height):
for j in range(width):
string = str(i+1) + '-' + str(j+1)
if int(room[i][j])&1 == 0:
stringWest = str(i+1) + '-' + str(j)
graph[string].append(stringWest)
if int(room[i][j])&2 == 0:
stringNorth = str(i) + '-' + str(j+1)
graph[string].append(stringNorth)
if int(room[i][j])&4 == 0:
stringEast = str(i+1) + '-' + str(j+2)
graph[string].append(stringEast)
if int(room[i][j])&8 == 0:
stringSouth = str(i+2) + '-' + str(j+1)
graph[string].append(stringSouth)
bigSeen = set()
def DFS(graph, s):
global bigSeen
stack = []
stack.append(s)
seen = set()
seen.add(s)
bigSeen.add(s)
roomNum = 0
while len(stack) > 0:
vertex = stack.pop()
nodes = graph[vertex]
for i in nodes:
if i not in seen:
stack.append(i)
seen.add(i)
bigSeen.add(i)
roomNum += 1
return roomNum
totalRoom = 0
roomList = []
for i in graph.keys():
if i not in bigSeen:
roomList.append(DFS(graph, i))
totalRoom += 1
print(totalRoom)
print(max(roomList))
No.2 分成互质组
链接:http://noi.openjudge.cn/ch0205/7834/
代码:
def gcd(x, y):
if y == 0:
return x
else:
return gcd(y, x%y)
def judgeGcd(x, y):
if gcd(x, y) == 1:
return True
else:
return False
items = int(input())
numList = input().split()
total = 0
graph = {}
for i in numList:
graph.setdefault(i,[])
for i in range(len(numList)):
for j in range(len(numList)):
if judgeGcd(int(numList[i]),int(numList[j])) == True:
graph[numList[i]].append(numList[j])
zuBie=[0]*items
zuBie[0]=1
total = 1
def fenZu(i):
global total, items
panBie=[0]*items
flag = 0
if i >= int(items):
return 0
else:
t = numList[i]
for j in range(i-1, -1, -1):
if panBie[j] == 0:
flag = 1
for k in range(j, -1, -1):
if zuBie[k] == zuBie[j]:
panBie[k] = 1
if t not in graph[numList[k]]:
flag = 0
if flag == 1:
zuBie[i] = zuBie[j]
break
if zuBie[i] == 0:
total += 1
zuBie[i] = total
if i < items:
fenZu(i+1)
else:
return 0
fenZu(1)
print(total)
No.3 八皇后问题
链接:http://noi.openjudge.cn/ch0205/1700/
代码:
graph = {'A':[0]*8, 'B':[0]*8, 'C':[0]*8, 'D':[0]*8, 'E':[0]*8, 'F':[0]*8, 'G':[0]*8, 'H':[0]*8}
def pprintGraph(graph):
deleteX()
for i in range(8):
cha = 'A'
while cha != 'I':
print(graph[cha][i], end = ' ')
cha = chr(ord(cha)+1)
print()
def setChess(column, row):
graph[column][row-1] = 1
cha = chr(ord(column)+1)
while cha != 'I':
graph[cha][row-1] = 2
cha = chr(ord(cha)+1)
num = row-1
cha = chr(ord(column)+1)
while cha != 'I' and num >= 1:
graph[cha][num-1] = 2
num -= 1
cha = chr(ord(cha)+1)
num = row+1
cha = chr(ord(column)+1)
while cha != 'I' and num <= 8:
graph[cha][num-1] = 2
num += 1
cha = chr(ord(cha)+1)
def deleteX():
for i in range(7, -1, -1):
cha = 'A'
while cha != 'I':
if graph[cha][i] == 2:
graph[cha][i] = 0
cha = chr(ord(cha)+1)
def deleteChess(column, row):
graph[column][row-1] = 0
deleteX()
for i in range(7, -1, -1):
cha = 'A'
while cha != 'I':
if graph[cha][i] == 1:
setChess(cha, i+1)
cha = chr(ord(cha)+1)
total = 0
def findChess(column):
global total
if column == 'I':
total +=1
print('No. '+str(total))
pprintGraph(graph)
return 0
for row in range(1, 9, 1):
if graph[column][row-1] == 0:
setChess(column, row)
findChess(chr(ord(column) + 1))
deleteChess(column, row)
findChess('A')
本人代码基础薄弱,算法全无,还请大佬轻喷。这些题是学长在高中备战奥林匹克时就做过的题目,感受到了很大的差距。。还要继续努力!!