-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Committing all files for first Codebook draft.
- Loading branch information
0 parents
commit 483d751
Showing
65 changed files
with
2,583 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
int grid[101][101]; | ||
int pre[101], low[101], parent[101]; | ||
|
||
void dfs(int v, int & cnt, int & crit, int n) | ||
{ | ||
pre[v]=low[v]=cnt++; | ||
int child=0; | ||
bool critical=false; | ||
for(int w=1; w<=n; w++) //This code uses 1-base. Change to 0-base as necessary. | ||
if(grid[v][w]) | ||
if(pre[w]==-1) | ||
{ | ||
child++; | ||
parent[w]=v; | ||
dfs(w, cnt, crit, n); | ||
if(low[w] < low[v]) | ||
low[v]=low[w]; | ||
//if any child's lowest preorder number referenced by a backedge is | ||
//greater than or equal to parent's preorder number, then that parent is | ||
//an articulation point | ||
if(low[w] >= pre[v] && !critical && pre[v]!=0) | ||
crit++, critical=true; | ||
} | ||
else if(parent[w]!=v && pre[w] < pre[v]) //A backedge | ||
if(pre[w] < low[v]) | ||
low[v]=pre[w]; | ||
if(pre[v]==0 && child>1) | ||
crit++; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//Directional arrays that specify the possible moves a Knight can do | ||
int di[] = {1, 1, -1, -1, 2, 2, -2, -2}; | ||
int dj[] = {2, -2, 2, -2, 1, -1, 1, -1}; | ||
|
||
//State describes the position and number of moves to get to that position | ||
struct state | ||
{ | ||
int i, j, d; | ||
state(int xx, int yy, int cc) | ||
{ | ||
i = xx; | ||
j = yy; | ||
d = cc; | ||
} | ||
bool valid() | ||
{ | ||
return i>=0 && i<8 && j>=0 && j<8; | ||
} | ||
state(){} | ||
}; | ||
|
||
//Seen array to avoid repetitions of positions on a chess board | ||
bool seen[8][8]; | ||
|
||
int bfs(int si, int sj, int ei, int ej) | ||
{ | ||
for(int i=0;i<8;i++) | ||
for(int j=0;j<8;j++) | ||
seen[i][j]=false; | ||
|
||
seen[si][sj]=true; | ||
state start= state(si, sj,0); | ||
|
||
queue<state> Q; | ||
Q.push( start); | ||
while(!Q.empty()) | ||
{ | ||
state curS = Q.front(); | ||
Q.pop(); | ||
|
||
int curi = curS.i; | ||
int curj = curS.j; | ||
int curd = curS.d; | ||
|
||
if(curi==ei && curj==ej) | ||
return curd; | ||
|
||
for(int d =0; d<8; d++) | ||
{ | ||
int newi = curi+di[d]; | ||
int newj = curj+dj[d]; | ||
int newd = curd+1; | ||
state ns = state(newi, newj, newd); | ||
if(ns.valid() && !seen[newi][newj]) | ||
{ | ||
seen[newi][newj] = true; | ||
Q.push(ns); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
public class BreadthFirstSearch | ||
{ | ||
//Testing code. Can be removed. | ||
public static void main(String[] args){ | ||
|
||
} | ||
|
||
static int bfs(char[][] g, int si, int sj) | ||
{ | ||
int[] di = {1, -1, 0, 0}; //directional arrays | ||
int[] dj = {0, 0, 1, -1}; | ||
|
||
Queue<State> q = new LinkedList<State>(); | ||
|
||
q.offer(new State(si, sj, 0)); | ||
g[si][sj] = 'X'; //don't forget to mark it as seen! | ||
|
||
while (!q.isEmpty()) | ||
{ | ||
State c = q.poll(); | ||
|
||
for (int k=0; k<4; k++) | ||
{ | ||
int ni = c.i+di[k]; | ||
int nj = c.j+dj[k]; | ||
|
||
if (ni>=0 && ni<g.length && nj>=0 && nj<g[0].length && g[ni][nj]!='X') | ||
{ | ||
if (g[ni][nj]=='*') return c.dist+1; | ||
|
||
q.offer(new State(ni, nj, c.dist+1)); | ||
g[ni][nj] = 'X'; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
|
||
class State //helper class | ||
{ | ||
int i, j, dist; | ||
State(int a, int b, int c) | ||
{ | ||
i = a; | ||
j = b; | ||
dist = c; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
struct Edge //denotes to an edge of weight w that goes from x to y | ||
{ | ||
int u, v, w; | ||
Edge() {} | ||
Edge(int a, int b, int c) | ||
{ u = a; v = b; w = c; } | ||
}; | ||
#define MAX_E 2048 | ||
#define MAX_V 1024 | ||
Edge graph[ MAX_E ]; | ||
int d[ MAX_V ]; | ||
int pi[ MAX_V ]; | ||
bool bellman_ford(int n, int m, int source) | ||
{ | ||
fill(d, d+n, 1000000000); //fill with +inf | ||
fill(pi, pi+n, -1000000000); //predecessor array | ||
//initial conditions | ||
d[source] = 0; | ||
pi[source] = -1; //root | ||
for (int i=0; i < n-1; i++) | ||
{ | ||
bool relaxed = false; //this is a little optimization I added | ||
for (int j=0; j<m; j++) | ||
if (d[ graph[j].v ] > d[ graph[j].u ] + graph[j].w) //relaxation step | ||
{ | ||
d[ graph[j].v ] = d[ graph[j].u ] + graph[j].w; //found a better path | ||
pi[ graph[j].v ] = graph[j].u; | ||
relaxed = true; | ||
} | ||
if (!relaxed) break; //will break early if it finishes early | ||
} | ||
for (int i=0; i<m; i++) //check for negative cycle | ||
if (d[ graph[i].v ] > d[ graph[i].u ] + graph[i].w) | ||
return false; | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
vector <int> list[1001]; | ||
int pre[1001], low[1001], parent[1001]; | ||
|
||
void dfs(int v, int & cnt) | ||
{ | ||
pre[v]=cnt++; | ||
low[v]=pre[v]; | ||
for(int i=0; i<list[v].size(); i++) | ||
{ | ||
int w=list[v][i]; | ||
if(pre[w]==-1) //an unvisisted edge from v to w | ||
{ | ||
parent[w]=v; | ||
printf("%d %d\n", v, w); | ||
dfs(w, cnt); | ||
if(low[w]<low[v]) | ||
low[v]=low[w]; | ||
if(low[w]==pre[w]) //must be a bridge | ||
printf("%d %d\n", w, v); | ||
} | ||
else if(parent[v]!=w && pre[w]<pre[v]) //a back link from v to w | ||
{ | ||
printf("%d %d\n", v, w); | ||
if(low[w]<low[v]) | ||
low[v]=low[w]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <vector> | ||
#include <list> | ||
#include <map> | ||
#include <set> | ||
#include <deque> | ||
#include <queue> | ||
#include <stack> | ||
#include <bitset> | ||
#include <algorithm> | ||
#include <functional> | ||
#include <numeric> | ||
#include <utility> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <cstdio> | ||
#include <cmath> | ||
#include <cstdlib> | ||
#include <ctime> | ||
#include <cctype> | ||
#include <string> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Circle | ||
{ | ||
Point c; | ||
double r; | ||
ArrayList<Point> inside; | ||
|
||
Circle(double x, double y, double z) | ||
{ | ||
c = new Point(x, y); | ||
r = z; | ||
inside = new ArrayList<Point>(); | ||
} | ||
|
||
Circle(Point a, double b) | ||
{ | ||
c = a; | ||
r = b; | ||
inside = new ArrayList<Point>(); | ||
inside.add(a); | ||
} | ||
|
||
Circle(Point a, Point b) | ||
{ | ||
c = a.plus(b).divide(2); | ||
r = c.dist(a)+.1; | ||
inside = new ArrayList<Point>(); | ||
} | ||
|
||
Circle(Point x, Point y, Point z) | ||
{ | ||
Point m = x.plus(y).divide(2); | ||
Point n = y.minus(x).rot90().unit(); | ||
Point xm = m.minus(x); | ||
Point zm = m.minus(z); | ||
double t = -(xm.dot(xm) - zm.dot(zm)) / (2 * (xm.dot(n) - zm.dot(n))); | ||
c = m.plus(n.times(t)); | ||
r = x.dist(c)+.1 | ||
inside = new ArrayList<Point>(); | ||
} | ||
|
||
void add(Point a) | ||
{ inside.add(a); } | ||
|
||
boolean contains(Point b) | ||
{ return c.minus(b).norm()<=r*r; } | ||
|
||
double area() | ||
{ return Math.PI*r*r; } | ||
|
||
public String toString() | ||
{ return c+" r = "+r; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
struct Point | ||
{ | ||
int x, y; | ||
Point() {} | ||
Point(int a, int b) | ||
{ x = a; y = b; } | ||
|
||
bool operator < (const Point & a) const | ||
{ | ||
if (x==a.x) return y<a.y; | ||
else return x<a.x; | ||
} | ||
}; | ||
|
||
#define MAX_P 1000 | ||
Point set_p[MAX_P]; //The points should be stored here | ||
//left turn: AB X AC>0, right turn: AB X AC < 0. Inclusive will detect colinear points | ||
bool left_turn(Point a, Point b, Point c) | ||
{ return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y) >= 0; } | ||
|
||
//Computes the convex hull of a sorted set of n points (set_p) | ||
vector<Point> convexHull(int n) | ||
{ | ||
sort(set_p, set_p+n); //Points must be sorted for this to work. | ||
vector<Point> upper, lower; //upper & lower hulls | ||
|
||
//initial states | ||
upper.push_back(set_p[0]); | ||
upper.push_back(set_p[1]); | ||
lower.push_back(set_p[n-1]); | ||
lower.push_back(set_p[n-2]); | ||
|
||
for (int i=2; i<n; i++) | ||
{ | ||
upper.push_back(set_p[i]); | ||
lower.push_back(set_p[n-1-i]); | ||
int s1 = upper.size(), s2 = lower.size(); | ||
while (s1>=3 && left_turn(upper[s1-3], upper[s1-2], upper[s1-1])) | ||
upper.erase(upper.begin() + s1-- -2); //update size | ||
while (s2>=3 && left_turn(lower[s2-3], lower[s2-2], lower[s2-1])) | ||
lower.erase(lower.begin() + s2-- -2); //update size | ||
} | ||
for (int i=1; i+1<lower.size(); i++) //combine hulls, without overlap | ||
upper.push_back(lower[i]); | ||
return upper; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <vector> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <string> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
bool seen[400][600]; | ||
|
||
int dfs(int i, int j) | ||
{ | ||
if( i>=400 || i<0 || j>=600 || j<0 || seen[i][j]) return 0; | ||
seen[i][j] = true; | ||
int A=1; | ||
A+=dfs(i-1, j); | ||
A+=dfs(i+1, j); | ||
A+=dfs(i, j-1); | ||
A+=dfs(i, j+1); | ||
return A; | ||
} | ||
|
||
vector <int> sortedAreas(vector <string> recs) { | ||
vector<int> areas; | ||
for(int i=0;i<recs.size();i++) | ||
{ | ||
int i1, i2, j1, j2; | ||
stringstream ss(recs[i]); | ||
ss>>i1>>j1>>i2>>j2; | ||
|
||
for(int ii=i1;ii<=i2;ii++) | ||
for(int jj=j1;jj<=j2;jj++) | ||
seen[ii][jj]=true; | ||
} | ||
|
||
for(int i=0;i<400;i++) | ||
for(int j=0;j<600;j++) | ||
if(!seen[i][j]) | ||
areas.push_back(dfs(i, j)); | ||
|
||
sort(areas.begin(), areas.end()); | ||
return areas; | ||
} |
Oops, something went wrong.