X
X
X
Revilla
PROGRAMMING CHALLENGES
The Programming Contest Training Manual
With 65 Illustrations
4y Springer
Contents
Getting Started 1
1.1 Getting Started With the Judge 1
1.1.1 The Programming Challenges Robot Judge 2
1.1.2 The Universidad de Valladolid Robot Judge 2
1.1.3 Feedback From the Judge 3
1.2 Choosing Your Weapon 4
1.2.1 Programming Languages 5
1.2.2 Reading Our Programs 6
1.2.3 Standard Input/Output 7
1.3 Programming Hints 9
1.4 Elementary Data Types 11
1.5 About the Problems 13
1.6 Problems 15
1.6.1 The 3n + 1 Problem 15
1.6.2 Minesweeper 16
1.6.3 The Trip 17
1.6.4 LCD Display 18
1.6.5 Graphical Editor 19
1.6.6 Interpreter 21
1.6.7 Check the Check 23
1.6.8 Australian Voting 25
1.7 Hints 26
1.8 Notes 26
Contents xiii
Data Structures 27
2.1 Elementary Data Structures 27
2.1.1 Stacks 28
2.1.2 Queues 28
2.1.3 Dictionaries 30
2.1.4 Priority Queues 31
2.1.5 Sets ~2
2.2 Object Libraries 33
2.2.1 T h e C + + Standard Template Library 33
2.2.2 The Java j a v a . u t i l Package 33
2.3 Program Design Example: Going to War 34
2.4 Hitting the Deck 35
2.5 String Input/Output 37
2.6 Winning the War 38
2.7 Testing and Debugging 39
2.8 Problems 42
2.8.1 Jolly Jumpers 42
2.8.2 Poker Hands 43
2.8.3 Hartals 45
2.8.4 Crypt Kicker 47
2.8.5 Stack 'em Up 48
2.8.6 Erdos Numbers 50
2.8.7 Contest Scoreboard 52
2.8.8 Yahtzee 53
2.9 Hints 55
2.10 Notes 55
Strings 56
3.1 Character Codes 56
3.2 Representing Strings 58
3.3 Program Design Example: Corporate Renamings 59
3.4 Searching for Patterns 61
3.5 Manipulating Strings 62
3.6 Completing the Merger 63
3.7 String Library Functions 64
3.8 Problems 66
3.8.1 WERTYU 66
3.8.2 Where's Waldorf? 67
3.8.3 Common Permutation 69
3.8.4 Crypt Kicker II 70
3.8.5 Automated Judge Script 71
3.8.6 File Fragmentation 73
3.8.7 Doublets 74
3.8.8 Fmt 75
3.9 Hints 77
Contents
3.10 Notes 77
Sorting 78
4.1 Sorting Applications 78
4.2 Sorting Algorithms 79
4.3 Program Design Example: Rating the Field 82
4.4 Sorting Library Functions 83
4.5 Rating the Field 85
4.6 Problems 88
4.6.1 Vito's Family 88
4.6.2 Stacks of Flapjacks 89
4.6.3 Bridge 91
4.6.4 Longest Nap 92
4.6.5 Shoemaker's Problem 94
4.6.6 CDVII 95
4.6.7 ShellSort 97
4.6.8 Football (aka Soccer) 99
4.7 Hints 101
4.8 Notes 101
Combinatorics 129
6.1 Basic Counting Techniques 129
6.2 Recurrence Relations 131
6.3 Binomial Coefficients 131
6.4 Other Counting Sequences 133
6.5 Recursion and Induction 135
6.6 Problems 137
6.6.1 How Many Fibs? 137
6.6.2 How Many Pieces of Land? 138
6.6.3 Counting 139
6.6.4 Expressions 140
6.6.5 Complete Tree Labeling 141
6.6.6 The Priest Mathematician 142
6.6.7 Self-describing Sequence 144
6.6.8 Steps 145
6.7 Hints 146
6.8 Notes 146
8 Backtracking 167
8.1 Backtracking 167
8.2 Constructing All Subsets 169
8.3 Constructing All Permutations 170
8.4 Program Design Example: The Eight-Queens Problem 172
8.5 Pruning Search 173
8.6 Problems 176
8.6.1 Little Bishops 176
8.6.2 15-Puzzle Problem 177
8.6.3 Queue 179
8.6.4 Servicing Stations 180
8.6.5 Tug of War 181
8.6.6 Garden of Eden 182
8.6.7 Color Hash 184
8.6.8 Bigger Square Please 186
8.7 Hints 188
8.8 Notes 188
12 Grids 268
12.1 Rectilinear Grids 268
12.1.1 Traversal 269
12.1.2 Dual Graphs and Representations 270
12.2 Triangular and Hexagonal Grids 271
12.2.1 Triangular Lattices 271
12.2.2 Hexagonal Lattices 272
12.3 Program Design Example: Plate Weight 275
12.4 Circle Packings 277
12.5 Longitude and Latitude 278
xviii Contents
13 Geometry 291
13.1 Lines 291
13.2 Triangles and Trigonometry 294
13.2.1 Right Triangles and the Pythagorean Theorem 295
13.2.2 Trigonometric Functions 295
13.2.3 Solving Triangles 296
13.3 Circles 298
13.4 Program Design Example: Faster Than a Speeding Bullet 299
13.5 Trigonometric Function Libraries 302
13.6 Problems 304
13.6.1 Dog and Gopher 304
13.6.2 Rope Crisis in Ropeland! 305
13.6.3 The Knights of the Round Table 306
13.6.4 Chocolate Chip Cookies 307
13.6.5 Birthday Cake 308
13.6.6 The Largest/Smallest Box 309
13.6.7 Is This Integration? 310
13.6.8 How Big Is It? 311
13.7 Hints 312
References 350
Index 353