Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
数独ではじめるアルゴリズム 第一章 実装と高速化
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Ryosuke Uchiyama
September 25, 2020
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
数独ではじめるアルゴリズム 第一章 実装と高速化
Ryosuke Uchiyama
September 25, 2020
More Decks by Ryosuke Uchiyama
See All by Ryosuke Uchiyama
AWSでサーバレスな書籍管理アプリを作る
step63r
0
74
PythonでSlack通知botを作る
step63r
0
24
ChatGPT実践
step63r
1
400
IoT実践! 行先予定表を電子ペーパーで作る
step63r
0
54
React x Socket.ioで人狼サーバを作る 第一章 フロントエンド実装
step63r
0
36
深層強化学習で東方AI 第一章 DQNの基本
step63r
0
430
Build 2021 プレイバック
step63r
0
45
WPFで実践アプリ開発! 第四章 機能の実装 Part 2
step63r
0
63
WPFで実践アプリ開発! 第三章 機能の実装 Part 1
step63r
0
140
Other Decks in Programming
See All in Programming
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
1.1k
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
500
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2.5k
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
150
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
180
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
3
440
The Rails Doctrine Decade
koic
2
600
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.3k
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
120
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
1.6k
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
320
Featured
See All Featured
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
950
New Earth Scene 8
popppiees
4
2.7k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
540
Automating Front-end Workflow
addyosmani
1369
210k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
How to Talk to Developers About Accessibility
jct
2
580
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
560
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
570
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
330
YesSQL, Process and Tooling at Scale
rocio
174
15k
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Transcript
数独ではじめるアルゴリズム 第一章 実装と高速化 Ryosuke Uchiyama step63r
はじめに
数独ってなに? 3✕3✕3のマスに1~9の数字を入れていくペンシルパズル 5 3 7 6 1 9 5 9
8 6 8 6 3 4 8 3 1 7 2 6 6 2 8 4 1 9 5 8 7 9 問題例 5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9 解答
手筋1 特定のマスに入る数値を絞る ★ 5 6 7 2 3 4 8
9 • ボックスより (2, 3, 4) は✕ • ヨコより (5, 6, 7) は✕ • タテより (8, 9) は✕ ★に入るのは 1 だけ! A B C 1 2 3 1 2 3 4 5 6 7 8 9
手筋2 特定の数値が入るマスを絞る 1 1 ★ 2 1 • C1の 1
でA1の1行目が潰れる • B1の 1 でA1の2行目が潰れる • A2の 1 でA1の3列目が潰れる 1 が入るのは★だけ! A B C 1 2 3
経緯
その昔……
step63r/NumpreTool02: Resolve NUMPRE (Number Place) automatically.
None
ところが……
None
None
None
_人人人人人人人人人_ > 解答できません <  ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^ ̄
Software Design 8月号 技術評論社 Software Design 2020年8月号|技術評論社
ここに書かれていることをやってみました
探索アルゴリズム
グラフ構造と深さ優先探索(DFS) 0 1 2 3 4 11 5 8 6
7 9 10 12 13 14
数独の盤面をグラフ化 ※ここでは単純化のため3✕3の盤面で考える 2 3 1 2 1 2 2 3
1 2 2 1 2 2 3 1 3 2 1 2 2 3 1 3 2 1 1 2 2 3 1 3 2 2 1 2 2 3 1 3 2 3 1 2 2 3 1 1 2 1 2 2 3 1 1 2 1 1 2 2 3 1 1 2 2 1 2 2 3 1 1 2 3 1 2 2 3 1 1 2 3 1 1 2 2 3 1 1 2 3 2 1 2 2 3 1 1 2 3 3 1 2
実装してみた SudokuCpp python-sudoku
Demo
高速化
ベンチマーク(Python版) 4 9 2 4 8 2 1 4 6
6 9 4 8 3 1 3 5 6 7 7 5 6 2 6 1 3 5 2 中級クラス 0.6955 秒 2 1 8 4 9 6 3 1 2 7 9 6 4 6 9 3 7 専門級クラス 110.6565 秒 ※Intel Core i7-10700 @2.90 GHz 空きマスが増えるにつれて処理時間が長くなってしまう
改善アプローチ その1 入れられる数字の選択肢が少ないマスから埋めていく その2 一意に決まるマスはあらかじめ埋めておく
入れられる数字の選択肢が少ないマスから埋めていく 3 6 5 4 8 7 9 4 5
1 9 7 3 5 6 8 9 6 5 4 4 6 5 5 6 5 3 5 3 4 5 6 5 3 4 4 4 6 5 5 5 4 4 5 5 5 4 4 5 5 5 4 7 3 3 4 3 4 4 5 6 4 4 5 6 5 4 4 6 4 5 7 7 4 6 7 7 5 6 6 8 問題 入れられる数字の残数 ここから処理するよりも… この辺から処理した方が速い!
一意に決まるマスはあらかじめ埋めておく 3 6 5 4 8 7 9 4 5
1 9 7 3 5 6 8 9 問題 5 8 3 6 5 4 8 7 5 9 4 6 5 9 1 5 9 7 3 5 6 8 5 9 5 前処理後の問題
ベンチマーク比較(Python版) 測定不可(時間かかりすぎ) 1080.966 281.378 5.0795 0 200 400 600 800
1000 1200 対策なし 対策1 対策2 対策1&2 最高級クラスを30問解くのにかかった時間 (秒)
まとめ
たまにはゴリゴリ書くのも楽しいよ!!!!!!!!
今後やること • UI実装(WPFかXamarinあたり) • モバイル対応(iOSアプリをビルドする環境がない……つらたん……) • OCR(問題をカメラで撮ってAzure Cognitive Servicesとかで盤面認識&自動解答) •
マルチスレッド(たぶんやらないやつ)