目錄
卡諾圖的相關知識可以參考此處:https://blog.csdn.net/hahasusu/article/details/88244155
二燈遊戲
一個遊戲機有兩個燈,一黃一綠,它們忽閃忽滅,你必須在出現以下情況的時候迅速按下遊戲機:
- 綠燈滅,黃燈亮
- 綠燈、黃燈都滅
- 綠燈、黃燈都亮
解題思路:
1、首先定義兩個基本的命題
- 命題A:綠燈亮
- 命題B:黃燈亮
2、畫出卡諾圖
A爲綠燈,0和1分別表示該燈的狀態;
B爲黃燈,同理。
根據遊戲規則的3種情況,分別對應卡諾圖種的:00、01、11
根據卡諾圖每格具備相鄰性的特點,使用1*1、1*2、2*2、4*4的網格圈出(僅能使用2^n個格子去圈)
3、卡諾圖化簡
根據上圖得出式子:(A`B` + A`B) + (A`B+AB)= A` + B (A`表示A反)
由此得出只要滿足 A` V(V表示或) B ,即綠燈滅 或者 黃燈亮即可按下按鈕
4、驗證
我們先不用卡諾圖化簡法來解決上述問題,以下是python代碼:
'''
二燈遊戲:
1、綠燈滅,黃燈亮
2、綠燈、黃燈都滅
3、綠燈、黃燈都亮
'''
def isPress(green, yellow):
if green == False and yellow == True:
return True
elif green == False and yellow == False:
return True
elif green == True and yellow == True:
return True
return False
#綠燈滅,黃燈亮
status = isPress(green=False, yellow=True)
print(status)
#綠燈、黃燈都滅
status = isPress(green=False, yellow=False)
print(status)
#綠燈、黃燈都亮
status = isPress(green=True, yellow=True)
print(status)
#綠燈亮,黃燈滅
status = isPress(green=True, yellow=False)
print(status)
輸出結果:
卡諾圖化簡後的式子A` V B,代碼:
def isPress2(green, yellow):
if (not green) or yellow :
return True
return False
status = isPress2(green=False, yellow=True)
print(status)
status = isPress2(green=False, yellow=False)
print(status)
status = isPress2(green=True, yellow=True)
print(status)
status = isPress2(green=True, yellow=False)
print(status)
輸出結果:
由此可見使用卡諾圖即可把複雜的句子簡化成兩行。
三燈遊戲
將上述遊戲進行升級,存在3種燈(綠燈、黃燈、紅燈)的情況下,符合如下規則即可按下按鈕:
- 3燈都滅
- 黃燈滅、紅燈亮
- 綠燈滅、黃燈亮
- 3燈都亮
解題思路:
1、定義命題
- 命題A:綠燈亮
- 命題B:黃燈亮
- 命題C:紅燈亮
2、畫出卡諾圖,並將上述情況在卡諾圖中用1來代替原先的二進制
上圖將4個規則在卡諾圖中對應出來。
3、卡諾圖化簡
根據卡諾圖每格具備相鄰性的特點,進行如下的畫圈:
因此可得式子:(A`B`C`+A`B`C+A`BC+A`BC`) + (A`B`C+A`BC+AB`C+ABC)
= (A`B` + A`B) + (A`C +AC)
= A` + C
化簡後獲得 A` + C
4、驗證
正常解法:
def isThreeLightPress(green, yellow, red):
if green == False and yellow == False and red == False:
return True
elif yellow == False and red:
return True
elif green == False and yellow:
return True
elif green and yellow and red:
return True
return False
# 全燈滅
status = isThreeLightPress(green=False, yellow=False, red=False)
print(status)
# 黃燈滅、紅燈亮(不在乎綠燈的狀態,因此都加上)
status = isThreeLightPress(green=False, yellow=False, red=True)
print(status)
status = isThreeLightPress(green=True, yellow=False, red=True)
print(status)
# 綠燈滅、黃燈亮(不在乎紅燈的狀態,因此都加上)
status = isThreeLightPress(green=False, yellow=True, red=False)
print(status)
status = isThreeLightPress(green=False, yellow=True, red=True)
print(status)
# 全燈亮
status = isThreeLightPress(green=True, yellow=True, red=True)
print(status)
輸出結果:
使用卡諾圖化簡法:
#卡諾圖改造爲not green or red
def isThreeLightPress2(green, yellow, red):
if not green or red:
return True
return False
print('---------this is a split----------')
# 全燈滅
status = isThreeLightPress2(green=False, yellow=False, red=False)
print(status)
# 不在乎綠燈的狀態,因此都加上
status = isThreeLightPress2(green=False, yellow=False, red=True)
print(status)
status = isThreeLightPress2(green=True, yellow=False, red=True)
print(status)
# 不在乎紅燈的狀態,因此都加上
status = isThreeLightPress2(green=False, yellow=True, red=False)
print(status)
status = isThreeLightPress2(green=False, yellow=True, red=True)
print(status)
# 全燈亮
status = isThreeLightPress2(green=True, yellow=True, red=True)
print(status)
輸出結果: