Friday, June 3, 2011

SRM212

Div 1, Level 1
Div 1, Level 2
Div 1, Level 3
Div 2, Level 1
Div 2, Level 2
Div 2, Level 3

Tutorials:

Division One - Level Three:

Solution

Source Code:

Division One - Level Two:

Solution

Source Code:

Division One - Level One:

Solution

Source Code:

Division Two - Level Three:

Solution

This one really takes me a lot of time, mainly caused by the boundary of the circle.
PS: you need the complete circle, as well as all cells are not '#' on the boundary.

Source Code:

//Fri Jun  3 19:25:12 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class LargestCircle {
public:
    int radius(vector <string>);
};

int LargestCircle::radius(vector <string> grid) {
    int M = grid.size();
    int N = grid[0].size();
    int maxr = min(M, N) / 2;
    for(int r=maxr; r>0; r--){
        for(int j=0; j<N; j++){
            for(int i=0; i<M; i++){
                if(i-r+1>=0 && i+r<M && j-r+1>=0 && j+r<N){
                    bool flag = true;
                    for(int x=i-r; x<= i+r && flag; x++)
                        for(int y=j-r; y<=j+r && flag; y++){
                            int dist1 = abs(x-i) * abs(x-i) + abs(y-j) * abs(y-j) - r*r;
                            int dist2 = abs(x-i-1) * abs(x-i-1) + abs(y-j) * abs(y-j) - r*r;
                            int dist3 = abs(x-i) * abs(x-i) + abs(y-j-1)*abs(y-j-1) - r*r;
                            int dist4 = abs(x-i-1) * abs(x-i-1) + abs(y-j-1) * abs(y-j-1) - r*r;
                            vector<int> dist;
                            dist.push_back(dist1);
                            dist.push_back(dist2);
                            dist.push_back(dist3);
                            dist.push_back(dist4);
                            sort(dist.begin(), dist.end());
                            if(dist[0]<0 && dist[3]>0 && grid[x][y] == '#')
                                flag = false;
//                          if(dist1>0 && dist2>0 && dist3>0 && dist4>0) continue;
//                          if(dist1<0 && dist2<0 && dist3<0 && dist4<0) continue;
//                          if(grid[x][y] == '#') flag = false;
                        }
                    if(flag){
                        cout << "Center: " << i << "," << j << endl;
                        return r;
                    }
                }
            }
        }
    }
    return 0;
}

//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!

Division Two - Level Two:

Solution

Source Code:

Division Two - Level One:

Solution

Source Code:

//2009/08/14 03:12:36
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class YahtzeeScore
{
public:
    int maxPoints(vector <int> toss)
    {
        int res = 0;
        for(int i=0; i<toss.size(); i++)
        {
            int score = toss[i];
            for(int j=i+1; j<toss.size(); j++)
            {
                if(toss[i]==toss[j])
                    score += toss[j];
            }
            res = max(res, score);
        }
        return res;
    }
};

No comments :