├── Main.py ├── README.md ├── main_v1.py ├── main_v2.py └── pic ├── result1.jpg ├── result2.jpg └── reuslt3.jpg /Main.py: -------------------------------------------------------------------------------- 1 | import functools 2 | def custom_sort(x,y): 3 | for i in range(len(x)): 4 | if(x[i] < y[i]): 5 | return -1 6 | elif(x[i] > y[i]): 7 | return 1 8 | return 1 9 | 10 | #data = open("./data_test/77409/test_data.txt", "r") 11 | #data = open("./test_data.txt", "r") 12 | data = open("/data/test_data.txt", "r") 13 | map_record = {} 14 | for i in data: 15 | p1,p2,money = [int(k) for k in i.split(",")] 16 | if(p1 not in map_record): 17 | map_record[p1] = set() 18 | map_record[p1].add(p2) 19 | 20 | result = {} 21 | for i in range(2,7): 22 | result[i] = [] 23 | sum_len = 0 24 | 25 | for key in sorted(map_record.keys()): 26 | que = set() 27 | path = {} 28 | position = {} 29 | 30 | que.add(key) 31 | path[0] = [key] 32 | position[key] = [0] 33 | 34 | for i in range(7): 35 | cnt = 0 36 | new_node = set() 37 | new_path = {} 38 | new_position = {} 39 | for each_node in sorted(list(que)): 40 | if(each_node in map_record): 41 | for add_node in sorted(map_record[each_node]): 42 | if(add_node == key): 43 | if(i > 1): 44 | for each_poisiton in position[each_node]: 45 | sum_len = sum_len + 1 46 | result[i].append(path[each_poisiton]) 47 | elif(add_node > key): 48 | if(add_node not in new_node): 49 | new_node.add(add_node) 50 | new_position[add_node] = [] 51 | for each_poisiton in position[each_node]: 52 | if(add_node not in path[each_poisiton]): 53 | new_path[cnt] = path[each_poisiton]+[add_node] 54 | new_position[add_node].append(cnt) 55 | cnt = cnt + 1 56 | else: 57 | for each_poisiton in position[each_node]: 58 | if (add_node not in path[each_poisiton]): 59 | new_path[cnt] = path[each_poisiton] + [add_node] 60 | new_position[add_node].append(cnt) 61 | cnt = cnt + 1 62 | #print(sorted(que)) 63 | #print(sorted(new_node)) 64 | #a = input() 65 | que = new_node 66 | position = new_position 67 | path = new_path 68 | 69 | #file = open("result1.txt", "w") 70 | file = open("/projects/student/result.txt", "w") 71 | file.write(str(sum_len)+"\n") 72 | for i in range(2,7): 73 | result[i] = sorted(result[i],key=functools.cmp_to_key(custom_sort)) 74 | for each_result in result[i]: 75 | for j in range(len(each_result)): 76 | if(j == 0): 77 | file.write(str(each_result[j])) 78 | else: 79 | file.write(","+str(each_result[j])) 80 | file.write("\n") -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # huawei2020 2 | 华为软件精英挑战赛2020,一起加油吧 3 | ## 第三版代码(广搜剪枝+深搜搜索): [main_v2.py](https://github.com/gao793583308/huawei2020/blob/master/main_v2.py) 4 | ![Image text](https://github.com/gao793583308/huawei2020/blob/master/pic/reuslt3.jpg) 5 | todo:尝试改成c++,有没有大佬教教我怎么读图之类的基本操作 6 | ## 第二版代码(深搜): [main_v1.py](https://github.com/gao793583308/huawei2020/blob/master/main_v1.py) 7 | ![Image text](https://github.com/gao793583308/huawei2020/blob/master/pic/result2.jpg) 8 | ### [视频讲解](https://m.ixigua.com/group/6815115124836139535) 9 | ## 第一版代码(广搜+剪枝): [Main.py](https://github.com/gao793583308/huawei2020/blob/master/Main.py) 10 | 缺点:1.广搜7层节点数可能很多,速度慢。2.python天生弊端?优化中 11 | ![Image text](https://github.com/gao793583308/huawei2020/blob/master/pic/result1.jpg) 12 | -------------------------------------------------------------------------------- /main_v1.py: -------------------------------------------------------------------------------- 1 | def deep_search(map_record,path,result,cnt): 2 | if(cnt > 7): 3 | return 4 | node = path[-1] 5 | if(node in map_record): 6 | for each_node in sorted(map_record[node]): 7 | if(each_node == path[0]): 8 | if(cnt > 2): 9 | result[cnt].append(path) 10 | elif(each_node > path[0] and each_node not in path): 11 | deep_search(map_record,list(path)+[each_node],result,cnt + 1) 12 | return 13 | 14 | #data = open("./test_data.txt", "r") 15 | data = open("./data_test/77409/test_data.txt", "r") 16 | #data = open("/data/test_data.txt", "r") 17 | map_record = {} 18 | for i in data: 19 | p1,p2,money = [int(k) for k in i.split(",")] 20 | if(p1 not in map_record): 21 | map_record[p1] = set() 22 | map_record[p1].add(p2) 23 | 24 | result = {} 25 | for i in range(3,8): 26 | result[i] = [] 27 | 28 | for key in sorted(map_record.keys()): 29 | print(key) 30 | deep_search(map_record,[key],result,1) 31 | 32 | sum_len = 0 33 | for i in range(3,8): 34 | sum_len = sum_len + len(result[i]) 35 | 36 | file = open("./result1.txt", "w") 37 | file.write(str(sum_len)+"\n") 38 | for i in range(3,8): 39 | for each_result in result[i]: 40 | for j in range(len(each_result)): 41 | if(j == 0): 42 | file.write(str(each_result[j])) 43 | else: 44 | file.write(","+str(each_result[j])) 45 | file.write("\n") 46 | -------------------------------------------------------------------------------- /main_v2.py: -------------------------------------------------------------------------------- 1 | def deep_search(map_record,path,result,cnt, visited): 2 | if(cnt > 7): 3 | return 4 | node = path[-1] 5 | if(node in map_record): 6 | for each_node in sorted(map_record[node]): 7 | if(each_node == path[0]): 8 | if(cnt > 2): 9 | result[cnt].append(path) 10 | elif(each_node > path[0] and each_node not in path): 11 | if(cnt > 3 and each_node not in visited[path[0]]): 12 | continue 13 | deep_search(map_record,list(path)+[each_node],result,cnt + 1, visited) 14 | return 15 | 16 | def search_all(map_record,visited): 17 | for key in map_record.keys(): 18 | #print(1,key) 19 | que = set() 20 | que.add(key) 21 | for i in range(3): 22 | que_new = set() 23 | for node in que: 24 | if (node in map_record): 25 | for each_node in sorted(map_record[node]): 26 | que_new.add(each_node) 27 | visited[each_node].add(key) 28 | que = que_new 29 | 30 | #data = open("./test_data.txt", "r") 31 | #data = open("./data_test/1004812/test_data.txt", "r") 32 | data = open("/data/test_data.txt", "r") 33 | map_record = {} 34 | visited = {} 35 | for i in data: 36 | p1,p2,money = [int(k) for k in i.split(",")] 37 | if(p1 not in map_record): 38 | map_record[p1] = set() 39 | map_record[p1].add(p2) 40 | if(p1 not in visited): 41 | visited[p1] = set() 42 | if(p2 not in visited): 43 | visited[p2] = set() 44 | 45 | result = {} 46 | for i in range(3,8): 47 | result[i] = [] 48 | 49 | search_all(map_record,visited) 50 | #print(visited) 51 | for key in sorted(map_record.keys()): 52 | #print(key) 53 | deep_search(map_record,[key],result,1, visited) 54 | 55 | sum_len = 0 56 | for i in range(3,8): 57 | sum_len = sum_len + len(result[i]) 58 | 59 | #file = open("./result1.txt", "w") 60 | file = open("/projects/student/result.txt", "w") 61 | file.write(str(sum_len)+"\n") 62 | for i in range(3,8): 63 | for each_result in result[i]: 64 | for j in range(len(each_result)): 65 | if(j == 0): 66 | file.write(str(each_result[j])) 67 | else: 68 | file.write(","+str(each_result[j])) 69 | file.write("\n") 70 | -------------------------------------------------------------------------------- /pic/result1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gao793583308/huawei2020/49c2445a5f98d1c7e0b8d1d1cd0d4e773c48b883/pic/result1.jpg -------------------------------------------------------------------------------- /pic/result2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gao793583308/huawei2020/49c2445a5f98d1c7e0b8d1d1cd0d4e773c48b883/pic/result2.jpg -------------------------------------------------------------------------------- /pic/reuslt3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gao793583308/huawei2020/49c2445a5f98d1c7e0b8d1d1cd0d4e773c48b883/pic/reuslt3.jpg --------------------------------------------------------------------------------