Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // РЕАЛИЗАЦИЯ СБОРЩИКА ЯБЛОК
- bool robot::shortest_coll(const int x_in_search,const int y_in_search,const int maximum_x,const int maximum_y, cell_value **tmp_map,just_map &god_map) {
- int tmp_rob_x = X + maximum_x;
- int tmp_rob_y = Y + maximum_y;
- std::vector<r_search> reach_data;
- for(auto & m : map.data){
- if(m.get_value() == APPLE){
- r_search new_cell;
- new_cell.set_X(m.get_X()+maximum_x);
- new_cell.set_Y(m.get_Y()+maximum_y);
- new_cell.set_value(APPLE);
- new_cell.set_to_go_point(abs(new_cell.get_X() - x_in_search)+abs(new_cell.get_Y() - y_in_search));
- reach_data.push_back(new_cell);
- }
- if(m.get_value() == EMPTY){
- r_search new_cell;
- new_cell.set_X(m.get_X()+maximum_x);;
- new_cell.set_Y(m.get_Y()+maximum_y);;
- new_cell.set_value(EMPTY);
- new_cell.set_to_go_point(abs(new_cell.get_X() - x_in_search)+abs(new_cell.get_Y() - y_in_search));
- reach_data.push_back(new_cell);
- }
- if(m.get_value() == ROBOT_COLLECTOR){
- r_search new_cell;
- new_cell.set_X(m.get_X()+maximum_x);;
- new_cell.set_Y(m.get_Y()+maximum_y);;
- new_cell.set_value(ROBOT_COLLECTOR);
- new_cell.set_to_go_point(abs(new_cell.get_X() - x_in_search)+abs(new_cell.get_Y() - y_in_search));
- reach_data.push_back(new_cell);
- }
- if(m.get_value() == ROBOT_SAPPER){
- r_search new_cell;
- new_cell.set_X(m.get_X()+maximum_x);;
- new_cell.set_Y(m.get_Y()+maximum_y);;
- new_cell.set_value(ROBOT_SAPPER);
- new_cell.set_to_go_point(abs(new_cell.get_X() - x_in_search)+abs(new_cell.get_Y() - y_in_search));
- reach_data.push_back(new_cell);
- }
- }
- std::vector<r_search> reachable;
- add_siblings(tmp_rob_x, tmp_rob_y, 0, reach_data, reachable);
- std::vector<r_search> explored;
- for(int i = 0; i < reach_data.size(); i++){
- if(reach_data[i].get_value() == ROBOT_COLLECTOR){
- r_search new_cell;
- new_cell.set_X(reach_data[i].get_X());
- new_cell.set_Y(reach_data[i].get_Y());
- new_cell.set_value(reach_data[i].get_value());
- explored.push_back(new_cell);
- }
- }
- bool trap;
- int x_to_find;
- int y_to_find;
- int itr = 1;
- bool is_reached = false;
- while(!is_reached){
- r_search tmp;
- tmp.set_to_go_point(1000);
- tmp.set_to_from_point(1000);
- tmp.set_from_to_sum(2000);
- for(auto & i : reachable){
- if(i.get_from_to_sum() <= tmp.get_from_to_sum()){
- if(!is_added(i.get_X(), i.get_Y(), explored)) {
- if (i.get_from_to_sum() == tmp.get_from_to_sum()) {
- if (i.get_to_go_point() < tmp.get_to_from_point()) {
- tmp = i;
- trap = false;
- }
- }
- if (i.get_from_to_sum() < tmp.get_from_to_sum()) {
- tmp = i;
- trap = false;
- }
- }
- }
- }
- if(trap){
- reach_data.clear();
- reachable.clear();
- explored.clear();
- return false;
- }
- explored.push_back(tmp);
- for (int i = 0; i < reachable.size(); i++){
- if(reachable[i].get_X() == tmp.get_X() && reachable[i].get_Y() == tmp.get_Y()){
- reachable.erase(reachable.begin() + i);
- }
- }
- add_siblings(tmp.get_X(), tmp.get_Y(), tmp.get_to_from_point(), reach_data, reachable);
- if((abs(tmp.get_X() - x_in_search) + abs(tmp.get_Y() - y_in_search)) == 1){
- is_reached = true;
- }
- itr++;
- }
- // обратный путь по explored-у
- r_search tmp;
- r_search ex_tmp;
- for(auto & i : explored){
- if((abs(i.get_X() - x_in_search) + abs(i.get_Y()- y_in_search)) == 1){
- tmp = i;
- }
- }
- std::vector<auto_command> way_out;
- int iterator = 1;
- while(tmp.get_X() != tmp_rob_x || tmp.get_Y() != tmp_rob_y){
- take_step_back(iterator, tmp, way_out);
- for (int i = 0; i < explored.size(); i++){
- if(explored[i].get_X() == tmp.get_previous_x() && explored[i].get_Y() == tmp.get_previous_y()){
- ex_tmp.set_value(tmp.get_value());
- ex_tmp.set_X(tmp.get_X());
- ex_tmp.set_Y(tmp.get_Y());
- tmp = explored[i];
- }
- }
- iterator++;
- }
- if(ex_tmp.get_value() == ROBOT_SAPPER){
- reachable.clear();
- explored.clear();
- way_out.clear();
- if(is_blocked(x_in_search, y_in_search, maximum_x, maximum_y, tmp_map)){
- return false;
- }
- add_siblings(tmp_rob_x, tmp_rob_y, 0, reach_data, reachable);
- for(int i = 0; i < reach_data.size(); i++){
- if(reach_data[i].get_value() == ROBOT_COLLECTOR){
- r_search new_cell;
- new_cell.set_X(reach_data[i].get_X());
- new_cell.set_Y(reach_data[i].get_Y());
- new_cell.set_value(reach_data[i].get_value());
- explored.push_back(new_cell);
- }
- }
- trap = true;
- itr = 1;
- is_reached = false;
- int counter = 0;
- while(!is_reached){
- if (counter == 25){
- reachable.clear();
- explored.clear();
- way_out.clear();
- return false;
- }
- tmp.set_to_go_point(1000);
- tmp.set_to_from_point(1000);
- tmp.set_from_to_sum(2000);
- for(auto & i : reachable){
- if(i.get_from_to_sum() <= tmp.get_from_to_sum() && i.get_value() != ROBOT_SAPPER){
- if(!is_added(i.get_X(), i.get_Y(), explored)) {
- if (i.get_from_to_sum() == tmp.get_from_to_sum()) {
- if (i.get_to_go_point() < tmp.get_to_from_point()) {
- tmp = i;
- trap = false;
- }
- }
- if (i.get_from_to_sum() < tmp.get_from_to_sum()) {
- tmp = i;
- trap = false;
- }
- }
- }
- }
- if(trap){
- reach_data.clear();
- reachable.clear();
- explored.clear();
- way_out.clear();
- return false;
- }
- counter++;
- explored.push_back(tmp);
- for (int i = 0; i < reachable.size(); i++){
- if(reachable[i].get_X() == tmp.get_X() && reachable[i].get_Y() == tmp.get_Y()){
- reachable.erase(reachable.begin() + i);
- }
- }
- add_siblings(tmp.get_X(), tmp.get_Y(), tmp.get_to_from_point(), reach_data, reachable);
- if((abs(tmp.get_X() - x_in_search) + abs(tmp.get_Y() - y_in_search)) == 1){
- is_reached = true;
- }
- itr++;
- }
- // обратный путь по explored-у
- for(auto & i : explored){
- if((abs(i.get_X() - x_in_search) + abs(i.get_Y()- y_in_search)) == 1){
- tmp = i;
- }
- }
- iterator = 1;
- while(tmp.get_X() != tmp_rob_x || tmp.get_Y() != tmp_rob_y){
- take_step_back(iterator, tmp, way_out);
- for (int i = 0; i < explored.size(); i++){
- if(explored[i].get_X() == tmp.get_previous_x() && explored[i].get_Y() == tmp.get_previous_y()){
- tmp = explored[i];
- }
- }
- iterator++;
- }
- }
- iterator--;
- for(int i = 0; i < way_out.size(); i++){
- if(way_out[i].get_idx() == iterator){
- move(way_out[i].get_value(), god_map);
- }
- }
- reach_data.clear();
- reachable.clear();
- explored.clear();
- way_out.clear();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement