DOJ logoDOJ
LoginRegister
DOJ logoDOJ
Call for Tasks
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Editorial

Sliding Puzzle

Back to problem

Editorial

Each misplaced block needs two vertical moves and at least ∣Ai−i∣|A_i-i|∣Ai​−i∣ horizontal moves, giving the lower bound ∑Ai≠i(∣Ai−i∣+2)\sum_{A_i\neq i}(|A_i-i|+2)∑Ai​=i​(∣Ai​−i∣+2). Process each nontrivial permutation cycle, labeling blocks X1,…,XkX_1,\ldots,X_kX1​,…,Xk​ so each targets the next block's starting cell. Depart X1X_1X1​, depart X2X_2X2​, finish X1X_1X1​, depart X3X_3X3​, finish X2X_2X2​, and so on, finally finishing XkX_kXk​. Alternate top and bottom rows. At most two consecutive blocks are outside, on different rows; each destination is already vacated. Thus every block takes a shortest horizontal path and the bound is attained. Time is O(N)O(N)O(N) and auxiliary space O(1)O(1)O(1). The answer is at most ⌊N2/2⌋+2N\lfloor N^2/2\rfloor+2N⌊N2/2⌋+2N, or 200004000002000040000020000400000 at maximum NNN. For the small subtask, BFS over actual boards finds the shortest distance: at most (3N)!/(2N)!(3N)!/(2N)!(3N)!/(2N)! states and 4N4N4N transitions per state.

Solution written by GPT6