uva1602

作者: Amosasas | 来源:发表于2017-10-14 18:08 被阅读0次

    打表加暴力搜索 看刘汝佳的代码照着写的
    开始的时候想用二维数组表示Polyomino的 但是后面用这个数据结构根本就无法写出公式
    看了这边的代码知道选择结构的问题 二维数组可变性实在太小了(但是确实很好表达逃)
    那用结构题刷过的这种题好像是uva1601把 刚开始也是用结构体洋洋洒洒写了一个晚上 后面发现不仅慢还有错。。。(因为没有用二进制表示坐标。。。。。。)
    代码就不写注释了。。。因为实在很裸。。。。

    #include <cstdio>
    #include <set>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    const int N = 10;
    int ans[N+1][N+1][N+1];
    int n, w, h;
    struct Cell{
        int x,y;
        Cell(int xx = 0, int yy = 0) : x(xx), y(yy){
        }
        bool operator < (const Cell& rhs) const {
            return x < rhs.x || (x == rhs.x && y < rhs.y); 
        }
    };
    typedef set<Cell> Polyomino;
    typedef set<Polyomino> Polys;
    Polys polysnum[N+1];
    const int dx[] = {1,-1,0,0};
    const int dy[] = {0,0,1,-1};
    Polyomino normalize(const Polyomino& p){
        Polyomino newp;
        int minX = (p.begin())->x;
        int minY = (p.begin())->y;
        for( Polyomino::const_iterator i = p.begin(); i != p.end(); i++){
            minX = min(minX, i->x);
            minY = min(minY, i->y);
        }
        for( Polyomino::iterator i = p.begin(); i != p.end(); i++){
            newp.insert(Cell( i->x - minX, i->y - minY ));
        }
        return newp;
    }
    Polyomino rotate(const Polyomino& p){
        Polyomino newp;
        for( Polyomino::iterator i = p.begin(); i != p.end(); i++){
            newp.insert(Cell( i->y, -i->x ));
        }
        return normalize(newp);
    }
    Polyomino flip(const Polyomino& p){
        Polyomino newp;
        for( Polyomino::iterator i = p.begin(); i != p.end(); i++){
            newp.insert(Cell( i->x, -i->y ));
        }
        return normalize(newp);
    }
    
    void put_cell(const Polyomino& p, const Cell& newCell){
        Polyomino newp = p;
        newp.insert( newCell );
        newp = normalize(newp);
        int nn = newp.size();
        for(int i = 0; i < 4; i++){
            if(polysnum[nn].count(newp) != 0) return;
            newp = rotate(newp);
        }
        newp = flip(newp);
        for(int i = 0; i < 4; i++){
            if(polysnum[nn].count(newp) != 0) return;
            newp = rotate(newp);
        }
        polysnum[nn].insert(newp);
    }
    void generate(){
        Polyomino p;
        p.insert(Cell(0,0));
        polysnum[1].insert(p);
        for(int k = 2; k <= N; k++){
            for(Polys::iterator iter_polys = (polysnum[k-1]).begin(); iter_polys != (polysnum[k-1]).end(); iter_polys++){
                for(Polyomino::iterator iter_cells = (*iter_polys).begin(); iter_cells != (*iter_polys).end(); iter_cells++){
                    for(int dir = 0; dir < 4; dir++){
                        Cell newCell( iter_cells->x + dx[dir], iter_cells->y + dy[dir]);
                        if( iter_polys->count(newCell) == 0 ){
                            put_cell( *iter_polys, newCell);
                        }
                    }
                }
            }
        }
        for(int i = 1; i <= N; i++){
            for(int j = 1; j <= N; j++){            //w
                for(int k = 1; k <= N; k++){        //h
                    //cout<<"n "<<i<<endl;
                    int cnt = 0;
                    for(Polys::iterator iter_polys = (polysnum[i]).begin(); iter_polys != (polysnum[i]).end(); iter_polys++){
                        int maxX = 0;
                        int maxY = 0;
                        for(Polyomino::iterator iter_cells = (*iter_polys).begin(); iter_cells != (*iter_polys).end(); iter_cells++){
                            maxX = max(maxX, iter_cells->x);
                            maxY = max(maxY, iter_cells->y);
                        }
                        if(min(maxX, maxY) < min(j, k) && max(maxX, maxY) < max(j, k)){
                            cnt++;
                            //cout<<i<<" "<<j<<" "<<k<<maxX<<" "<<maxY<<endl;
                        }
                    }
                    ans[i][j][k] = cnt;
                }
            }
        }
    }
    int main(){
        generate();
        while(scanf("%d %d %d", &n, &w, &h) == 3){
            printf("%d\n", ans[n][w][h]);
        }
        return 0;
    } 
    

    相关文章

      网友评论

          本文标题:uva1602

          本文链接:https://www.haomeiwen.com/subject/hzkfuxtx.html