202009-3 点亮数字人生

202009-3 点亮数字人生

一. 原理

这道题本身是一个图的题。可以将每个逻辑门抽象为图的节点,输入输出抽象为有向边。
题目需要根据输入计算每个逻辑门的输出,并且要求判断是否有环。
那么正常这就是一个标准DAG图(有向无环图)。对DAG图可进行拓扑排序操作,在进行排序后逐一输出每个门的output。
若经过拓扑排序后,还有部分节点没有被排序,则说明该图有环。

二. 代码

#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <stack>

using namespace std;

struct node{

    int id;
    int type;
//input 0
//NOT	1	非
//AND	2		与
//OR	3		或
//XOR	4		异或
//NAND	5		与非(先全部与后取非)
//NOR   6
    vector<node*> childs;
    vector<node*> fathers;

    node(){
        type = 0;
    }
};

struct mechine{

    vector<node*> inputs;
    vector<node*> doors;
    vector<node*> sort_doors;

    vector<bool> output;

    mechine(int m,int n){
        //m input
        //n doors
        for(int i = 0; i < m;i++){
            inputs.push_back(new node());
        }
        for(int j = 0; j < n;j++){
            doors.push_back(new node());
        }
    }

    void add_doors(int p,int type){
        //p doors's number
        //type door's type
        doors[p]->id = p;
        doors[p]->type = type;
    }

    void add_eage(int p,int input_num,bool input){
        //p doors's number
        //input_num input's num
        //input or output by door
        if(input){
            inputs[input_num]->childs.push_back(doors[p]);
            doors[p]->fathers.push_back(inputs[input_num]);
        }else{
            doors[input_num]->childs.push_back(doors[p]);
            doors[p]->fathers.push_back(doors[input_num]);
        }

    }

    bool topology(){
        vector<int> curr_count(doors.size(),0);
        queue<node*> myqueue;
        //init
        for(int i = 0; i < inputs.size(); i++){
            inputs[i]->id = i;
            myqueue.push(inputs[i]);
        }

        while(!myqueue.empty()){
            node* curr = myqueue.front();
            myqueue.pop();
            if(curr->type != 0){
                sort_doors.push_back(curr);
            }
            for(int i = 0; i < curr->childs.size();i++){
                curr_count[curr->childs[i]->id]++;
                if(curr_count[curr->childs[i]->id] == curr->childs[i]->fathers.size()){
                    myqueue.push(curr->childs[i]);
                }
            }
        }

        return sort_doors.size() == doors.size();
    }

    bool addflag(bool flag,bool add,int type){
        switch(type){
                        case 1:
                            //不可能为这种情况
                            break;
                        case 2:
                            flag = flag && add;
                            break;
                        case 3:
                            flag = flag || add;
                            break;
                        case 4:
                            flag = (flag && !add) || (!flag && add);
                            break;
                        case 5:
                            flag = !(flag && add);
                            break;
                        case 6:
                            flag = !(flag || add);
                            break;
                        default:
                            break;
       }
       return flag;
    }

    void cal(vector<bool>& input_value){
        output.clear();
        output.resize(doors.size());

        //init
        for(int i = 0; i < sort_doors.size(); i++){
            bool flag ;
            node* curr = sort_doors[i];
            for(int j = 0; j < curr->fathers.size();j++){
                node* curr_father = curr->fathers[j];
                if(curr_father->type == 0){
                    if(j == 0){
                        if(curr->type == 1){
                            flag = (bool)(!input_value[curr_father->id]);
                        }else{
                            flag = (bool)input_value[curr_father->id];
                        }
                    }else{
//                        printf("flag:%d %d %d\n",flag,(bool)input_value[curr_father->id],curr->type);
                        flag = addflag(flag,(bool)input_value[curr_father->id],curr->type);
//                        printf("flag:%d\n",flag);
                    }
                }else{
                    if(j == 0){
                        if(curr->type == 1){
                            flag = (!output[curr_father->id]);
                        }else{
                            flag = output[curr_father->id];
                        }
                    }else{
                        flag = addflag(flag,output[curr_father->id],curr->type);
                    }
                }
            }
            output[curr->id] = flag;

//            printf("output:%d %d %d\n",curr->id,curr->type,(int)output[curr->id]);
        }
    }

};

