












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In all programming language only syntax is different not the logic. This course discuss core concepts for many different programming language and techniques. Key points for this lecture are: Backtracking, Solving a Maze, Coloring a Map, Solving a Puzzle, Animation, Terminology, Nodes, Real and Virtual Trees, Backtracking Algorithm, Map Coloring
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













7
There are three kinds of nodes:
A tree is composed of nodes
The (one) root node Internal nodes Leaf nodes
Backtracking can be thought of as searching a tree for a particular “goal” leaf node
8
parent
children
parent
children
Usually, however, we draw our trees downward, with the root at the top
3.1. Explore C 3.1.1. If C was successful, return “success”
Creating the map
13
0 1 2 4 3 5 6
int map[][];
void createMap() { map = new int[7][]; map[0] = new int[] { 1, 4, 2, 5 }; map[1] = new int[] { 0, 4, 6, 5 }; map[2] = new int[] { 0, 4, 3, 6, 5 }; map[3] = new int[] { 2, 4, 6 }; map[4] = new int[] { 0, 1, 6, 3, 2 }; map[5] = new int[] { 2, 6, 1, 0 }; map[6] = new int[] { 2, 3, 4, 1, 5 }; }
14
static final int NONE = 0; static final int RED = 1; static final int YELLOW = 2; static final int GREEN = 3; static final int BLUE = 4;
int mapColors[] = { NONE, NONE, NONE, NONE, NONE, NONE, NONE };
boolean explore(int country, int color) { if (country >= map.length) return true; if (okToColor(country, color)) { mapColors[country] = color; for (int i = RED; i <= BLUE; i++) { if (explore(country + 1, i)) return true; } } return false; }
boolean okToColor(int country, int color) { for (int i = 0; i < map[country].length; i++) { int ithAdjCountry = map[country][i]; if (mapColors[ithAdjCountry] == color) { return false; } } return true; }
Recap