sgu 224

本文介绍了一种解决N皇后问题的算法实现,通过递归深度优先搜索(DFS)来寻找放置k个皇后在n×n的棋盘上的所有可能方案,并采用了一些优化策略如对称性优化等。
//dfs

//   k = 0, ans = 1;
//   k = 1, ans = n*n ;
//   k > n,  ans = 0;
//还可以进一步优化  
//   if (n - ps < k - count) rollback;  当剩余列数小于剩余棋子数时,回滚
//   对称性优化, 第一个棋子放在某一列m行,和放在该列n-m+1
//   直接用数组保存所有结果 ---   有些耍赖皮

#include <stdio.h>

int n, k;
int stk[12];
int ps = 0;

int isOK(int m) {
  int i;
  for (i=0; i<ps; ++i) {
    if (m == stk[i]) {
      return 0;
    }
    if (stk[i] > 0 && abs(ps-i) == abs(m-stk[i])) {
      return 0;
    }
  }
  return 1;
}

int dfs() {
  int m = 1;
  int count = 0;
  int ans = 0;

  while (ps >= 0) {
    
    if (count == k) {
      ++ans;
      --ps;
      while (stk[ps] <=0) --ps;
      m = stk[ps] + 1;
      --count;
      continue;
    }

    if (ps >= n) {
      --ps;
      while (stk[ps] <= 0 && ps >= 0) --ps;
      m = stk[ps] + 1;
      --count;
      continue;
    }

    if (m > n) {
      stk[ps] = 0;
      ++ps;
      m = 1;
      continue;
    }
    
    if (isOK(m)) {
      stk[ps] = m;
      ++ps;
      m = 1;
      ++count;
    }
    else {
      ++m;
    }
  }

  return ans;
}

int main() {
    scanf("%d%d", &n, &k);
    if (k > n) {
      printf("0\n");
      return 0;
    }
    if (k == 0) {
      printf("1\n");
      return 0;
    }
    if (k == 1) {
      printf("%d\n", n*n);
      return 0;
    }

    printf("%d\n", dfs());
    return 0;
}


根据我提供的信息,优化程序 待写入的topic是三个分区; 生产者线程调整为4; {"log_type":"auth","cnmaster":"764991563","last_domain":"tlbbgl-activity.changyou.com","phone":"0FDD7A79D10EE13430AFDF","media_type":"{\"base_url\":\"https://tlbbgl.changyou.com/\"}","ip":"182.135.97.61","product_id":"90","time":"2025-07-09 15:54:42","cn":"qstl0709@changyou.com","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"zjhy107@changyou.com","last_domain":"tlbbglact.changyou.com","phone":"0FD07A73D003E43236A7DE","media_type":"{\"rcc_id\":\"788920c8-61cb-4d27-a228-5c1d31fe5862\",\"type\":\"xdlb3\",\"sid\":\"52746\",\"sign\":\"A8AACB481CA753C80215182224ECF006\",\"base_url\":\"https://tlbbglact.changyou.com/tlbbgl/rlr/20250507/m/index17.shtml\"}","ip":"183.199.230.1","product_id":"90","time":"2025-07-09 15:54:46","cn":"zjhy107@changyou.com","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"602132693","last_domain":"tlbbgl-activity.changyou.com","phone":"0FDD7B79D00AE13631A2DD","media_type":"{\"base_url\":\"https://tlbbgl.changyou.com/m/index.shtml\",\"rcc_id\":\"01b8800c-74e7-4303-90b8-98e414783c66\",\"type\":\"gfgzh1\"}","ip":"218.56.20.246","product_id":"90","time":"2025-07-09 15:54:49","cn":"237176761@qq.com","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"764991582","last_domain":"tlbbglact.changyou.com","phone":"0FDC7672D00FE73736A6DD","media_type":"{\"rcc_id\":\"f9b1f1cf-bc85-40cb-b4e4-5fe2997ff177\",\"type\":\"xdlb14\",\"sid\":\"27597\",\"sign\":\"D76F6FA0C3CFCB6598E7EC9E5B71EA8A\",\"base_url\":\"https://tlbbglact.changyou.com/tlbbgl/rlr/20250507/m/index37.shtml\"}","ip":"117.136.8.228","product_id":"90","time":"2025-07-09 15:54:52","cn":"0FDC7672D00FE73736A6DD","type":"newAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"574596646","last_domain":"tlbbgl-activity.changyou.com","phone":"0FD27471D50EE03432A3D9","media_type":"{\"base_url\":\"https://tlbbgl.changyou.com/load/20250523/index.shtml\",\"rcc_id\":\"949acafa-1833-44f3-b899-03f250dd8f78\",\"media\":\"bd\",\"type\":\"bdsemob1\",\"bd_vid\":\"8536153497538321445\"}","ip":"58.240.95.36","product_id":"90","time":"2025-07-09 15:54:55","cn":"0FD27471D50EE03432A3D9","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"715406543","last_domain":"tlbbglact.changyou.com","phone":"0FD37471D70DE63736A3DE","media_type":"{\"rcc_id\":\"f9b1f1cf-bc85-40cb-b4e4-5fe2997ff177\",\"type\":\"xdlb14\",\"base_url\":\"https://tlbbglact.changyou.com/tlbbgl/rlr/20250507/m/index03.shtml\"}","ip":"1.197.240.150","product_id":"90","time":"2025-07-09 15:54:56","cn":"bs0410@changyou.com","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"731641905","last_domain":"tlbbgl-activity.changyou.com","phone":"0FDD7671D70CE1313BAFD3","media_type":"{\"base_url\":\"https://tlbbgl.changyou.com/load/20250701/m/index.shtml\",\"kscallback\":\"KvI4N-snDnO5j90vY8WBWi_MNuOQ-XKA_VvjczWc1dlkR-a74F8a2tIC54j2hp8Ch8qNSLaziB60yCCOVEDqfW32CyxNYQ1_ZSJM4L6dtu5l3Hxn0JNf_r9UdFAkpygreSicn-aXuiytn01k8sgU8GKrhYT6rdt9j8YWDFnWARzpEieYEJrRjaViX6xajd1B7gch_dog2Cj9xoICAdyfxTuK20AP6d2BWAbO3Iu7kqFbGfGHR-yPk-gIcwMOERFp\",\"rcc_id\":\"d6e4cad6-357a-418d-94cb-2105288bd6e4\",\"media\":\"ks\",\"type\":\"tfby3\"}","ip":"112.65.27.242","product_id":"90","time":"2025-07-09 15:54:57","cn":"0FDD7671D70CE1313BAFD3","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} {"log_type":"auth","cnmaster":"758218289","last_domain":"tlbbgl-activity.changyou.com","phone":"0FDD7170D702EB3937A7D9","media_type":"{\"rcc_id\":\"1c817f78-f113-499c-be6f-de523b199a2b\",\"wx_aid\":\"43651481675\",\"base_url\":\"https://tlbbgl.changyou.com/load/20250523/m/index.shtml\",\"wx_traceid\":\"wx0b2fzf6bw6kg5o01\",\"media\":\"gdt\",\"type\":\"gdtob2\",\"tid\":\"43651555625\",\"gdt_vid\":\"wx0b2fzf6bw6kg5o01\"}","ip":"183.224.110.1","product_id":"90","time":"2025-07-09 15:54:58","cn":"0FDD7170D702EB3937A7D9","type":"oldAccount","current_domain":"auth.changyou.com","mac_new":"0"} 这几种格式的json随机发送,内容做替换
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值