215 - Spreadsheet Calculator (UVA)

题目链接如下:

Online Judge

写了两个版本(都能AC),都感觉不太利索,但暂时想不出更好的了:

版本一:

#include 
#include 
#include 
#include 
// #define debug

int r, c;
std::string input[20][10];
int ans[20][10];
std::vector need[200];
std::vector vec;

void process(int loc, std::string str){
    for (int i = 0; i < str.size(); ++i){
        if (isalpha(str[i])){
            int temp = 10 * (str[i] - 'A') + str[i + 1] - '0';
            need[loc].push_back(temp);
            ++i;
        }
    }
}

int cal(std::string str){
    int temp, sum = 0;
    if (str[0] != '-'){
        str = "+" + str;
    }
    for (int i = 0; i < str.size();){
        if (isalpha(str[i + 1])){
            temp = ans[str[i + 1] - 'A'][str[i + 2] - '0'];
            str[i] == '+' ? sum += temp : sum -= temp;
            i = i + 3;
        } else {
            temp = 0;
            int k = 1;
            while (i + k < str.size() && isdigit(str[i + k])){
                temp = temp * 10 + str[i + k] - '0';
                k++;
            }
            str[i] == '+' ? sum += temp : sum -= temp;
            i = i + k;
        }
    }
    return sum;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d %d\n", &r, &c) == 2 && r && c){
        for (int i = 0; i < 200; ++i){
            need[i].clear();
        }
        vec.clear();
        for (int i = 0; i < r; ++i){
            for (int j = 0; j < c; ++j){
                getline(std::cin, input[i][j]);
                if (!isalpha(input[i][j][0])){
                    ans[i][j] = std::atoi(input[i][j].c_str());
                } else {
                    process(i * 10 + j, input[i][j]);
                    vec.push_back(i * 10 + j);
                    ans[i][j] = 100000;
                }
            }
        }
        while (1){
            bool flag = false;
            for (int i = 0; i < vec.size(); ++i){
                for (int j = 0; j < need[vec[i]].size(); ++j){
                    int temp = need[vec[i]][j];
                    if (ans[temp / 10][temp % 10] <= 10000){
                        flag = true;
                        need[vec[i]].erase(need[vec[i]].begin() + j);
                        --j;
                    }
                }
                if (need[vec[i]].empty()){
                    ans[vec[i] / 10][vec[i] % 10] = cal(input[vec[i] / 10][vec[i] % 10]);
                    vec.erase(vec.begin() + i);
                    --i;
                }
            }
            if (!flag || vec.empty()){
                break;
            }
        }
        if (!vec.empty()){
            for (int i = 0; i < vec.size(); ++i){
                printf("%c%c: %s\n", 'A' + vec[i] / 10, '0' + vec[i] % 10, input[vec[i] / 10][vec[i] % 10].c_str());
            }
        } else{
            printf(" ");
            for (int i = 0; i < c; ++i){
                printf("%6d", i);
            }
            printf("\n");
            for (int i = 0; i < r; ++i){
                printf("%c", i + 'A');
                for (int j = 0; j < c; ++j){
                    printf("%6d", ans[i][j]);
                }
                printf("\n");
            }
        }
        printf("\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

版本二:

#include 
#include 
#include 
#include 
// #define debug

struct node{
    bool flag = false;
    int sum = 0;
    std::vector vec;
    std::string input;
};

int r, c;
node mat[20][10];
std::string str;

void process(node *n, std::string ss){
    n->input = ss;
    ss = "+" + ss;
    for (int i = 0; i < ss.size();){
        if (isalpha(ss[i + 1])){
            n->vec.push_back(ss.substr(i, 3));
            i += 3;
        } else {
            int temp = 0;
            int k = 1;
            while (i + k < ss.size() && isdigit(ss[i + k])){
                temp = 10 * temp + ss[i + k] - '0';
                k++;
            }
            ss[i] == '+' ? n->sum += temp : n->sum -= temp;
            i += k;
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d %d\n", &r, &c) == 2 && r && c){
        node temp;
        for (int i = 0; i < r; ++i){
            for (int j = 0; j < c; ++j){
                mat[i][j] = temp;
                getline(std::cin, str);
                if (!isalpha(str[0])){
                    mat[i][j].sum = std::stoi(str.c_str());
                    mat[i][j].flag = true;
                } else {
                    process(mat[i] + j, str);
                }
            }
        }
        bool finish;
        while (1){
            finish = true;
            bool flag = false;
            for (int i = 0; i < r; ++i){
                for (int j = 0; j < c; ++j){
                    if(!mat[i][j].flag){
                        for (int k = 0; k < mat[i][j].vec.size(); ++k){
                            std::string temp = mat[i][j].vec[k];
                            if (mat[temp[1] - 'A'][temp[2] - '0'].flag){
                                temp[0] == '+' ? mat[i][j].sum += mat[temp[1] - 'A'][temp[2] - '0'].sum : mat[i][j].sum -= mat[temp[1] - 'A'][temp[2] - '0'].sum;
                                mat[i][j].vec.erase(mat[i][j].vec.begin() + k);
                                k--;
                            }
                        }
                        if (mat[i][j].vec.empty()){
                            flag = true;
                            mat[i][j].flag = true;
                        }
                        finish = false;
                    }
                }
            }
            if (!flag){
                break;
            }
        }
        if (finish){
            printf(" ");
            for (int i = 0; i < c; ++i){
                printf("%6d", i);
            }
            printf("\n");
            for (int i = 0; i < r; ++i){
                printf("%c", i + 'A');
                for (int j = 0; j < c; ++j){
                    printf("%6d", mat[i][j].sum);
                }
                printf("\n");
            }
        } else {
            for (int i = 0; i < r; ++i){
                for (int j = 0; j < c; ++j){
                    if (!mat[i][j].flag){
                        printf("%c%c: %s\n", i + 'A', j + '0', mat[i][j].input.c_str());
                    }
                }
            }
        }
        printf("\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

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