Advanced Programming Spring Week1
Advanced Programming Spring Week1
An Introduction
Paul Grigoras
[email protected]
About Me
First Attempt
Learning Outcome
Solve interesting problems
See
new tools
new languages
cool tricks
UKIEPC http://www.cs.nott.ac.uk/~mlw/ukiepc/2013/
ICPC Regionals - Northwestern Europe
ICPC World Finals
Online:
codeforces
topcoder
Competitive Programming@Imperial
Winter Contest @ Imperial
1st February - Sign Up NOW!
Why - Fun
Problem Solving
Complexity Analysis
Coding
Use APIs
How - Books
Competitive Programming, Steven Halim
Introduction to Algorithms, CLRS
The Algorithm Design Manual, Skiena
Who
Mainly Beginners
no competitive programming experience
But also
people that want coding interview-like practice
people that need a refresher before going back to
competitive programming
Overview
Implementation
This Week...
Complete Search
Week 2
Week 3
Week 4
Graphs
Weeks 5 & 6
Maths
Week 7
Geometry
Week 8
Languages
Haskell - cool, but
rarely available on OJs
Java - not so cool, but
available (well use it)
C++ - even better (but
you dont know it.. yet)
1 Implementation
Minesweeper
Problem statement
on UVA
*100
2210
1*10
1110
Minesweeper - Solution
Minesweeper - Solution
Pretty easy:
1. Keep a count matrix
2. Loop through the board
if we find a mine (*) at i, j
i. increment count for all non-mine adjacent fields
Minesweeper - Solution
Think before you write any code!
Is your solution correct?
Try it on the sample input and a few examples
Are there any EDGE CASES?
ICPC is all or nothing - one tiny edge case can
cost you the whole problem
For minesweeper - edges of the board
Minesweeper - Solution
Think before you write any code!
Is your solution fast enough?
Minesweeper - Solution
Think before you write any code!
Is your solution fast enough?
N
Complexity
Complexity
10
O(n!), O(n^6)
100
O(n^4)
15
O(2^n * n^2)
1000
O(n^2)
20
O(2^n), O(n^5)
100K
O(nlogn)
50
O(n^4)
1M
Minesweeper - Solution
Think before you write any code!
How long will it take to write it?
Teammates want to know that.
Essential for team contests:
can prioritize actions/problems based on estimates
quick problems first
debugging vs implementation
Minesweeper - Solution
Think before you write any code!
How long will it take to write it?
Teammates want to know that.
Essential for team contests:
can prioritize actions/problems based on estimates
ICPC ranking: first by score, then by penalty
Correct? Yes
Fast? O(n^2)
Yes
if we find a mine (*) at i, j
Time? ~10 mins
increment count for all non-mine Lets code it!
adjacent fields
Java I/O
Scanner - useful functions, slower
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
// OR
// double d = sc.nextDouble();
// BigInteger bi = sc.nextBigInteger();
}
Java I/O
BufferedReader - faster, no parsing
functions
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String line;
while ( (line = br.readLine()) != null) {
int n = Integer.parseInt(line);
}
// useful function for parsing: line.split(delimiter)
Minesweeper - Solution
for (int i = 0; i < n; i++) {
char[] line = sc.nextLine().toCharArray();
for (int j = 0; j < m; j++) {
if ( line[j] != '*' ) continue;
for (int l = i - 1; l <= i + 1
Minesweeper - Testing
Write tests for:
1. Any edge case
2. Very small inputs
3. Very large inputs, catch:
a. Array out of bounds
b. Overflow
Minesweeper - Testing
Generating large test cases - Python
import sys, random
MAX=100
print '{} {}'.format(MAX, MAX)
for i in range(MAX):
for j in range(MAX):
sys.stdout.write('.' if random.randint(0, 1) == 0 else '*')
print ''
print '0 0'