/**
 * 算法分析教学平台 - 章节与代码文件数据
 * 对应教材章节：基础(复杂度分析)、分治法、动态规划、贪心、回溯、分支限界
 */

export const chapters = [
  {
    id: 'ch1',
    title: '第一章 基础 — 复杂度分析',
    subtitle: '算法时间复杂度与空间复杂度基础',
    description: '本章介绍算法分析的基础知识，包括时间复杂度、空间复杂度的概念，以及通过实验比较不同算法的性能差异。',
    icon: '📊',
    topics: [
      '时间复杂度概念（O、Ω、Θ）',
      '空间复杂度分析',
      '递归算法复杂度',
      '实验对比分析方法'
    ],
    files: [
      { path: 'c/ch1/first.c', name: 'first.c', label: '递归打印图案', description: '递归打印矩形/三角形图案，演示递归基本结构', pyPath: 'py/ch1/first.py' },
      { path: 'c/ch1/compare.c', name: 'compare.c', label: '性能对比（累加 vs 高斯公式）', description: '比较循环累加与高斯求和公式的耗时差异，直观感受算法效率', pyPath: 'py/ch1/compare.py' },
      { path: 'c/ch1/compareall.c', name: 'compareall.c', label: '多项性能对比', description: '更多累加与高斯公式的计时对比实验' }
    ]
  },
  {
    id: 'ch2',
    title: '第二章 分治法',
    subtitle: 'Divide and Conquer — 分而治之',
    description: '分治法将大问题分解为若干规模较小的子问题，递归求解后再合并结果。本章涵盖归并排序、快速排序、二分查找、大整数乘法、矩阵乘法等经典分治算法。',
    icon: '🔪',
    topics: [
      '分治策略基本思想',
      '归并排序',
      '快速排序（多种划分方式）',
      '二分查找（递归与迭代）',
      '大整数乘法',
      'Strassen矩阵乘法',
      '递归求最大值',
      '全排列生成'
    ],
    subfolders: [
      {
        name: 'mergesort',
        label: '归并排序',
        files: [
          { path: 'c/ch2/mergesort/guibing.c', name: 'guibing.c', label: '归并排序完整实现', description: '分治归并排序，包含 merge 与 mergeSort 函数', pyPath: 'py/ch2/mergesort/mergesort.py' },
          { path: 'c/ch2/mergesort/merge.c', name: 'merge.c', label: '归并操作', description: '合并两个有序子数组的核心 merge 操作' }
        ],
        demo: { path: '/sort-demo#merge', label: '🎬 动态演示', description: '观看归并排序的完整动画过程' }
      },
      {
        name: 'quicksort',
        label: '快速排序',
        files: [
          { path: 'c/ch2/quicksort/danppt.c', name: 'danppt.c', label: '快速排序（单侧指针）', description: '单侧指针遍历划分的快速排序实现', pyPath: 'py/ch2/quicksort/quicksort.py' },
          { path: 'c/ch2/quicksort/danleft.c', name: 'danleft.c', label: '快速排序（左指针法）', description: '基于左右指针移动的快速排序变体' },
          { path: 'c/ch2/quicksort/dantwo.c', name: 'dantwo.c', label: '快速排序（双指针法）', description: '另一种双指针快速排序实现' },
          { path: 'c/ch2/quicksort/shuangbian.c', name: 'shuangbian.c', label: '快速排序（双边扫描）', description: '经典的双边扫描分区快速排序' }
        ],
        demo: { path: '/sort-demo#quick', label: '🎬 动态演示', description: '观看快速排序的完整动画过程' }
      },
      {
        name: 'halfsearch',
        label: '二分查找',
        files: [
          { path: 'c/ch2/student/halfsearch.c', name: 'halfsearch.c', label: '二分查找（递归版）', description: '递归实现的二分查找算法', pyPath: 'py/ch2/halfsearch/binary_search.py' },
          { path: 'c/ch2/student/halfsearchnew.c', name: 'halfsearchnew.c', label: '二分查找（迭代版）', description: '迭代实现的二分查找算法' },
          { path: 'c/ch2/halfsearch/fenzhi.c', name: 'fenzhi.c', label: '分治分割示例', description: '二分分治分割示例' },
          { path: 'c/ch2/halfsearch/fenzhirec.c', name: 'fenzhirec.c', label: '递归查找', description: '递归结构的查找实现' }
        ]
      },
      {
        name: 'bigcheng',
        label: '大整数乘法',
        files: [
          { path: 'c/ch2/bigcheng/bigchengold.c', name: 'bigchengold.c', label: '大整数乘法（基础版）', description: '低位进位数组模拟乘法' },
          { path: 'c/ch2/bigcheng/bigchengnew.c', name: 'bigchengnew.c', label: '大整数乘法（分治版）', description: '分治递归的大整数乘法实现' }
        ]
      },
      {
        name: 'matrix',
        label: '矩阵乘法',
        files: [
          { path: 'c/ch2/juzhen/juzhenold.c', name: 'juzhenold.c', label: '矩阵乘法（朴素版）', description: '朴素三层循环矩阵乘法' },
          { path: 'c/ch2/juzhen/juzhennew.c', name: 'juzhennew.c', label: '矩阵乘法（分治版）', description: '分治/Strassen风格的矩阵乘法' },
          { path: 'c/ch2/matrix/macheng.c', name: 'macheng.c', label: '分治矩阵乘法', description: '分块递归矩阵乘法' }
        ]
      },
      {
        name: 'digui',
        label: '递归示例',
        files: [
          { path: 'c/ch2/digui/printnumber.c', name: 'printnumber.c', label: '递归打印整数', description: '递归按位打印整数（高位到低位）', pyPath: 'py/ch2/digui/print_number.py' },
          { path: 'c/ch2/digui/tuxing.c', name: 'tuxing.c', label: '递归图形', description: '递归绘制图形示例' },
          { path: 'c/ch2/shangji/findarrmax.c', name: 'findarrmax.c', label: '递归求最大值', description: '递归查找数组最大值' },
          { path: 'c/ch2/shangji/sanjiao.c', name: 'sanjiao.c', label: '递归三角形', description: '递归打印星号三角形' }
        ]
      },
      {
        name: 'maopao',
        label: '冒泡排序',
        files: [
          { path: 'c/ch2/maopao/mp.c', name: 'mp.c', label: '冒泡排序', description: '冒泡排序实现并打印中间状态' }
        ]
      },
      {
        name: 'allpai',
        label: '全排列',
        files: [
          { path: 'c/ch2/ch2.allpai/allpai.c', name: 'allpai.c', label: '全排列生成', description: '递归交换法全排列生成', pyPath: 'py/ch2/allpai/permutations.py' },
          { path: 'c/ch2/ch2.allpai/allpaichong.c', name: 'allpaichong.c', label: '全排列（去重）', description: '带去重逻辑的全排列生成' },
          { path: 'c/ch2/ch2.allpai/arrpaichong.c', name: 'arrpaichong.c', label: '数组排列去重', description: '数组排列生成与去重' },
          { path: 'c/ch2/student/quanpai.c', name: 'quanpai.c', label: '全排列（学生版）', description: '全排列/排列打印程序' },
          { path: 'c/ch2/student/ppp.c', name: 'ppp.c', label: '全排列（ppp）', description: '递归交换并打印排列' },
          { path: 'c/ch2/student/paichongright.c', name: 'paichongright.c', label: '去重排列', description: '排除重复输出的排列逻辑' }
        ]
      },
      {
        name: 'richeng',
        label: '日程表',
        files: [
          { path: 'c/ch2/richeng/bisan.c', name: 'bisan.c', label: '循环赛日程表', description: '使用2^k分组复制的赛程表逻辑' }
        ]
      }
    ]
  },
  {
    id: 'ch3',
    title: '第三章 动态规划',
    subtitle: 'Dynamic Programming — 最优子结构与重叠子问题',
    description: '动态规划通过将问题分解为重叠子问题，并利用最优子结构性质，自底向上求解。本章涵盖0/1背包、最长公共子序列(LCS)、矩阵链乘、图像压缩、最少硬币等经典DP问题。',
    icon: '🧩',
    topics: [
      '动态规划基本思想',
      '最优子结构与重叠子问题',
      '0/1背包问题',
      '最长公共子序列(LCS)',
      '矩阵链乘',
      '图像压缩',
      '最少硬币问题',
      '杨辉三角'
    ],
    subfolders: [
      {
        name: 'bag01',
        label: '0/1背包问题',
        files: [
          { path: 'c/ch3/bag01/bag01.c', name: 'bag01.c', label: '0/1背包 DP 实现', description: '经典二维DP矩阵解法', pyPath: 'py/ch3/bag01/knapsack01.py' },
          { path: 'c/ch3/bag01/bagbag.c', name: 'bagbag.c', label: '背包状态追踪', description: '多种背包实现/状态追踪' },
          { path: 'c/ch3/bag01/bagevery.c', name: 'bagevery.c', label: '背包枚举', description: '背包问题枚举/遍历示例' },
          { path: 'c/ch3/bag01/yanghui.c', name: 'yanghui.c', label: '杨辉三角（二维数组）', description: '使用二维数组打印杨辉三角' },
          { path: 'c/ch3/bag01/yanghuiarr.c', name: 'yanghuiarr.c', label: '杨辉三角（动态分配）', description: '用malloc动态分配版杨辉三角' }
        ]
      },
      {
        name: 'lcs',
        label: '最长公共子序列(LCS)',
        files: [
          { path: 'c/ch3/lcs/lcs1.c', name: 'lcs1.c', label: 'LCS 实现（一）', description: '构建LCS DP表', pyPath: 'py/ch3/lcs/lcs.py' },
          { path: 'c/ch3/lcs/lcs2.c', name: 'lcs2.c', label: 'LCS 实现（二）', description: 'LCS动态规划实现，返回长度' },
          { path: 'c/ch3/lcs/printtable.c', name: 'printtable.c', label: 'LCS 路径回溯', description: '打印LCS路径表并回溯输出LCS' }
        ]
      },
      {
        name: 'matrix',
        label: '矩阵算法',
        files: [
          { path: 'c/ch3/matrix/chengJia.c', name: 'chengJia.c', label: '矩阵乘法', description: '朴素矩阵乘法示例' },
          { path: 'c/ch3/matrix/matrixmul.c', name: 'matrixmul.c', label: '矩阵乘法（标准）', description: '标准矩阵乘法实现' },
          { path: 'c/ch3/matrix/duijiaoxian.c', name: 'duijiaoxian.c', label: '对角线处理', description: '矩阵对角线特性打印' },
          { path: 'c/ch3/matrix/kuohao.c', name: 'kuohao.c', label: '矩阵括号链乘', description: '矩阵链乘括号化DP实现' },
          { path: 'c/ch3/matrix/weishu.c', name: 'weishu.c', label: '位数计算', description: '数字位数/二进制位宽计算' }
        ]
      },
      {
        name: 'image',
        label: '图像压缩',
        files: [
          { path: 'c/ch3/image/imgcompress.c', name: 'imgcompress.c', label: '图像压缩算法', description: '图像压缩段划分与最优分段' },
          { path: 'c/ch3/image/imgcompress0.c', name: 'imgcompress0.c', label: '图像压缩（基础版）', description: '图像压缩段编码示例' },
          { path: 'c/ch3/image/get2Len.c', name: 'get2Len.c', label: '位长计算', description: '计算二进制位长等辅助函数' }
        ]
      },
      {
        name: 'homework',
        label: '课后练习',
        files: [
          { path: 'c/ch3/homework/lesscoin.c', name: 'lesscoin.c', label: '最少硬币问题', description: '典型背包/零钱兑换DP实现' }
        ]
      }
    ]
  },
  {
    id: 'ch4',
    title: '第四章 贪心算法',
    subtitle: 'Greedy Algorithm — 局部最优与全局最优',
    description: '贪心算法在每一步选择中都采取当前最优的选择，希望最终得到全局最优解。本章涵盖活动选择、Huffman编码、背包问题（贪心版）、最优装载、最短路径、找零问题等。',
    icon: '🎯',
    topics: [
      '贪心策略基本思想',
      '活动选择问题',
      'Huffman编码',
      '贪心背包（部分背包）',
      '最优装载问题',
      '最短路径',
      '找零问题'
    ],
    subfolders: [
      {
        name: 'huodong',
        label: '活动选择',
        files: [
          { path: 'c/ch4/huodong/fenpei.c', name: 'fenpei.c', label: '活动分配', description: '活动选择问题的贪心实现', pyPath: 'py/ch4/huodong/activity_selection.py' }
        ]
      },
      {
        name: 'huffman',
        label: 'Huffman编码',
        files: [
          { path: 'c/ch4/huffman/halfall.c', name: 'halfall.c', label: 'Huffman编码主流程', description: '构建Huffman树并生成编码', pyPath: 'py/ch4/huffman/huffman.py' },
          { path: 'c/ch4/huffman/halfcode.c', name: 'halfcode.c', label: 'Huffman编码生成', description: '从Huffman树回溯得到编码' },
          { path: 'c/ch4/huffman/halftree.c', name: 'halftree.c', label: 'Huffman树构建', description: '节点创建、合并、排序构建Huffman树' },
          { path: 'c/ch4/huffman/treelist.c', name: 'treelist.c', label: 'Huffman节点管理', description: '树节点创建和列表操作' }
        ]
      },
      {
        name: 'bag',
        label: '贪心背包',
        files: [
          { path: 'c/ch4/bag/tanbag.c', name: 'tanbag.c', label: '贪心背包', description: '部分背包问题的贪心解法' },
          { path: 'c/ch4/bag/tanbagtest.c', name: 'tanbagtest.c', label: '贪心背包测试', description: '贪心背包的测试/驱动' }
        ]
      },
      {
        name: 'bestload',
        label: '最优装载',
        files: [
          { path: 'c/ch4/bestload/loading.c', name: 'loading.c', label: '最优装载问题', description: '装载问题的贪心实现' },
          { path: 'c/ch4/bestload/sortAttr.c', name: 'sortAttr.c', label: '排序辅助', description: '装载问题排序比较器' }
        ]
      },
      {
        name: 'money',
        label: '找零问题',
        files: [
          { path: 'c/ch4/money/getmoney.c', name: 'getmoney.c', label: '贪心找零', description: '按面额计算所需最少张数', pyPath: 'py/ch4/money/coin_change.py' },
          { path: 'c/ch4/money/moneytwowei.c', name: 'moneytwowei.c', label: '找零（二维）', description: '面额分解的另一版本' }
        ]
      },
      {
        name: 'shortest',
        label: '最短路径',
        files: [
          { path: 'c/ch4/shortest/path.c', name: 'path.c', label: '最短路径', description: '最短路径算法实现' },
          { path: 'c/ch4/shortest/shortpath.c', name: 'shortpath.c', label: '最短路径（分支界）', description: '分支界/分治的最短路径' }
        ]
      }
    ]
  },
  {
    id: 'ch5',
    title: '第五章 回溯法',
    subtitle: 'Backtracking — 搜索与剪枝',
    description: '回溯法通过深度优先搜索所有可能的解，并在搜索过程中用剪枝函数避免无效搜索。本章涵盖0/1背包的回溯解法、装载问题、N皇后问题等。',
    icon: '🔙',
    topics: [
      '回溯法基本思想',
      '深度优先搜索',
      '剪枝函数（约束函数与限界函数）',
      '0/1背包的回溯解法',
      '装载问题的回溯解法',
      'N皇后问题'
    ],
    subfolders: [
      {
        name: 'hui01bag',
        label: '回溯0/1背包',
        files: [
          { path: 'c/ch5/hui01bag/huisu01bag.c', name: 'huisu01bag.c', label: '0/1背包回溯解法', description: '递归回溯搜索最优解' }
        ]
      },
      {
        name: 'loading',
        label: '装载问题',
        files: [
          { path: 'c/ch5/loading/huiloading.c', name: 'huiloading.c', label: '回溯装载', description: '回溯搜索最佳放置方案' },
          { path: 'c/ch5/loading/loading2.c', name: 'loading2.c', label: '装载实现（二）', description: '装载问题的另一种回溯实现' }
        ]
      },
      {
        name: 'queue',
        label: 'N皇后',
        files: [
          { path: 'c/ch5/queue/nqueue.c', name: 'nqueue.c', label: 'N皇后问题', description: '回溯法解N皇后问题' }
        ]
      }
    ]
  },
  {
    id: 'ch6',
    title: '第六章 分支限界法',
    subtitle: 'Branch and Bound — 剪枝搜索最优解',
    description: '分支限界法以广度优先或最小耗费优先的方式搜索解空间树，并用限界函数剪去不可能产生最优解的分支。本章涵盖图的BFS/DFS、0/1背包的分支限界解法、TSP问题等。',
    icon: '🌿',
    topics: [
      '分支限界法基本思想',
      '广度优先搜索与优先队列',
      '上界函数与剪枝',
      '0/1背包的分支限界解法',
      '旅行商问题(TSP)',
      '图的BFS与DFS遍历'
    ],
    subfolders: [
      {
        name: 'xianbag01',
        label: '分支限界0/1背包',
        files: [
          { path: 'c/ch6/xianbag01/bag01fifo.c', name: 'bag01fifo.c', label: '0/1背包（FIFO队列）', description: 'FIFO活结点队列的分支限界实现' },
          { path: 'c/ch6/xianbag01/bag01livevalue.c', name: 'bag01livevalue.c', label: '0/1背包（上界估计）', description: '用上界剪枝搜索最佳值的分支限界实现' }
        ]
      },
      {
        name: 'tsp',
        label: '旅行商问题(TSP)',
        files: [
          { path: 'c/ch6/tsp/fenzhi/tsp2.c', name: 'tsp2.c', label: 'TSP（分支限界）', description: '分支限界法解旅行商问题' },
          { path: 'c/ch6/tsp/huisu/travelroute.c', name: 'travelroute.c', label: 'TSP（回溯法）', description: '回溯法求解旅行商路径' }
        ]
      },
      {
        name: 'graph',
        label: '图遍历',
        files: [
          { path: 'c/ch6/graph/bfs.c', name: 'bfs.c', label: '广度优先遍历(BFS)', description: '队列实现的图广度优先遍历' },
          { path: 'c/ch6/graph/dfs.c', name: 'dfs.c', label: '深度优先遍历(DFS)', description: '邻接矩阵/递归的图深度优先遍历' }
        ]
      }
    ]
  }
]

/**
 * 获取所有代码文件的扁平列表（含章节信息）
 */
export function getAllFiles() {
  const allFiles = []
  for (const chapter of chapters) {
    if (chapter.files) {
      for (const file of chapter.files) {
        allFiles.push({ ...file, chapterId: chapter.id, chapterTitle: chapter.title })
      }
    }
    if (chapter.subfolders) {
      for (const folder of chapter.subfolders) {
        for (const file of folder.files) {
          allFiles.push({ ...file, chapterId: chapter.id, chapterTitle: chapter.title, folder: folder.label })
        }
      }
    }
  }
  return allFiles
}

export function getChapterById(id) {
  return chapters.find(ch => ch.id === id)
}
