zoukankan      html  css  js  c++  java
  • uva 177:Paper Folding(模拟 Grade D)

    题目链接

    题意:一张纸,每次从右往左对折。折好以后打开,让每个折痕都自然的呈90度。输出形状。

    思路:模拟折……每次折想象成把一张纸分成了正面在下的一张和反面在上的一张。维护左边和方向,然后输出。细节有点多。

    代码:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define N (1<<13)+10
    
    struct Paper{
        int l, r;
        bool isFace;
        Paper(){}
        Paper(int _l, int _r, bool is) : l(_l), r(_r), isFace(is){}
    }paper[N];
    
    int data[N];
    
    int pp;
    
    void flod(Paper &now) {
        int mid = (now.l+now.r)/2;
        data[mid] = now.isFace?1:-1;
        //printf("%d %c %d
    ", now.l, "^v"[data[mid]==1], now.r);
        if( now.isFace ) {
            paper[pp++] = Paper(mid,now.r,!now.isFace);
            now.r = mid;
        } else {
            paper[pp++] = Paper(mid,now.r,now.isFace);
            now.r = mid;
            now.isFace = !now.isFace;
        }
    
    }
    
    struct Point{
        int x,y;
        int way;
        Point(int x=0, int y=0, int way=-1):x(x),y(y),way(way){}
    }point[N];
    
    enum{
        R,U,L,D
    };
    
    char mat[2000][2000];
    void showGraph(int end) {
        point[0] = Point(0,0,R);
        //printf("%d,%d,%c
    ",0,0,"RULD"[R]);
        int lmost = 0;
        int umost = 0;
        int rmost = 0;
        int dmost = 0;
        for (int i = 1; i < end; i++) {
            int way = (point[i-1].way+data[i]+4)%4;
            int x = point[i-1].x;
            int y = point[i-1].y;
    
            // 细节:找出现在的位置
            switch(point[i-1].way) {
                case R: y += 1; break;
                case L: y -= 1; break;
                case U: x -= 1; break;
                case D: break;
            }
            switch (way) {
                case R: y += 1; break;
                case L: y -= 1; break;
                case U: break;
                case D: x += 1; break;
            }
                
            point[i] = Point(x, y, way);
    
            //printf("%d,%d,%c
    ",x,y,"RULD"[way]);
    
            umost = min(umost, x);
            dmost = max(dmost, x);
    
            lmost = min(lmost, y);
            rmost = max(rmost, y);
        }
        //printf("   %d 
    %d   %d
       %d
    ", umost, lmost, rmost, dmost);
        memset(mat, ' ', sizeof(mat));
        for (int i = 0; i < end; i++) {
            mat[point[i].x-umost][point[i].y-lmost] = (point[i].way == L || point[i].way == R)?'_':'|';
        }
        dmost = dmost - umost;
        rmost = rmost - lmost;
        for (int i = 0; i <= dmost; i++) {
            mat[i][rmost+1] = 0;
            int p = rmost;
            while (mat[i][p] == ' ') mat[i][p--] = 0;
            printf("%s
    ", mat[i]);
        }
        puts("^");
        //printf("   %d 
    %d   %d
       %d
    ", umost, lmost, rmost, dmost);
    }
    
    int main() {
        int n;
        while (scanf("%d", &n) != EOF) {
            if (n == 0) break;
            pp = 0;
            paper[pp++] = Paper(0,1<<n,true);
    
            memset(data, -1, sizeof(data));
            for (int i = 0; i < n; i++) {
                int nowpp = pp;
                for (int j = 0; j < nowpp; j++) {
                    flod(paper[j]);
                }
            }
    
            //for (int i = 1; i < (1<<n); i++) {
            //    printf("%c ", "^v"[data[i]==1]);
            //}puts("");
    
            showGraph(1<<n);
        }
        return 0;
    }
  • 相关阅读:
    3步轻松搞定Spring Boot缓存
    备战“金九银十”10道String高频面试题解析
    ConcurrentHashMap比其他并发集合的安全效率要高一些?
    3年java开发竟然还不知道Lambda的这个坑
    5分钟搞清楚Synchronized和Lock的概念与区别
    3年Java开发都知道的Redis数据结构和通用命令
    8月份21道最新Java面试题剖析(数据库+JVM+微服务+高并发)
    35个Java代码优化的细节,你知道几个?
    vba里面打开word文档,并实现通过特殊的字符将文档中的字符实现切分
    通过vba实现替换word里面指定的字符的方法
  • 原文地址:https://www.cnblogs.com/shinecheng/p/3993911.html
Copyright © 2011-2022 走看看