Pippin’s Garden Problem There are many solutions depending how efficient you want to be. Generate all possible locations (x,y) for the lower left corner,

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Summer 2012 Instructor: Hassan Khosravi
MATH 224 – Discrete Mathematics
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
(JUST LIKE PHASE 10 CARD GAME) pHase-ion. General Rules Each player is dealt 10 cards. The remaining cards are placed in the middle for a card pool from.
Minimax and Alpha-Beta Reduction Borrows from Spring 2006 CS 440 Lecture Slides.
AE1APS Algorithmic Problem Solving John Drake..  Previously we introduced matchstick games, using one pile of matches  It is possible to have more than.
Two examples of Problem Solving in Programming H. Chad Lane University of Pittsburgh CS7: Introduction to Programming.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 02/07/12
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Permutations. Motivation Shuffling – Games – Music players Brute-force algorithms.
Games of Chance Introduction to Artificial Intelligence COS302 Michael L. Littman Fall 2001.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
DAST 2005 Tirgul 6 Heaps Induction. DAST 2005 Heaps A binary heap is a nearly complete binary tree stored in an array object In a max heap, the value.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Hamming Codes 11/17/04. History In the late 1940’s Richard Hamming recognized that the further evolution of computers required greater reliability, in.
The Fundamentals: Algorithms, the Integers & Matrices.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
If statements while loop for loop
White-box Testing.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Greedy Algorithms. Surprisingly, many important and practical computational problems can be solved this way. Every two year old knows the greedy algorithm.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Chapter 4 Conditional Statements. A Programmer's Lament I really hate this damned machine; I wish that they would sell it It never does quite what I want.
Part 3 Linear Programming
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Multiples and Factors. Multiples A multiple is a number that is in the times tables. A multiple is a number that is in the times tables. Multiples of.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
PREPARED BY: Qurat Ul Ain SUBMITTED TO: Ma’am Samreen.
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
Applied Discrete Mathematics Week 2: Functions and Sequences
Great Theoretical Ideas in Computer Science
CS150 Introduction to Computer Science 1
Enough Mathematical Appetizers!
Week 8 - Programming II Today – more features: Loop control
Linear and Binary Search
Algorithms Chapter 3 With Question/Answer Animations
NIM - a two person game n objects are in one pile
CSE 2010: Algorithms and Data Structures Algorithms
CS150 Introduction to Computer Science 1
Console.WriteLine(“Good luck!”);
Each player can remove 1 or 2 pegs. Player who removes the last peg wins
Try starting with fewer objects to investigate.
Structure diagrams for lab 13
Starter Look at the hand-out.
CS51A David Kauchak Spring 2019
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

Pippin’s Garden Problem There are many solutions depending how efficient you want to be. Generate all possible locations (x,y) for the lower left corner, and generate increasing sizes s until something goes wrong: –The square contains a point –The square goes outside the outer rectangle Report the largest square found. x y s s

Pippin’s Garden Problem Pseudo code: input width, height and points; maxSize = 0; // saves maximum square size so far for x = 0 to width-1 { for y = 0 to height-1 { // (x,y) = lower left corner of square okay = true; s = 0; // holds size of the square while (okay) { // while square is still valid s = s+1;// increment square size if ((x+s > width) or (y+s > height)) okay = false; for i = 0 to numberOfPoints-1 if (point[i] is contained within (x,y)..(x+s,y+s)) okay = false; } if (s > maxSize) { maxSize = s; save (x,y,s); } } } output saved square: (x, x+s, y, y+s)

Pippin’s Garden Problem Enhancements: Early loop termination: Exit the while loop as soon as okay = false; Limit x and y values: Observe that the choices for x and y can be restricted to the (distinct) x- and y-coordinates of the input points (and 0). Limit s values: Rather than testing the size s by a linear search, use a binary search instead. Implementing all these enhancements leads to an O(n 2 log n) time solution, where n is the number of points. There is an O(n log n) solution based on Voronoi diagrams. But this is quite hard.

The Game of Rings Rules: Three piles of rings. Players take turns removing some numbers of stones from one or more piles. Player who takes the last stone(s) wins. Game State: The state of the game is determined by: –The numbers of stones in each pile: (i, j, k) There are at most = 1,000,000 different states. Winning Strategy: Since no draws or randomness involved, the result is fully determined from the state. Our encoding: S[i, j, k] = Wif current player can force a win S[i, j, k] = Lotherwise Solution: Construct the entire S table. Given the initial numbers of stones (a,b,c), if S[a,b,c] = W then Bilbo wins else Frodo wins.

The Game of Rings Examples: S[0,0,0] = L (current player loses when stones are gone) S[1,0,0] = W (by removing last stone from pile 1 we win) S[0,1,0] = W (by removing last stone from pile 2 we win) S[0,2,0] = W (by removing last 2 stones from pile 2 we win) S[1,2,0] = L (every move leads to W state for opponent) State Transition: If any move leads to an “L”, then this state is “W” If all moves lead to “W”, then this state is “L” 0,0,0:L 1,0,0:W 0,1,0:W 1 from [2] 1 from [1] 0,2,0:W 2 from [2] 1,2,0:L 1 from each 1,1,0:W 1 from each 1 from [1] 2 from [2] 1 from [2]

The Game of Rings Pseudo code: input number of stones per pile  (a, b, c) for i = 0 to a for j = 0 to b for k = 0 to c if (i == j == k == 0)S[i, j, k] = L else if (S[i-1, j, k] == L)S[i, j, k] = W else if (S[i, j-1, k] == L)S[i, j, k] = W else if (S[i, j-2, k] == L)S[i, j, k] = W else if (S[i, j, k-1] == L)S[i, j, k] = W else if (S[i, j, k-2] == L)S[i, j, k] = W else if (S[i, j, k-3] == L)S[i, j, k] = W else if (S[max(0,i-1), max(0, j-1), max(0,k-1)] == L) S[i, j, k] = W elseS[i, j, k] = L if (S[a,b,c] == W) output “Bilbo wins” else output “Frodo wins” Design a “smart” subscripting operator that returns “L” if subscripts are negative.