Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-09-09

GravityBomb

| 16:40

問題文, SRM 200

テトリスの局面において、隙間をなくすとどうなるか。

421.30/500

class GravityBomb {
    public:
        vector <string> aftermath (vector <string> board) {
            const int H = board.size();
            const int W = board[0].size();
            const static char BLOCK = 'X';
            vector<int> counter(W, 0);
            for (int i = 0; i < H; i++)
                for (int j = 0; j < W; j++)
                    if (board[i][j] == BLOCK)
                        counter[j]++;
            int minBlocks = *min_element(counter.begin(), counter.end());

            vector <string> result(H, string(W, '.'));
            for (int j = 0; j < W; j++)
                for (int i = 0; i < counter[j]-minBlocks; i++)
                    result[H-1-i][j] = BLOCK;
            return result;
        }
};

2009-09-03

NoOrderOfOperations

| 12:44

問題文, SRM 200

How To Dissect a TopCoder Problem Statement -- Algorithm Tutorialsから。

式を左から右に評価。

243.45/250

class NoOrderOfOperations {
public:
    int evaluate(string expr) {
        int result = expr[0]-'0';
        for (int i = 1; i < expr.length(); i += 2) {
            switch (expr[i]) {
                case '+':
                    result += expr[i+1]-'0';
                    break;
                case '-':
                    result -= expr[i+1]-'0';
                    break;
                case '*':
                    result *= expr[i+1]-'0';
                    break;
            }
        }
        return result;
    }
};