1 / 9

Computer Science Club

Computer Science Club. The best thing about a boolean is even if you are wrong, you are only off by a bit. USACO. It’s 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

denzel
Download Presentation

Computer Science Club

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. USACO • It’s 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)

  3. USACO Silver #2 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. • 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

  4. 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; }

  5. 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<0 || x>=numX || y<0 || 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 }

  6. Dijkstra’s 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?

  7. Dijkstra’s 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 2 3

  8. Dijkstra’s 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. 2 3 Consider… Why don’t we need to re-check visited nodes? Why can’t there be a shorter path to a visited node?

  9. 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 

More Related