SlideShare a Scribd company logo
1 of 14
川渡り問題
@tokoroten_lab
問題
• https://ja.wikipedia.org/wiki/%E5%B7%9D%E6%B8%A1%E3
%82%8A%E5%95%8F%E9%A1%8C
問題解決方法
プログラムで解く
普通に解いても面白くないため…
そのためにこの問題を解くアルゴリズムを考える
考察
• 問題を図にしてみる
宣教師
人喰い
ボート
左岸 右岸
川
初期状態
考察
• 問題を図にしてみる
宣教師
人喰い
ボート
左岸 右岸
川
最終状態
こうしたい
考察
• ボートに乗る宣教師と人喰いの組み合わせを考える。
ボートの上で宣教師が
食べられてしまう
組み合わせは存在しない。
ボート
考察
• ボートの上で宣教師が食べらることはないため、左の川岸と右
の川岸それぞれで以下の関係が成り立ってさえすれば良い。
• 上の条件を満たしながら、全ての宣教師と人喰いを右の川岸に
移動させることが目標。
宣教師の数 ≥ 人喰いの数
考察
• ある時点での左岸の宣教師と人喰いの数を𝑙1, 𝑙2とし、右岸の宣教師と人喰
いの数を𝑟1, 𝑟2とする。さらにボートの位置を𝑏(左岸なら0, 右岸なら1)とす
るとその時点での状態は 𝑙1, 𝑙2, 𝑟1, 𝑟2, 𝑏 と表現できる。
• 𝑙1, 𝑙2, 𝑟1, 𝑟2, 𝑏 という状態から次の状態への遷移(ボートによる移動)は以下
の通りである。
𝑙1, 𝑙2, 𝑟1, 𝑟2, 𝑏
𝑠 = (−1) 𝑏
𝑙1 − 𝑠, 𝑙2 , 𝑟1 +𝑠, 𝑟2 , 1 − 𝑏
𝑙1, 𝑙2 −𝑠, 𝑟1 , 𝑟2 +𝑠, 1 − 𝑏
𝑙1 − 2𝑠, 𝑙2, 𝑟1 + 2𝑠, 𝑟2 , 1 − 𝑏
𝑙1, 𝑙2 − 2𝑠, 𝑟1, 𝑟2 + 2𝑠, 1 − 𝑏
𝑙1 − 𝑠, 𝑙2 − 𝑠, 𝑟1 + 𝑠, 𝑟2 + 𝑠, 1 − 𝑏
𝑙1′, 𝑙2′, 𝑟1′, 𝑟2′, 𝑏′
ただし、以下の条件が伴う
𝑙1
′
≥ 𝑙2
′
, 𝑟1
′
≥ 𝑟2
′
,
𝑙1
′
≥ 0, 𝑙2
′
≥ 0, 𝑟1
′
≥ 0, 𝑟2
′
≥ 0
考察
• この問題が解決可能であるなら 𝑙1, 𝑙2, 𝑟1, 𝑟2, 𝑏 から
𝑙1′, 𝑙2′, 𝑟1′, 𝑟2′, 𝑏′ に条件を満たしながら遷移することで
(0,0,3,3,1)に到達可能なはずである。
• したがって、初期状態の(3,3,0,0,0)から(0,0,3,3,1)への遷移経路
を探すことでこの問題を解決するための手順がわかる。
• 深さ優先探索を利用してこの経路探索を行う。
注意点
• 深さ優先探索を行う上で注意しなければならない点がある。
• ある 𝑙1, 𝑙2, 𝑟1, 𝑟2, 𝑏 の状態に対して、遷移元は一意ではない。
• つまり、探索中に同じ 𝑙1, 𝑙2, 𝑟1, 𝑟2, 𝑏 の状態に遷移する経路が存
在する。
• 例を挙げると(3,3,0,0,0)→(2,3,1,0,1)→(3,3,0,0,0)といった遷移
などがある。
• 上のような場合、無限ループに陥ってしまう。したがって、一
度訪れた状態はそれ以降は訪れないようにする。
実装
• 以上の考察と注意点を踏まえて、C++11にて深さ優先探索を行
うプログラムを実装する。
• この作ったプログラムは、GitHubに公開したため、以下のURL
から確認できる。
• https://github.com/tokoroten-
lab/math_problems/blob/master/boat_missionary_eater_pro
blem/solver.cpp
実行結果
• プログラムの実行結果を示す
結果
(3,3,0,0,0) 左岸から二人の人喰いを右岸に運ぶ
→(3,1,0,2,1) 右岸から一人の人喰いを左岸に運ぶ
→(3,2,0,1,0) 左岸から二人の人喰いを右岸に運ぶ
→(3,0,0,3,1) 右岸から一人の人喰いを左岸に運ぶ
→(3,1,0,2,0) 左岸から二人の宣教師を右岸に運ぶ
→(1,1,2,2,1) 右岸から一人の宣教師と一人の人喰いを左岸に運ぶ
→(2,2,1,1,0) 左岸から二人の宣教師を右岸に運ぶ
→(0,2,3,1,1) 右岸から一人の人喰いを左岸に運ぶ
→(0,3,3,0,0) 左岸から二人の人喰いを右岸に運ぶ
→(0,1,3,2,1) 右岸から一人の人喰いを左岸に運ぶ
→(0,2,3,1,0) 左岸から二人の人喰いを右岸に運ぶ
→(0,0,3,3,1) 終了
結果
プログラムの結果をもとに
図に表したものが左図になる

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

川渡り問題