SPOJ 4565 What is your Logo

SPOJ_4565

    一开始本来打算先把线拓宽,当成方格来看待,然后模拟行走过程并对格子染色,最后用种子填充的方式扫描一下有多少封闭的块。但后来发现best solution基本都是0.00s的,于是便开始找规律,猛然发现原来交点的个数就是封闭正方形的个数……

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 1010
using namespace std;
int N;
char b[MAXD], *op = "UDLR";
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
struct Point
{
    int x, y;
    bool operator < (const Point &t) const
    {
        if(x == t.x)
            return y < t.y;
        return x < t.x;    
    }    
    bool operator == (const Point &t) const
    {
        return x == t.x && y == t.y;    
    }
}p[MAXD];
void solve()
{
    int i, k, x = 0, y = 0, ans = 0;
    p[0].x = p[0].y = 0;
    for(i = 1; i < N; i ++)
    {
        k = strchr(op, b[i]) - op;
        x += dx[k], y += dy[k];
        p[i].x = x, p[i].y = y;
    }
    sort(p, p + N);
    for(i = 1; i < N; i ++)
        if(p[i] == p[i - 1])
            ++ ans;
    printf("%d\n", ans);
}
int main()
{
    while(scanf("%s", b + 1) == 1)
    {
        if(b[1] == 'Q')
            break;
        N = strlen(b + 1);
        solve();
    }
    return 0;    
}

你可能感兴趣的:(logo)