Codeforces Gym 101173 A. Appearance Analysis

Keywords: Windows

meaning of the title

Given the abstract character representation of a pane, # represents a window frame,. And + the connected area represents a window, all of which are rectangular and have the same length and width. For all windows, how many different window styles are there? If the window can be rotated to coincide patterns, it is considered the same style.

Solving problems

Simple simulation of this problem, using DFS to obtain all windows according to, record the position of the pattern in the window and choose 90, 180, 270 degrees of pattern position, match two to two for all windows to obtain different numbers.

Code

#include<bits/stdc++.h>
using namespace std;
const int dir[4][2] = {-1,0, 1,0, 0,-1, 0,1};
const int inf = 1e9 + 7;
int r, c;
char mp[120][120];
bool vis[120][120];
struct Window {
    int lft, up, rgt, down;
    int r[4], c[4];
    vector<pair<int, int> > paint[4];
    void clear() {
        lft = inf;    rgt = 0;
        up =inf; down = 0;
        for(int i=0;i<4;i++)
            r[i] = c[i] = 0,
            paint[i].clear();
    }
} p;
bool operator<(pair<int, int> a, pair<int, int> b)
{
    if(a.first == b.first)  return a.second < b.second;
    return a.first < b.first;
}
vector<Window> v;
vector<int> mrk;
void dfs(int x, int y)
{
    p.lft = min(p.lft, y);
    p.rgt = max(p.rgt, y);
    p.up = min(p.up, x);
    p.down = max(p.down, x);
    if(mp[x][y] == '+') p.paint[0].push_back(make_pair(x, y));
    vis[x][y] = 1;
    for(int i=0, nx, ny;i<4;i++)
    {
        nx = x + dir[i][0];
        ny = y + dir[i][1];
        if(nx <= 0 || nx > r || ny <= 0 || ny > c || vis[nx][ny] || mp[nx][ny] == '#')   continue;
        dfs(nx, ny);
    }
}
void dealP()
{
    p.r[0] = p.r[2] = p.down - p.up + 1;
    p.c[0] = p.c[2] = p.rgt - p.lft + 1;
    p.r[1] = p.r[3] = p.c[0];
    p.c[1] = p.c[3] = p.r[0];
    for(int i=0;i<p.paint[0].size();i++)
    {
        p.paint[0][i].first -= p.up;
        p.paint[0][i].second -= p.lft;
    }
    for(int j=0, x, y;j<p.paint[0].size();j++)
    {
        x = p.paint[0][j].first;
        y = p.paint[0][j].second;
        p.paint[1].push_back(make_pair(y, p.r[0]-x-1));
        p.paint[2].push_back(make_pair(p.r[0]-x-1, p.c[0]-y-1));
        p.paint[3].push_back(make_pair(p.c[0]-y-1, x));
    }
    for(int i=0;i<4;i++)
        sort(p.paint[i].begin(), p.paint[i].end());
}
bool jug(int idx)
{
    for(int i=0;i<mrk.size();i++)
    {
        if(v[idx].paint[0].size() != v[mrk[i]].paint[0].size())  continue;
        for(int ii=0;ii<4;ii++)
        for(int jj=ii;jj<4;jj++)
        {
            if(v[idx].r[ii] != v[mrk[i]].r[jj] || v[idx].c[ii] != v[mrk[i]].c[jj])  continue;
            bool flg = true;
            for(int k=0;k<v[idx].paint[ii].size();k++)
                if(v[idx].paint[ii][k] != v[mrk[i]].paint[jj][k]) {
                    flg = false;
                    break;
                }
            if(flg == true) return true;
        }
    }
    return false;
}
int main()
{
    while(scanf("%d %d", &r, &c)!=EOF)
    {
    memset(vis, 0, sizeof(vis));
    v.clear();
    mrk.clear();
    for(int i=1;i<=r;i++)
        scanf(" %s", mp[i]+1);
    for(int i=1;i<=r;i++)
    for(int j=1;j<=c;j++)
    {
        if(mp[i][j] != '#' && vis[i][j] == 0)
        {
            p.clear();
            dfs(i, j);
            dealP();
            v.push_back(p);
        }
    }
    int cnt = 0;
    for(int i=0;i<v.size();i++)
    {
        if(jug(i) == false) {
            cnt ++;
            mrk.push_back(i);
        }
    }
    printf("%d\n", cnt);
    }
}

Posted by mdversion1 on Mon, 11 Feb 2019 16:09:19 -0800