zoukankan      html  css  js  c++  java
  • poj 1011 Sticks(dfs+剪枝)

    http://poj.org/problem?id=1011

      回归基础,搞一下DFS+剪枝的题。

      这题的代码打了两次,测了好多组数据才得到AC的代码。两次的代码都是因为返回的时候没有及时将used标记更新,而导致不停的WA。可是第一个代码写的太乱了,debug 漫天飞,于是就决定重新写了一遍。第二次打的时候思路清晰的许多,最后得到下面AC代码~

    View Code
      1 #include <cstdio>
      2 #include <cmath>
      3 #include <cstring>
      4 #include <iomanip>
      5 #include <iostream>
      6 #include <algorithm>
      7 #include <vector>
      8 #include <string>
      9 #include <set>
     10 #include <queue>
     11 
     12 using namespace std;
     13 
     14 #define pb push_back
     15 #define fi first
     16 #define se second
     17 #define mpr make_pair
     18 #define _clr(x) memset(x, 0, sizeof(x))
     19 #define _rst(x) memset(x, -1, sizeof(x))
     20 #define REP(i, n) for (int i = 0; i < n; i++)
     21 #define FORI(i, a, b) for (int i = a; i <= b; i++)
     22 #define FORD(i, a, b) for (int i = a; i >= b; i--)
     23 
     24 typedef __int64 ll;
     25 typedef pair<int, int> pii;
     26 typedef vector<ll> vll;
     27 typedef vector<pii> vpii;
     28 typedef vector<int> vi;
     29 typedef vector<double> vdbl;
     30 const int N = 70;
     31 const int hashMod = 1000007;
     32 const int inf = 0x55555555;
     33 const double eps = 1e-8;
     34 const ll linf = 0x5555555555555555ll;
     35 const double finf = 1e50;
     36 const double pi = acos(-1.0);
     37 
     38 int rec[N], n;
     39 bool used[N];
     40 
     41 bool dfs(int p, int g, int G, int s, int S) {
     42 //    cout << p << ends << g << ends << G << ends << s << ends << S << endl;
     43 //    cout << used[0] << endl;
     44     used[p] = true;
     45     if (s == S && g == G) return true;
     46     if (s == S) {
     47         int t = n;
     48         while (t--) {
     49             if (used[t]) continue;
     50 //            cout << "~~~" << t << endl;
     51             if (dfs(t, g + 1, G, rec[t], S)) return true;
     52             else {
     53                 used[p] = false;
     54                 return false;
     55             }
     56         }
     57     }
     58     int t = p;
     59     while (t--) {
     60         if (used[t] || rec[t] + s > S) continue;
     61         if (dfs(t, g, G, rec[t] + s, S)) return true;
     62         while (t && rec[t] == rec[t - 1]) t--;
     63     }
     64     used[p] = false;
     65     return false;
     66 }
     67 
     68 int work() {
     69     int sum = 0;
     70     REP(i, n) {
     71         cin >> rec[i];
     72         sum += rec[i];
     73     }
     74     sort(rec, rec + n);
     75     int mk = sum;
     76 //    REP(i, n) cout << rec[i] << ends;
     77 //    cout << endl;
     78     FORI(i, rec[n - 1], sum - 1) {
     79         _clr(used);
     80         if (sum % i == 0 && dfs(n - 1, 1, sum / i, rec[n - 1], i)) {
     81             mk = i;
     82             break;
     83         }
     84     }
     85     return mk;
     86 }
     87 
     88 int main() {
     89 //    freopen("in", "r", stdin);
     90     while (cin >> n && n) {
     91         cout << work() << endl;
     92     }
     93     return 0;
     94 }
     95 
     96 /*
     97 9
     98 5 2 1 5 2 1 5 2 1
     99 4
    100 1 2 3 4
    101 64
    102 40 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40
    103 40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40
    104 40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40 40
    105 64
    106 5 8 6 1 4 5 8 2 1 6 4 2 3 6 2 5 8 7 9 2 1 4 3 6 5 4 7 8
    107 2 6 5 1 4 5 3 6 5 6 2 1 4 7 8 9 5 2 1 9 4 7 5 2 3 6 9 4
    108 5 8 9 2 4 6 3 6
    109 3
    110 1 5 8
    111 9
    112 15 3 2 11 4 1 8 8 8
    113 0
    114 */

    ——written by Lyon

  • 相关阅读:
    JEECG与帆软报表集成
    各种数据库的锁表和解锁操作
    sql server数据库查询超时报错
    java项目部署后的文件路径获取
    js解决跨站点脚本编制问题
    java递归算法实现拼装树形JSON数据
    FreeMarker中的list集合前后台代码
    去除list集合中重复项的几种方法
    Java中Properties类的操作
    mysql 容灾 灾备 备份
  • 原文地址:https://www.cnblogs.com/LyonLys/p/poj_1011_Lyon.html
Copyright © 2011-2022 走看看