深度优先搜索(DFS)
背包问题
有 n 件物品,每件物品的重量为 w[i], 价值为
c[i]。现在需要选出若干件物品放入一个容量为 V
的背包中,使得在选入背包的物品重量和不超过容量 V
的前提下,让背包中物品的价值之和最大,求最大价值。(1<= n <=
20)
输入数据:
1 2 3
   | 5 8  3 5 1 2 2  4 5 2 1 3 
 
  | 
 
输出数据:
实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | # include <cstido> const int maxn = 30; int n, V, maxValue = 0;  int w[maxn], c[maxn]; 
 
  void DFS(int index, int sumW, int sumC){     if(index == n){          if(sumW <= V && sumC > maxValue){             maxValue = sumC;          }         return;     }          DFS(index + 1, sumW, sumC);      DFS(index + 1, sumW + w[index], sumC + c[index]);  } int main(){     scanf("%d%d", &n, &V);     for(int i = 0; i < n; i++){         scanf("%d", &w[i]);      }     for(int i = 0; i < n; i++){         scanf("%d", &c[i]);      }     DFS(0, 0, 0);      printf("%d\n", maxValue);     return 0; }
 
  | 
 
广度优先搜索(BFS)
给出一个 m * n 的矩阵,矩阵中的元素为 0 或 1。称位置 (x, y)
与其上下左右四个位置 (x, y + 1)、(x, y - 1)、(x + 1, y)、(x - 1, y)
是相邻的。如果矩阵中有若干个 1 是相邻的(不必两两相邻),那么称这些 1
构成了一个 “块”。求给定的矩阵中 “块” 的个数。
1 2 3 4 5 6
   | 0 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 1 1 1 0 1 0 0 1 1 1 1 0 0 0
 
  | 
 
上面的 6 * 7 的矩阵中,“块”的个数为 4。
实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   | # include <cstido> # include <queue> using namespace std; const int maxn = 100; struct node{     int x, y; } Node;
  int n, m; int matrix[maxn][maxn]; bool inq[maxn][maxn] = {false}; int X[4] = {0, 0, 1, -1}; int Y[4] = {1, -1, 0, 0};
  bool judge(int x, int y){     if(x >= n || x < 0 || y >= m || y < 0) return false;          if(matrix[x][y] == 0 || inq[x][y] == true) return false;          return true; }
  void BFS(int x, int y){     queue<node> Q;     Node.x = x, Node.y = y;     Q.push(Node);      inq[x][y] = true;      while(!Q.empty()){         node top = Q.front();          Q.pop();          for(int i = 0; i < 4; i++){              int newX = top.x + X[i];             int newY = top.y + Y[i];             if(judge(newX, newY)){                                   Node.x = newX, Node.y = newY;                 Q.push(Node);                  inq[newX][newY] = true;              }         }     } } int main(){     scanf("%d%d", &n, &m);     for(int x = 0; x < n; x++){         for(int y = 0; y < m; y++){             scanf("%d", &matrix[x][y]);          }     }     int ans = 0;      for(int x = 0; x < n; x++){          for(int y = 0; y < m; y++){             if(matrix[x][y] == 1 && inq[x][y] == false){                 ans++;                  BFS(x, y);              }         }     }     printf("%d\n", ans);     return 0; }
 
  |