Word Swap

Description

Discuss (3 comments)
Given a dictionary and two words of equal length that are in the dictionary, find a sequence of transformations required to rewrite the first word into the second one. Each word in the sequence must belong to the dictionary and must differ from the previous word by only one letter.

For example, if the words are: BIG CAT. Any of the following sequences is a valid answer:
  • BIG, BIT, BUT, BAT, CAT.
  • BIG, BIT, BAT, CAT.
So your program can return whichever it wants, both solutions will be accepted as valid.

Input Specifications

Your program must take three command line arguments in this order:
  1. the file name of the dictionary containing the allowed words
  2. the word to start from
  3. the word to reach to
For example, the command line for the previous example would be:
myprogram.exe minidic.txt BIG CAT
Where myprogram.exe is the program you write and minidic.txt the file where you stored the dictionary given in example. So obviously, tour program must then open the dictionary file and read its data.

The dictionary file contains one word per line and is sorted alphabetically. Words can have any length and are all in UPPER case. For example
BAG
BANG
BAT
BIG
BIT
BUG
BUT
CAR
CAT

Output Specifications

Your program should print the sequence of words on the standard output (stdout/printf/echo/puts). It should print one word per line. Using the above example, the output could be:
BIG
BIT
BAT
CAT
As mentioned earlier, any valid sequence is acceptable regardless of its length.
© CoderCharts - All Rights Reserved
Type Puzzle Pass 71 Fail 207
Scoring Language Level Novice Points 400
Tags backtracking, characters, ordering, recursion, words

Source code editor

Unit Tests

Name CPU Unit Memory Unit

Simple test

Just 2 easy words
1s
250 MB

Medium test

2 slightly harder words
2s
250 MB

Hard test

Difficult words
3s
250 MB

Extreme

These are tricky to trace
4s
250 MB

Confirm that you want to switch languages
Your current edits will be lost.

Confirm that you wish to load a previous submission
Your current edits will be lost.