码迷,mamicode.com
首页 > 其他好文 > 详细

uva 177:Paper Folding(模拟 Grade D)

时间:2014-09-26 02:38:48      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

题目链接

题意:一张纸,每次从右往左对折。折好以后打开,让每个折痕都自然的呈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\n", 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\n",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\n",x,y,"RULD"[way]);

        umost = min(umost, x);
        dmost = max(dmost, x);

        lmost = min(lmost, y);
        rmost = max(rmost, y);
    }
    //printf("   %d \n%d   %d\n   %d\n", 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\n", mat[i]);
    }
    puts("^");
    //printf("   %d \n%d   %d\n   %d\n", 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;
}

 

uva 177:Paper Folding(模拟 Grade D)

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/shinecheng/p/3993911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!