int main(){

    int q;
    scanf("%d",&q);
    for(int i = 0; i < q; i++){
        int M,n;
        scanf("%d %d",&M,&n);
        mechine m(M,n);
        char str[6];

        for(int j = 0; j < n; j++){
            int type;
            string func;
            int input_num;
            scanf("%s %d",str,&input_num);
            func = str;
            if(func == "NOT")
                type = 1;
            else if(func == "AND")
                type = 2;
            else if(func == "OR")
                type = 3;
            else if(func == "XOR")
                type = 4;
            else if(func == "NAND")
                type = 5;
            else if(func == "NOR")
                type = 6;
            else
                type = 0;
            //add door
            m.add_doors(j,type);
            //add eages
            for(int k = 0 ; k < input_num; k++){
                char temp;
                int p;
                scanf("%c",&temp);
                scanf("%c%d",&temp,&p);
                if(temp == 'I'){
                    m.add_eage(j,p-1,true);
                }else if(temp == 'O'){
                    m.add_eage(j,p-1,false);
                }else{
                }
            }
        }

        int s;
        scanf("%d",&s);
        vector<vector<bool> > input_value_group(s);
        for(int j = 0; j < s; j++){
            for(int k = 0; k < M; k++){
                bool temp;
                scanf("%d",&temp);
//                printf("%d\n",temp);
                input_value_group[j].push_back(temp);
            }
        }
        bool loop = !m.topology();
        //cal
        for(int j = 0; j < s; j++){
            int num_re;
            scanf("%d",&num_re);
            if(!loop){
                m.cal(input_value_group[j]);
            }
            for(int k = 0; k < num_re; k++){
                int want_p;
                scanf("%d",&want_p);
                if(!loop){
                   printf("%d ",(int)m.output[want_p-1]);
                }
            }
            if(!loop){
               printf("\n");
            }
        }

        if(loop){
            printf("LOOP\n");
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
点亮WS2812 LED灯带(也被称为Neopixel灯带),可以通过ESP32-S3的GPIO引脚和相应的库来实现。以下是一个简单的步骤: 1. 准备硬件:连接WS2812灯带到ESP32-S3的GPIO引脚上。确保引脚与代码中设置的引脚一致,并根据灯带的电源要求提供适当的电源。 2. 安装库:使用Arduino开发环境或者PlatformIO,安装支持WS2812灯带控制的库,如FastLED或Adafruit_NeoPixel库。这些库提供了方便的函数和工具来控制WS2812灯带。 3. 编写代码:在你的代码中导入相关的库,并初始化WS2812灯带。根据你的需求,设置灯带的颜色、亮度、模式等。 4. 控制灯带:使用库提供的函数来控制灯带。例如,可以使用setPixelColor函数来设置每个LED的颜色,使用show函数来更新灯带显示。你还可以根据需要编写动画效果或其他自定义功能。 5. 上传和运行:将编写好的代码上传到ESP32-S3,并观察WS2812灯带是否正确点亮。 以下是一个使用FastLED库控制WS2812灯带的示例代码: ```cpp #include <FastLED.h> #define LED_PIN 4 // 设置WS2812灯带连接到ESP32-S3的GPIO引脚 #define NUM_LEDS 10 // 灯带中LED的数量 CRGB leds[NUM_LEDS]; // 定义一个CRGB类型的数组用于存储每个LED的颜色 void setup() { FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); // 初始化灯带 } void loop() { // 设置灯带的颜色 for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB(255, 0, 0); // 设置为红色 } FastLED.show(); // 更新灯带显示 delay(1000); // 延时1秒 // 清除灯带 for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB(0, 0, 0); // 设置为黑色 } FastLED.show(); // 更新灯带显示 delay(1000); // 延时1秒 } ``` 请注意,以上代码仅为示例,你可以根据需要进行修改和扩展。确保你的硬件连接正确,并根据实际情况调整代码中的引脚和灯带参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值