码迷,mamicode.com
首页 > 其他好文 > 详细

Section 1.4 搜索

时间:2015-07-13 00:47:25      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

Arithmetic Progressions

http://www.wzoi.org/usaco/14%5C110.asp

 

并没有看出哪里像搜索。。大概是我蠢,暴力过去了

处理出所有的p^2+p^2的值,标记处有的值vis[i]

在其中枚举a,以1-a+(n-1)*b<=2*m*m的限制枚举b,检查是否有n位的存在

技术分享
#include <bits/stdc++.h>
using namespace std;
const int N  = 140000;
bool vis[N];
int f[N>>1];
struct point{
    int a, b;
    point(){};
    point(int _a, int _b){a = _a, b = _b;};

    bool operator<(const point &I)const{
        if(b == I.b)    return a <I.a;
        return b <I.b;
    }
}p[10004];
int main()
{
    freopen("ariprog.in","r",stdin);
    #ifndef poi
    freopen("ariprog.out","w",stdout);
    #endif
    int n, m, i, j, k, a, b;
    cin >> n >> m;
    int id = 0, no = 0;
    for(i = 0; i <= m; i++){
        for(j = i; j <= m; j++) {
            f[++id] = i * i + j * j;
            vis[f[id]] = true;
        }
    }
    sort(f + 1, f + id + 1);
    id = unique(f + 1, f + id + 1) - f;
    int limit = 2 * m * m;
    for(i = 1; i < id; i++){
        a = f[i];
     //   printf("!!%d\n", a);
        for(j = 1; j*(n-1)+a <= limit; j++){
            for(k = 1; k < n; k++){
                if(!vis[j*k+a]) break;
            }
            if(k == n)  p[++no] = point(a, j);
        }
    }
    if(no == 0){
        printf("NONE\n");
        return 0;
    }
    sort(p + 1, p + no + 1);

    for(i = 1; i <= no; i++){
        printf("%d %d\n", p[i].a, p[i].b);
    }

    return 0;
}
View Code

Mother‘s Milk

http://www.wzoi.org/usaco/12%5C403.asp

真。搜索。。以(0, 0, C)为初始,bfs一下,当a为0时记录c的值,因为很小,避免去重用vis标记

技术分享
#include <bits/stdc++.h>
using namespace std;

const int N = 22;
struct point{
    int a, b, c;

    point(){};
    point(int _a, int _b, int _c){a = _a, b = _b, c = _c;};
};
bool state[N][N][N], ok[N];
int A, B, C;
vector<int>ans;
void bfs(){
    state[0][0][C] = true;
    queue<struct point>Q;
    Q.push(point(0, 0, C));
    struct point x;
    int temp, a, b, c;
    while(!Q.empty()){
        x = Q.front();
        if(x.a == 0)   ok[x.c] = true;
      // cout << x.a << x.b << x.c << endl;
        Q.pop();
        if(x.a){
            if(x.b != B){
                temp = min(B-x.b, x.a);
                a = x.a - temp;
                b = x.b + temp;
                c = x.c;
               if(!state[a][b][c]){
                    Q.push(point(a, b, c));
                    state[a][b][c] = true;
                }
            }
            if(x.c != C){
                temp = min(C-x.c, x.a);
                a = x.a - temp;
                b = x.b;
                c = x.c + temp;
                if(!state[a][b][c]){
                    Q.push(point(a, b, c));
                    state[a][b][c] = true;
                }
            }
        }
        if(x.b){
            if(x.a != A){
                temp = min(A-x.a, x.b);
                a = x.a + temp;
                b = x.b - temp;
                c = x.c;
               if(!state[a][b][c]){
                    Q.push(point(a, b, c));
                    state[a][b][c] = true;
                }
            }
            if(x.c != C){
                temp = min(C-x.c, x.b);
                a = x.a;
                b = x.b - temp;
                c = x.c + temp;
               if(!state[a][b][c]){
                    Q.push(point(a, b, c));
                    state[a][b][c] = true;
                }
            }
        }
        if(x.c){
            if(x.a != A){
                temp = min(A-x.a, x.c);
                a = x.a + temp;
                b = x.b;
                c = x.c - temp;
               if(!state[a][b][c]){
                    Q.push(point(a, b, c));
                    state[a][b][c] = true;
                }
            }
            if(x.b != B){
                temp = min(B-x.b, x.c);
                a = x.a;
                b = x.b + temp;
                c = x.c - temp;
               if(!state[a][b][c]){
                    Q.push(point(a, b, c));
                    state[a][b][c] = true;
                }
            }
        }
    }

}
int main()
{
    freopen("milk3.in","r",stdin);
    #ifndef poi
    freopen("milk3.out","w",stdout);
    #endif
    cin >> A>> B>> C;

    bfs();
    bool first = true;
    for(int i = 0; i <= 20; i++){

        if(ok[i])  {
            if(!first)  cout << " ";
            cout << i;first = false;
        }

    }
    cout <<endl;

    return 0;
}
View Code

 

考研好痛苦。好羡慕保研的。。

Section 1.4 搜索

标签:

原文地址:http://www.cnblogs.com/bbbbbq/p/4641757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!