diff --git a/01-knapsack.cpp b/01-knapsack.cpp new file mode 100644 index 0000000..ae3dcde --- /dev/null +++ b/01-knapsack.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +const int N = 5; // number of items +const int C = 10; // capacity of bag +int w[N + 1] = {0, 2, 2, 6, 5, 4}; +int v[N + 1] = {0, 6, 3, 5, 4, 6}; +int f[N + 1][C + 1] = {{0}}; +int ff[N + 1] = {0}; + +/* +recurrence equation: +f(i,j) = max{f(i - 1,j - w[i]) + v[i](j>=w[i]), f(i - 1,j)} +*/ +int Knapsack() +{ + for(int i = 1; i <= N; i++) + { + for(int j = 1; j <= C; j++) + { + if(j >= w[i]) + f[i][j] = max(f[i-1][j],f[i-1][j-w[i]] + v[i]); + else + f[i][j] = f[i-1][j]; + } + } + return f[N][C]; +} + +/* +space optimization +recurrence equation: +f(j) = max{f(j - w[i]) + v[i](j>=w[i]), f(j)} +*/ + +int Knapsack_SpaceOpt() +{ + for(int i = 1; i <= N; i++) + { + for(int j = C; j >= w[i]; j--) //set low bound to be w[i] + { + if(j >= w[i]) + ff[j] = max(ff[j], ff[j - w[i] + v[i]]); + else + ff[j] = ff[j]; + } + } + return f[N][C]; +} + +int main() +{ + cout << Knapsack() << endl; + cout << Knapsack_SpaceOpt() << endl; + return 0; +} \ No newline at end of file diff --git a/btsearch.cpp b/btsearch.cpp deleted file mode 100644 index f908d6f..0000000 --- a/btsearch.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -using namespace std; - -int index = 0; - -typedef struct Node -{ - char data; - struct Node *lchild; - struct Node *rchild; -}* Tree; - -void treeConstructor(Tree &node, char data[]) -{ - char e = data[index++]; - if(e == '#') - node = nullptr; - else - { - node = new Node; - node->data = e; - treeConstructor(node->lchild, data); - treeConstructor(node->rchild, data); - } -} - -void DFS(Tree root) -{ - stack nodeStack; - nodeStack.push(root); - Node *node; - while(!nodeStack.empty()) - { - node = nodeStack.top(); - cout << node->data; - nodeStack.pop(); - if(node->rchild) - nodeStack.push(node->rchild); - if(node->lchild) - nodeStack.push(node->lchild); - } -} - -void BFS(Tree root) -{ - queuenodeQueue; - nodeQueue.push(root); - Node *node; - while(!nodeQueue.empty()) - { - node = nodeQueue.front(); - cout << node->data; - nodeQueue.pop(); - if(node->lchild) - nodeQueue.push(node->lchild); - if(node->rchild) - nodeQueue.push(node->rchild); - } -} - -int main() -{ - char data[16] = {'A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F', '#', '#', 'G', '#', '#', '\0'}; - Tree root; - treeConstructor(root, data); - cout << "DFS: "; - DFS(root); - cout << endl; - cout << "DFS: "; - BFS(root); - cout << endl; - - return 0; -} diff --git a/eight_queens.cpp b/eight_queens.cpp new file mode 100644 index 0000000..97aac26 --- /dev/null +++ b/eight_queens.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +int a[8],n = 8,i,j,t=1; + +//参考:http://blog.csdn.net/bone_ace/article/details/41419695 + +//Check函数检验第n行的皇后是否和之前的皇后有冲突,没有冲突返回1 + +/*约束条件: +1.不在同一列:a[n] != a[i] +2.不在同一行:因为现在是每一行求一个皇后的位置,所以同一行不会有冲突,不需要考虑。 +3.不在同一对左角线:a[n]-a[i] != n-i +4.不在同一右对角线:a[n]-a[i] != -(n-i) +条件3和4可以合成:abs(a[n]-a[i]) != abs(n-i) +*/ +int Check(int a[],int n) +{ + for(int i=1;i -#include -#include -using namespace std; - -const int vnum = 5; -int visited[vnum]; - -struct Graph -{ - int vertex[vnum]; - int arc[vnum][vnum]; -}; - -void CreatGraph(Graph &G, int data[], int maxtrix[][vnum]) -{ - for(int i = 0; i < vnum; i++) - G.vertex[i] = data[i]; - for(int i = 0; i < vnum; i++) - for(int j = 0; j < vnum; j++) - G.arc[i][j] = maxtrix[i][j]; -} - -void DFS(Graph *G, int v) -{ - if(!visited[v]) - { - cout << G->vertex[v]; - visited[v] = 1; - } - for(int i = 0; i < vnum; i++) - if(G->arc[v][i] && visited[i] != 1) - DFS(G, i); -} - -void BFS(Graph *G, int v) -{ - int f; - queue q; - if(!visited[v]) - { - cout << G->vertex[v]; - visited[v] = 1; - q.push(v); - } - while(!q.empty()) - { - f = q.front(); - q.pop(); - for(int i = 0; i < vnum; i++) - if(G->arc[f][i] && visited[i] != 1) - { - cout << G->vertex[i]; - visited[i] = 1; - q.push(i); - } - } -} - -int main() -{ - Graph *G = new Graph; - - int data[vnum] = {1, 2, 3, 4, 5}; - int arcMatrix[vnum][vnum] = - { - {0, 1, 1, 0, 0}, - {1, 0, 0, 1, 0}, - {1, 0, 0, 1, 1}, - {0, 1, 1, 0, 1}, - {0, 0, 1, 1, 0}, - }; - - CreatGraph(*G, data, arcMatrix); - cout << "vertex: "; - for(int i = 0; i < vnum; i++) - cout << G->vertex[i]; - cout << endl << "arc: " << endl; - for(int i = 0; i < vnum; i++) - { - cout << " "; - for(int j = 0; j < vnum; j++) - cout << G->arc[i][j]; - cout << endl; - } - cout << endl; - - cout << "DFS: " << endl; - DFS(G, 0); - cout << endl; - - memset(visited, 0, sizeof(visited)); - - cout << "BFS: " << endl; - BFS(G, 0); - cout << endl; - - return 0; -}