# Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit.

## Presentation on theme: "Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit."— Presentation transcript:

Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit.

USACO Its over! You can find analysis and test data at ace.delos.com/OCT10B.htm (or 10S, 10G) If you missed it, go take it next weekend Nov.5-8 You can still qualify for the next division by getting ~85% Note that Gold #1 was almost the same as the problem of the week… (the knapsack one)

USACO Silver #2 Count the number of lakes Recursive flood-fill (also known as dfs or depth first search) Begin the recursion at each unvisited point Mark points visited as you visit them At each step, continue the recursion to each unvisited neighbor Each time you restart the recursion, you have found a new lake W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW. W.W.W.....W..W.W......W...W.......W.

Code for Flood Fill int countlakes() { int groupc=0; for(int x=0;x<numX;x++) for(int y=0;y<numY;y++) if(!vis[x][y]) // not visited yet { dfs(x,y); // start recursion groupc++; // increment count } return groupc; }

Code for Flood Fill (cont.) int[] mx={-1,-1,-1,0,0,1,1,1}; // movement arrays int[] my={-1,0,1,-1,1,-1,0,1}; // save a lot of typing! void dfs(int x,int y) { if(x =numX || y =numY || vis[x][y]) return; // out of bounds or already visited vis[x][y]=true; // mark as visited for(int m=0;m<8;m++) // go through movement array dfs(x+mx[m],y+my[m]); // recurse }

Dijkstras Algorithm Used to find the shortest path between a starting position and destination. Given a graph with specified distances of the directional paths between nodes: Task: Find the shortest path from Node a (initial node) to Node f (destination). For example, A-C-D-F has a distance of 3 + 10 + 2 = 15. The path A-C-E-F has a distance of 3 + 3 + 5 = 11. Is there a path even SHORTER than that? Can you be sure your path is the shortest possible?

3 2 Dijkstras cont… The Steps Step 1: Each node has a distance value. (a = 0, others = ) Step 2: Two node sets: visited and unvisited (init: none visited) Step 3: a = curNode Step 4: (recursive step!) - Update unvisited neighbors of curNode with new shortest dist - move curNode to visited set - new curNode = smallest unvisited node

3 2 Dijkstras cont…Finishing Up When curNode = destination, shortest path = value of final node. Try to trace the algorithm and find the shortest path and minimal distance. Consider… Why dont we need to re- check visited nodes? Why cant there be a shorter path to a visited node?

REMEMBER… The first real USACO round will be this weekend! Practice with the free USACO Training lessons: http://ace.delos.com/usacogate Sign in! Have fun coding

Similar presentations