101 - The Blocks Problem (UVA)

题目链接如下:

Online Judge

一开始的代码(能AC但是比较复杂,是考虑到还能再精简,但犯懒....以后还是应该自己先动脑想一想再看别人解法):

#include 
#include 
#include 
const int maxx = 25;
// #define debug

int n, a, b;
std::vector vec[maxx];
char op[5], op1[5];
int loc[maxx];

void moveOnto(int u, int v){
    int tu = loc[u];
    int tv = loc[v];
    while(vec[tu].back() != u){
        int temp = vec[tu].back();
        vec[temp].push_back(temp);
        loc[temp] = temp;
        vec[tu].pop_back();
    }
    while(vec[tv].back() != v){
        int temp = vec[tv].back();
        vec[temp].push_back(temp);
        loc[temp] = temp;
        vec[tv].pop_back();
    }
    vec[tv].push_back(u);
    loc[u] = tv;
    vec[tu].pop_back();
}

void moveOver(int u, int v){
    int tu = loc[u];
    int tv = loc[v];
    while(vec[tu].back() != u){
        int temp = vec[tu].back();
        vec[temp].push_back(temp);
        loc[temp] = temp;
        vec[tu].pop_back();
    }
    vec[tv].push_back(u);
    loc[u] = tv;
    vec[tu].pop_back();
}

void pileOnto(int u, int v){
    int tu = loc[u];
    int tv = loc[v];
    while(vec[tv].back() != v){
        int temp = vec[tv].back();
        vec[temp].push_back(temp);
        loc[temp] = temp;
        vec[tv].pop_back();
    }
    auto location = find(vec[tu].begin(), vec[tu].end(), u) - vec[tu].begin();
    for(int i = location; i < vec[tu].size(); ++i){
        vec[tv].push_back(vec[tu][i]);
        loc[vec[tu][i]] = tv;
    }
    while(vec[tu].back() != u){
        vec[tu].pop_back();
    }
    vec[tu].pop_back();
}

void pileOver(int u, int v){
    int tu = loc[u];
    int tv = loc[v];
    auto location = find(vec[tu].begin(), vec[tu].end(), u) - vec[tu].begin();
    for(int i = location; i < vec[tu].size(); ++i){
        vec[tv].push_back(vec[tu][i]);
        loc[vec[tu][i]] = tv;
    }
    while(vec[tu].back() != u){
        vec[tu].pop_back();
    }
    vec[tu].pop_back();
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d\n", &n);
    for(int i = 0; i < n; ++i){
        vec[i].push_back(i);
        loc[i] = i;
    }
    while(scanf("%s", op) == 1 && op[0] != 'q'){
        scanf("%d %s %d", &a, op1, &b);
        if(a == b || loc[a] == loc[b]){
            continue;
        }
        if(op[0] == 'm' && op1[1] == 'n'){
            moveOnto(a, b);
        } else if(op[0] == 'm' && op1[1] == 'v'){
            moveOver(a, b);
        } else if(op1[1] == 'n'){
            pileOnto(a, b);
        } else{
            pileOver(a, b);
        }
    }
    for(int i = 0; i < n; ++i){
        printf("%d:", i);
        for(int j = 0; j < vec[i].size(); ++j){
            printf(" %d", vec[i][j]);
        }
        printf("\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

然后看了刘汝佳的代码(不愧是大佬)之后修改的代码如下:

#include 
#include 
#include 
const int maxx = 25;
// #define debug

int n, a, b, ta, tb;
std::vector vec[maxx];
char op[5], op1[5];
int loc[maxx];

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d\n", &n);
    for(int i = 0; i < n; ++i){
        vec[i].push_back(i);
        loc[i] = i;
    }
    while(scanf("%s", op) == 1 && op[0] != 'q'){
        scanf("%d %s %d", &a, op1, &b);
        ta = loc[a];
        tb = loc[b];
        if(ta == tb){
            continue;
        }
        if(op1[1] == 'n'){
            while(vec[tb].back() != b){
                int temp = vec[tb].back();
                vec[temp].push_back(temp);
                loc[temp] = temp;
                vec[tb].pop_back();
            }
        }
        if(op[0] == 'm'){
            while(vec[ta].back() != a){
                int temp = vec[ta].back();
                vec[temp].push_back(temp);
                loc[temp] = temp;
                vec[ta].pop_back();
            }
        }
        auto location = find(vec[ta].begin(), vec[ta].end(), a) - vec[ta].begin();
        for(int i = location; i < vec[ta].size(); ++i){
            vec[tb].push_back(vec[ta][i]);
            loc[vec[ta][i]] = tb;
        }
        vec[ta].resize(location);
    }
    for(int i = 0; i < n; ++i){
        printf("%d:", i);
        for(int j = 0; j < vec[i].size(); ++j){
            printf(" %d", vec[i][j]);
        }
        printf("\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,算法)