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

zoj 1071 Follow My Logic

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

zoj 1071 Follow My Logic

#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>using namespace std;enum {    SIZ = 104,    AND = ')',    OR  = '>',};struct Node {    bool rev;    int type;    int v, l, r;    Node *link;};char pic[SIZ][SIZ];Node tree[SIZ];int num, x, y, row;Node * pnt[SIZ]; int move[4][2] = {    {-1, 0},    {0, 1},    {1, 0},    {0, -1}};bool valid(int r, int c){    return (r>=0)&&(r<row) &&(c>=0&&c<100);}int calc(int p){    if (tree[p].v != -1){        return tree[p].v;    }    int a = calc(tree[p].l);    int b = calc(tree[p].r);    if (tree[p].type == AND){        a = (a&&b);    } else if (tree[p].type == OR){        a = (a||b);    }    if (tree[p].rev)        a = 1 - a;    tree[p].v = a;        return tree[p].v;}bool match(char ch, int d){    if (ch <= 32) return false;    if (d%2==0 && ch=='-')        return false;    if (d%2==1 && ch=='|')        return false;    return true;}void build_tree(int r, int c){    Node &o = tree[num];    o.rev  = false;     o.v = o.l = o.r = -1; o.link = 0;    o.type = 0;    int d = 3;    if (num == 0){        for (d=0; d<4; ++d){ if (!valid(r+move[d][0], c+move[d][1]))     continue; char ch = pic[r+move[d][0]][c+move[d][1]]; if (ch > 32)      break;        }    }    ++num;    do {        r += move[d][0];        c += move[d][1];        switch(pic[r][c]){        case ')': o.type = AND; break;        case '>': o.type = OR; break;        case 'o': o.rev = !o.rev; break;        case '+': {     int od = (d+3)%4;     for (d=(d+1)%4; d!=od; d=(d+2)%4){         int nx = r + move[d][0];         int ny = c + move[d][1];         if (valid(nx,ny) && match(pic[nx][ny],d)){  r = nx, c = ny;  break;         }     } }        default: if (isalpha(pic[r][c])){     o.type = pic[r][c]; } break;        }    } while(o.type == 0);    if (isalpha(o.type)){        int p = o.type - 'A';        o.link = pnt[p];        pnt[p] = &o    } else {        o.l = num;        build_tree(r+move[0][0], c+move[0][1]-1);        o.r = num;        build_tree(r+move[2][0], c+move[2][1]-1);    }}void fun(){    int i;    const char *ans[2] = { "0", "1" };    num = 0;    memset(pnt, 0, sizeof(pnt));    build_tree(x, y);    cin.getline(pic[0], SIZ);    while(pic[0][0] != '*'){        for (i=0; i<num; i++){ tree[i].v = -1;        }        for (i=0; pic[0][i]; i++){ int v; if (pnt[i] == 0) continue; v = (pic[0][i]=='1'); for (Node *p = pnt[i]; p; p=p->link){     p->v = v;     if (p->rev){         p->v = 1 - p->v;     } }        }        i = calc(0);        printf("%sn", ans[i]);        cin.getline(pic[0], SIZ);    }}int readIn(){    row = 0; x = 0, y = 0;    char *s;    memset(pic, 0, sizeof(pic));    cin.getline(pic[row], SIZ);    while(cin && pic[row][0] != '*'){        for (int i=0; pic[row][i]; ++i){ if (pic[row][i] == '?'){     x = row; y = i; }        }        ++row;        cin.getline(pic[row], SIZ);    }    return row;}int main(){    int tst = 0;    while(readIn() > 0){        if (tst++) printf("n");        fun();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380023.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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