Puzzle solving: Minimum number of steps to achieve a goal

I have been doing some programming contest problems, and I have noticed that many of them involve something along the lines of “get the minimum number of steps necessary to achieve a certain goal”.

Examples:

  • Given the position of a knight and certain pawns in a chessboard, what is the minimum number of jumps the knight has to do to kill all the pawns?
  • Given a parking lot with disordered cars labeled alphabetically, a “move” is the process of taking a car and inserting it elsewhere. What is the minimum number of moves to get this parking lot ordered?

So, many puzzles seem to share this structure: given a scenario, some rules, and a set of “operations”, determine the least amount of operations to achieve a specific goal.

There’s a twist in some of these puzzles though – sometimes, the puzzles will also ask if it is impossible to achieve.

I have discovered that I am particularly terrible with this kind of puzzles. In fact, I don’t think I have ever solved them in an elegant way ever.

I’m not sure how to “test all cases”, “pick the shortest one” or “determine it is impossible”. Can you advice me here?

For the sake of the question, we can base ourselves on the chess puzzle:

Example Problem

We got a chessboard. Each square is labeled with a number (which represents the position), from 1 to 64.

(Possible) Input

2 8 31 13

This means “two pawns, on positions 8 and 31. The knight is on position 13.”

Output

2

Two steps. From position 13, the knight can jump to 8, and from 8, it can jump to 31.


The Question

How can I approach this kind of problem? I solved the chess one by generating a list of all possible steps and then I picked the shortest one. But that was very slow.

8

There are more than a few different ideas one could employ on these kinds of problems:

Breadth-first search would be one technique to find the minimum moves for a problem that basically enumerates all possibilities. While this is brute force there can be some applications of this technique that can work quite well. The alternative to this is a depth-first search.

Dynamic programming would be another technique that may be used to generate answers assuming certain characteristics about the problem. This can be used if one can break down a problem into smaller pieces and then build up which way to go.

Greedy algorithms would be another heuristic that can be useful with these kinds of problems as “making change” would be a classic that is solved with this method.

Divide and conquer would be another strategy that can be employed to solve some types of computing problems. Mergesort would be an example of using this technique.

These are a few of the common methods that would be taught as part of an, “Introduction to Algorithm Design and Analysis” course that covers how to use these techniques.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật