解题思路:
广度搜索bfs。
注意事项:
参考代码:
if __name__ == '__main__': n = int(input().strip()) s = input().strip() queen = [[s,0]] states = [] if s.count('2') < 2 or s.count('1') < 1 or s.count('0') < 1: print(-1) else: if '2012' in s: print(0) else: flag = 0 while queen: now_state = queen.pop(0) states.append(now_state[0]) for i in range(n-1): temp = now_state[0][:i]+now_state[0][i+1]+now_state[0][i]+now_state[0][i+2:] if '2012' in temp: print(now_state[1]+1) flag = 1 break if temp not in states: queen.append([temp,now_state[1]+1]) if flag: break
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:546 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:1919 |
模拟计算器 (C++代码)浏览:807 |
C二级辅导-分段函数 (C语言代码)浏览:601 |
陈教主的三角形 (C语言代码)浏览:1143 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:643 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:564 |
判定字符位置 (C++代码)浏览:676 |
A+B for Input-Output Practice (C语言代码)浏览:472 |
P1002 (Java代码)浏览:774 |