computer science club the best thing about a boolean is even if you are wrong, you are only off by a...
TRANSCRIPT
![Page 1: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/1.jpg)
Computer Science Club
The best thing about a boolean is even if you are wrong, you are only off by a bit.
![Page 2: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/2.jpg)
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)
![Page 3: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/3.jpg)
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.
![Page 4: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/4.jpg)
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; }
![Page 5: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/5.jpg)
Code for Flood Fill (cont.)
int[] mx={-1,-1,-1,0,0,1,1,1}; // movement arraysint[] 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}
![Page 6: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/6.jpg)
Dijkstra’s AlgorithmUsed 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?
![Page 7: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/7.jpg)
3 2
Dijkstra’s cont… The StepsStep 1: Each node has a distance value. (a = 0, others = ∞)Step 2: Two node sets: visited and unvisited (init: none visited)Step 3: a = curNodeStep 4: (recursive step!)- Update unvisited neighbors of curNode with new shortest dist- move curNode to visited set- new curNode = smallest unvisited node
![Page 8: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/8.jpg)
3 2
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.Consider…Why don’t we need to re-check visited nodes? Why can’t there be a shorter path to a visited node?
![Page 9: Computer Science Club The best thing about a boolean is even if you are wrong, you are only off by a bit](https://reader035.vdocuments.mx/reader035/viewer/2022081907/5513bf935503465b298b48db/html5/thumbnails/9.jpg)
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