第一行输入 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;
}