输入三行后程序无响应卡死

第一行输入 3 4 4,第二行输入 row 1 on获得正确结果,但输入第三行 column 1 on时程序开始卡死



#include <iostream>
#include <string>
using namespace std;
int main()
{
    int nn,mm,q;
    cin >>nn >> mm >> q;
    struct Point
    {
        bool is_this_dark = false;
        bool is_this_crossing = false;
        int position = 0;
        Point * latter = nullptr;
        Point * former = nullptr;
    };
    struct Line
    {
        int position = 0;
        string on = "mango";
        Point * point = nullptr;
        Point * first_point = nullptr;
        Line * latter = nullptr;
        Line * former = nullptr;
    };
    class Light
    {
    public:
        Light(int n1,int m1)
        {
            this->n = n1;
            this->m = m1;
            this->hor_lines = &the_first_line_for_hor;
            this->ver_lines = &the_first_line_for_ver;
            this->first_hor_lines = this->hor_lines;
            this->first_ver_lines = this->ver_lines;
            this->hor_lines->point->former = nullptr;
            this->ver_lines->point->former = nullptr;
            this->hor_lines->point->latter = nullptr;
            this->ver_lines->point->latter = nullptr;
            this->hor_lines->first_point = this->hor_lines->point;
            this->ver_lines->first_point = this->ver_lines->point;
            this->hor_lines->latter = nullptr;
            this->ver_lines->latter = nullptr;
            this->hor_lines->former = nullptr;
            this->ver_lines->former = nullptr;
            this->temp_hor_lines = this->hor_lines;
            this->temp_ver_lines = this->ver_lines;
        }
        void Create_new_line()
        {
            cin >> direction;
            if (direction == standard)
            {
                cin >> hor_lines->position >> hor_lines->on;
                handle_hor_line(hor_lines->on == "on");
            }
            else
            {
                cin >> ver_lines->position >> ver_lines->on;
                handle_ver_line(ver_lines->on == "on");
                this->ver_lines->latter->former = this->ver_lines;
                this->ver_lines = this->ver_lines->latter;
            }
        }
        void handle_hor_line(bool is_this_on)
        {
            if (is_this_on)
            {
                while (this->temp_ver_lines->latter != nullptr)
                {
                    while(this->temp_ver_lines->point->latter != nullptr)
                    {
                        if (this->temp_ver_lines->point->position == hor_lines->position)
                        {
                            this->temp_ver_lines->point->is_this_dark = false;
                            this->temp_ver_lines->point->is_this_crossing = true;
                        }
                        this->temp_ver_lines->point = this->temp_ver_lines->point->latter;
                    }
                    this->temp_ver_lines->point = this->temp_ver_lines->first_point;
                }
                this->temp_ver_lines = first_ver_lines;
            }
            else
            {
                while (this->temp_hor_lines->latter != nullptr)        //循环检测是否与白线重合,若有则消除
                {
                    if (this->temp_hor_lines->position == this->hor_lines->position)
                    {
                        this->temp_hor_lines->former->latter = this->temp_hor_lines->latter;        //删除重合的白线
                        this->temp_hor_lines->latter->former = this->temp_hor_lines->former;
                    }
                    this->temp_hor_lines = this->temp_hor_lines->latter;
                }
                temp_hor_lines = first_hor_lines;
                while (this->temp_ver_lines->latter != nullptr)     //将经过的点调整为暗点并设置为未相交
                {
                    while(this->temp_ver_lines->point->latter != nullptr)
                    {
                        if(this->temp_ver_lines->point->position == this->hor_lines->position)
                        {
                            this->temp_ver_lines->point->is_this_crossing = false;
                            this->temp_ver_lines->point->is_this_dark = true;
                        }
                        this->temp_ver_lines->point = this->temp_ver_lines->point->latter;
                    }
                    this->temp_ver_lines->point = this->temp_ver_lines->first_point;
                    this->temp_ver_lines = this->temp_ver_lines->latter;
                }
                this->temp_ver_lines = this->first_ver_lines;
            }
        }
        void handle_ver_line(bool is_this_on)
        {
            if (is_this_on)
            {
                while (this->temp_hor_lines->latter != nullptr)
                {
                    while(this->temp_hor_lines->point->latter != nullptr)
                    {
                        if (this->temp_hor_lines->point->position == hor_lines->position)
                            this->temp_hor_lines->point->is_this_dark = false;
                        this->temp_hor_lines->point = this->temp_hor_lines->point->latter;
                    }
                    this->temp_hor_lines->point = this->temp_hor_lines->first_point;
                }
                this->temp_ver_lines = first_ver_lines;
            }
            else
            {
                while (this->temp_ver_lines->latter != nullptr)        //循环检测是否与白线重合,若有则消除
                {
                    if (this->temp_ver_lines->position == this->ver_lines->position)
                    {
                        this->temp_ver_lines->former->latter = this->temp_ver_lines->latter;        //删除重合的白线
                        this->temp_ver_lines->latter->former = this->temp_ver_lines->former;
                    }
                    this->temp_ver_lines = this->temp_ver_lines->latter;
                }
                this->temp_ver_lines = this->first_ver_lines;
                while (this->temp_hor_lines->latter != nullptr)
                {
                    while(this->temp_hor_lines->point->latter != nullptr)
                    {
                        if(this->temp_hor_lines->point->position == this->ver_lines->position)
                            this->temp_hor_lines->point->is_this_dark = true;
                        this->temp_hor_lines->point = this->temp_hor_lines->point->latter;
                    }
                    this->temp_hor_lines->point = this->temp_hor_lines->first_point;
                    this->temp_hor_lines = this->temp_hor_lines->latter;
                }
                this->temp_hor_lines = this->first_hor_lines;
            }
        }
        int calculate_and_report_lights()
        {
            while (this->temp_hor_lines->latter != nullptr)     //计算横线
            {
                this->on_lights_numbers += n;
                while (this->temp_hor_lines->point->latter != nullptr)
                {
                    if (this->temp_hor_lines->point->is_this_dark)
                        on_lights_numbers--;
                    this->temp_hor_lines->point = this->temp_hor_lines->point->latter;
                }
                this->temp_hor_lines->point = this->temp_hor_lines->first_point;
                this->temp_hor_lines = this->temp_hor_lines->latter;
            }
            this->temp_hor_lines = this->first_hor_lines;
            while (this->temp_ver_lines->latter != nullptr)     //计算横线
            {
                this->on_lights_numbers += m;
                while (this->temp_ver_lines->point->latter != nullptr)
                {
                    if (this->temp_ver_lines->point->is_this_dark && !this->temp_ver_lines->point->is_this_crossing)
                        on_lights_numbers--;
                    this->temp_ver_lines->point = this->temp_ver_lines->point->latter;
                }
                this->temp_ver_lines->point = this->temp_ver_lines->first_point;
                this->temp_ver_lines = this->temp_ver_lines->latter;
            }
            this->temp_ver_lines = this->first_ver_lines;
            return this->on_lights_numbers;
        }
        int time = 0;
    private:
        Line * hor_lines;       //横线
        Line * ver_lines;       //竖线
        Line * first_hor_lines;
        Line * first_ver_lines;
        Line * temp_hor_lines;
        Line * temp_ver_lines;
        Line the_first_line_for_hor;
        Line the_first_line_for_ver;
        int n,m;
        string direction;
        string standard = "row";
        int on_lights_numbers = 0;
    };
    Light lights(nn,mm);
    while (lights.time < q )
    {
        lights.Create_new_line();
        cout << lights.calculate_and_report_lights();
        lights.time++;
    }
    return 0;
}


这代码是GPT生成的,GPT生成的代码都是这个毛病,看上去气势如虹,其实如同说梦话错误百出,很正常。

【以下回答由 GPT 生成】

在给出的代码中,并没有看到明显的造成程序卡死的原因。需要更多的信息来进一步分析问题所在。请提供程序在卡死时的错误信息或任何调试信息,并根据需要提供更详细的描述。



【相关推荐】


  • 这篇博客: 三子棋——so easy!中的 1.这个二维数组呢,本来应该是3 *3类型。这里可以宏定义两个行与列(为什么呢?)因为如果写3*3了那么这个棋盘就固定了,而如果你想改变这个棋盘大小就可以直接对ROW,COL进行修改,而不用进入函数中对数据进行修改了。 部分也许能够解决你的问题。

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^