解题思路:
广度搜索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语言代码)浏览:825 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:277 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:675 |
A+B for Input-Output Practice (V) (C语言代码)浏览:625 |
求组合数 (C语言代码)浏览:1159 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:557 |
简单的a+b (C语言代码)浏览:817 |
1025题解浏览:738 |
陈教主的三角形 (C语言代码)浏览:1143 |
青年歌手大奖赛_评委会打分 (C语言代码)浏览:2151 |