Advertisement
markyrocks

mazerunner

May 21st, 2022 (edited)
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. #include<vector>
  5. #include<map>
  6. using namespace std;
  7.  
  8. struct Fire {
  9.     int x, y;
  10. };
  11.  
  12. struct Pos {
  13.     int x, y;
  14.     Pos(int x = 0, int y = 0) :x{ x }, y{ y } {}
  15.     Pos(const Pos& p) :x{ p.x }, y{ p.y }{}
  16.     Pos operator+=(const Pos& other) {
  17.         x += other.x;
  18.         y += other.y;
  19.         return *this;
  20.     }
  21.     Pos operator+(const Pos& other) {
  22.         Pos p;
  23.         p.x = x+other.x;
  24.         p.y = y+other.y;
  25.         return p;
  26.     }
  27.     Pos operator-(const Pos& other) {
  28.         Pos p;
  29.         p.x = x - other.x;
  30.         p.y = y - other.y;
  31.         return p;
  32.     }
  33.     void operator=(const Pos& other) {
  34.         copy(other);
  35.     }
  36.     Pos operator!() {
  37.         return { -x,-y };
  38.     }
  39.     Pos operator&(int i) {
  40.         return { x > 0 ? x & i : x & - i , y > 0 ? y & i : y & - i };
  41.     }
  42.     Pos operator|(int i) {
  43.         return{ x == 0 ? 0 : x | i , y==0 ? 0 : y | i };
  44.     }
  45.     void copy(const Pos& other) {
  46.         x = other.x;
  47.         y = other.y;
  48.     }
  49. };
  50.  
  51. struct Maze {
  52.     int sizex;
  53.     int sizey;
  54.     int t;
  55.     int h, w;
  56.     int selfpath, firepath;
  57.     vector<string> m;
  58.     vector<Fire> vf;
  59.     Pos self;
  60.     Pos exit;
  61.     Maze(int sizex, int sizey, int time)
  62.         :sizex{ sizex }, sizey{ sizey }, t{ time }, vf{}, h{ sizey }, w{ sizex }, selfpath{ 0 }, firepath{0}{
  63.         stringstream ss;
  64.  
  65.         ss << "*****************\n";  //obviously cheap attempt at io
  66.         ss << "*...*.......**..*\n";
  67.         ss << "**..*....*.*.*..*\n";
  68.         ss << "*......*.**.**.**\n";
  69.         ss << "*..**...**..**.**\n";
  70.         ss << "**.....**..*.*..*\n";
  71.         ss << "*....*..........*\n";
  72.         ss <<"*.....****.*...**\n";
  73.         ss <<"****.*.*........*\n";
  74.         ss <<"*****************\n" ;
  75.  
  76.         for (auto i = 0; i < sizey; i++) {
  77.             string s{};
  78.             ss >> s;
  79.             m.push_back(s);
  80.         }
  81.     }
  82.    
  83.  
  84.     Pos movePriority() {
  85.         vector<Pos> vp;
  86.         Pos p = self - exit;
  87.         Pos maxp = self - exit;
  88.         maxp = !p;
  89.         p = !p;
  90.         p = p | 1;
  91.         p = p & 1;
  92.                
  93.         p = self + p;
  94.         if (m[p.x][p.y] == '.') {
  95.             return p;
  96.         }
  97.         else if(abs(maxp.x)>abs(maxp.y) && m[self.x+1][self.y]=='.') {
  98.             self.x += 1;
  99.             return self;
  100.         }
  101.         else if (abs(maxp.y) > abs(maxp.x) && m[self.x][self.y + 1] == '.') {
  102.             self.y += 1;
  103.             return  self;
  104.         }
  105.         else if (m[self.x][self.y + 1] == '.') {
  106.             self.y += 1;
  107.             return self;
  108.         }
  109.         else if (m[self.x + 1][self.y] == '.') {
  110.             self.x += 1;
  111.             return self;
  112.         }
  113.         else {
  114.             for (int j = self.y - 1; j < self.y + 2 && j < w; j++) {
  115.                 for (int i = self.x - 1; i < self.x + 2 && i < h; i++) {
  116.                     if (m[i][j] == '.') {
  117.                         return { i,j };
  118.                     }
  119.                 }
  120.             }
  121.             return{ -1,-1 };
  122.  
  123.         }
  124.     }
  125.  
  126.     bool nextToExit() {
  127.         Pos p = self - exit; \
  128.             cout << p.x << " " << p.y << "\n\n";
  129.         if (abs(p.x) < 2 && abs(p.y) < 2 && abs(p.x) + abs(p.y) < 3) {
  130.             return true;
  131.         }
  132.         return false;
  133.     }
  134.  
  135.     void setSelf(const Pos& p) {
  136.         m[p.x][p.y] = 'x';
  137.         self.x = p.x;
  138.         self.y = p.y;
  139.     }
  140.     void setExit(const Pos& e) {
  141.         m[e.x][e.y] = 'e';
  142.         exit = e;
  143.     }
  144.  
  145.     void setFire(int fx, int fy) {
  146.         vf.push_back({ fx,fy });
  147.         m[fx][fy] = 'f';
  148.     }
  149.  
  150.     void burn() {
  151.         vector<Fire> fire{};
  152.         for (auto& f:vf){
  153.             for (int k = f.y - 1; k < f.y + 2 && k < w; k++) {
  154.                 for (int l = f.x - 1; l < f.x + 2 && l < h; l++) {
  155.                     if (m[l][k] == '.') {
  156.                         fire.push_back({ l,k });
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         //update map
  162.         for (auto& f : fire) {
  163.             vf.push_back({ f.x,f.y });
  164.             m[f.x][f.y] = 'f';
  165.         }
  166.     }
  167.     void print() {
  168.         for (int i = 0; i < sizey; i++) {
  169.             cout << m[i] << "\n";
  170.         }
  171.         cout << '\n';
  172.     }
  173.  
  174.  
  175. };
  176.  
  177.  
  178. //*****************
  179. //*fff*.......**..*
  180. //**f.*....*.*.*..*
  181. //*..e...*.**.**.**
  182. //*x.**...**..**.**
  183. //**.....**..*.*..*
  184. //*....*..........*
  185. //*.....****.*...**
  186. //****.*.*........*
  187. //*****************
  188.  
  189.  
  190. int main() {
  191.     int w = 10;
  192.     int h = 17;
  193.     int t = 4;
  194.     Maze mz(17, 10,t);
  195.     mz.setFire(1, 1);
  196.     mz.setSelf({ 4,1 });
  197.     mz.setExit({ 3,2 });
  198.  
  199.     mz.print();
  200.  
  201.     Pos move{};
  202.     while (!mz.nextToExit()) {
  203.         mz.burn();
  204.         mz.print();
  205.         move = mz.movePriority();
  206.         if (move.x == -1 || move.y == -1) {
  207.             cout << "help";
  208.             break;
  209.         }
  210.         else {
  211.             mz.self = move;
  212.             mz.m[move.x][move.y] = 'x';
  213.             t++;
  214.         }
  215.     }
  216.     if (move.x != -1 && move.y != -1) {
  217.         mz.print();
  218.         cout << t + 1;
  219.     }
  220.    
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement