栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1043 Split Windows

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1043 Split Windows

#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define UD -1#define LR -2#define maxn 256#define maxlen 1024const double EPS = 1e-2;typedef struct node{    int w, h;    int type;    int lson, rson;}node;node win[maxn];char str[maxlen][maxlen];bool isletter(char x){    if (x!='-' && x!='|') return true;    return false;}void streach_w(int k, int w){    win[k].w = w;    if (win[k].type>=0) return;    else if (win[k].type==UD)    {        streach_w(win[k].lson, w);        streach_w(win[k].rson, w);    }    else    {        int w1 = (int)(ceil(1.0*w*win[win[k].lson].w/(win[win[k].lson].w+win[win[k].rson].w))+EPS);        streach_w(win[k].lson, w1);        streach_w(win[k].rson, w-w1);        win[k].w=w;    }}void streach_h(int k, int h){    win[k].h = h;    if (win[k].type>=0) return;    else if (win[k].type==LR)    {        streach_h(win[k].lson, h);        streach_h(win[k].rson, h);    }    else    {        int h1 = (int)(ceil(1.0*h*win[win[k].lson].h/(win[win[k].lson].h+win[win[k].rson].h))+EPS);        streach_h(win[k].lson, h1);        streach_h(win[k].rson, h-h1);    }}void print(int w, int h, char str[][maxlen]){    for (int i=0; i<=h; i++)    {        for (int j=0; j<=w; j++) printf("%c", str[i][j]);        printf("n");    }}void clear(int h, int w, char str[][maxlen]){    for (int j=0; j<=w; j++)        str[0][j] = str[h][j] = '-';    for (int i=0; i<=h; i++)        str[i][0] = str[i][w] = '|';    str[0][0] = str[0][w] = str[h][0] = str[h][w] = '*';    for (int i=1; i<h; i++)        for (int j=1; j<w; j++) str[i][j]=' ';}void cre_window(int k, int line1, int line2, int col1, int col2, char str[][maxlen]){    if (win[k].type>=0)    {        str[line1][col1] = win[k].type + 'A';        return;    }    else if (win[k].type==UD)    {        int line = line1 + win[win[k].lson].h;        for (int i=col1+1; i<col2; i++) str[line][i] = '-';        str[line][col1] = str[line][col2] = '*';        cre_window(win[k].lson, line1, line, col1, col2, str);        cre_window(win[k].rson, line, line2, col1, col2, str);    }    else     {        int col = col1 + win[win[k].lson].w;        for (int i=line1+1; i<line2; i++) str[i][col] = '|';        str[line1][col] = str[line2][col] = '*';        cre_window(win[k].lson, line1, line2, col1, col, str);        cre_window(win[k].rson, line1, line2, col, col2, str);    }}int cal_size(int k, int l, int ∑, char tree[]){    if (isletter(tree[l]))    {        win[k].w = win[k].h = 2;        win[k].lson = win[k].rson = -1;        win[k].type = tree[l]-'A';        if (k==0)        { clear(win[0].h, win[0].w, str); cre_window(0, 0, win[0].h, 0, win[0].w, str); print(win[0].w, win[0].h, str);        }        return l;    }    else{        int tmp;        win[k].lson = ++sum; tmp = cal_size(win[k].lson, l+1, sum, tree);        win[k].rson = ++sum; tmp = cal_size(win[k].rson, tmp+1, sum, tree);        win[k].type = (tree[l]=='-') ? UD : LR;        if (win[k].type==UD)          { if (win[win[k].lson].w!=win[win[k].rson].w) {     if (win[win[k].lson].w > win[win[k].rson].w)     {         win[k].w = win[win[k].lson].w;         streach_w(win[k].rson, win[k].w);     }     else     {         win[k].w = win[win[k].rson].w;         streach_w(win[k].lson, win[k].w);     } } else win[k].w = win[win[k].lson].w; win[k].h = win[win[k].lson].h + win[win[k].rson].h; if (k==0) {     clear(win[0].h, win[0].w, str);     cre_window(0, 0, win[0].h, 0, win[0].w, str);     print(win[0].w, win[0].h, str); } return tmp;        }        else   { if (win[win[k].lson].h!=win[win[k].rson].h) {     if (win[win[k].lson].h > win[win[k].rson].h)     {         win[k].h = win[win[k].lson].h;         streach_h(win[k].rson, win[k].h);     }     else     {         win[k].h = win[win[k].rson].h;         streach_h(win[k].lson, win[k].h);     } } else win[k].h = win[win[k].lson].h; win[k].w = win[win[k].lson].w + win[win[k].rson].w; if (k==0) {     clear(win[0].h, win[0].w, str);     cre_window(0, 0, win[0].h, 0, win[0].w, str);     print(win[0].w, win[0].h, str); } return tmp;        }    }    return -1;}int main(){    int cs, sum;    char tree[maxlen];    node win[maxn];    scanf("%d", &cs);    for (int cases=1; cases<=cs; cases++)    {        sum=0;        scanf("%s", tree);        printf("%dn", cases);        cal_size(0, 0, sum, tree);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371933.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号