Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<sstream>
- #include<vector>
- #include<map>
- using namespace std;
- struct Fire {
- int x, y;
- };
- struct Pos {
- int x, y;
- Pos(int x = 0, int y = 0) :x{ x }, y{ y } {}
- Pos(const Pos& p) :x{ p.x }, y{ p.y }{}
- Pos operator+=(const Pos& other) {
- x += other.x;
- y += other.y;
- return *this;
- }
- Pos operator+(const Pos& other) {
- Pos p;
- p.x = x+other.x;
- p.y = y+other.y;
- return p;
- }
- Pos operator-(const Pos& other) {
- Pos p;
- p.x = x - other.x;
- p.y = y - other.y;
- return p;
- }
- void operator=(const Pos& other) {
- copy(other);
- }
- Pos operator!() {
- return { -x,-y };
- }
- Pos operator&(int i) {
- return { x > 0 ? x & i : x & - i , y > 0 ? y & i : y & - i };
- }
- Pos operator|(int i) {
- return{ x == 0 ? 0 : x | i , y==0 ? 0 : y | i };
- }
- void copy(const Pos& other) {
- x = other.x;
- y = other.y;
- }
- };
- struct Maze {
- int sizex;
- int sizey;
- int t;
- int h, w;
- int selfpath, firepath;
- vector<string> m;
- vector<Fire> vf;
- Pos self;
- Pos exit;
- Maze(int sizex, int sizey, int time)
- :sizex{ sizex }, sizey{ sizey }, t{ time }, vf{}, h{ sizey }, w{ sizex }, selfpath{ 0 }, firepath{0}{
- stringstream ss;
- ss << "*****************\n"; //obviously cheap attempt at io
- ss << "*...*.......**..*\n";
- ss << "**..*....*.*.*..*\n";
- ss << "*......*.**.**.**\n";
- ss << "*..**...**..**.**\n";
- ss << "**.....**..*.*..*\n";
- ss << "*....*..........*\n";
- ss <<"*.....****.*...**\n";
- ss <<"****.*.*........*\n";
- ss <<"*****************\n" ;
- for (auto i = 0; i < sizey; i++) {
- string s{};
- ss >> s;
- m.push_back(s);
- }
- }
- Pos movePriority() {
- vector<Pos> vp;
- Pos p = self - exit;
- Pos maxp = self - exit;
- maxp = !p;
- p = !p;
- p = p | 1;
- p = p & 1;
- p = self + p;
- if (m[p.x][p.y] == '.') {
- return p;
- }
- else if(abs(maxp.x)>abs(maxp.y) && m[self.x+1][self.y]=='.') {
- self.x += 1;
- return self;
- }
- else if (abs(maxp.y) > abs(maxp.x) && m[self.x][self.y + 1] == '.') {
- self.y += 1;
- return self;
- }
- else if (m[self.x][self.y + 1] == '.') {
- self.y += 1;
- return self;
- }
- else if (m[self.x + 1][self.y] == '.') {
- self.x += 1;
- return self;
- }
- else {
- for (int j = self.y - 1; j < self.y + 2 && j < w; j++) {
- for (int i = self.x - 1; i < self.x + 2 && i < h; i++) {
- if (m[i][j] == '.') {
- return { i,j };
- }
- }
- }
- return{ -1,-1 };
- }
- }
- bool nextToExit() {
- Pos p = self - exit; \
- cout << p.x << " " << p.y << "\n\n";
- if (abs(p.x) < 2 && abs(p.y) < 2 && abs(p.x) + abs(p.y) < 3) {
- return true;
- }
- return false;
- }
- void setSelf(const Pos& p) {
- m[p.x][p.y] = 'x';
- self.x = p.x;
- self.y = p.y;
- }
- void setExit(const Pos& e) {
- m[e.x][e.y] = 'e';
- exit = e;
- }
- void setFire(int fx, int fy) {
- vf.push_back({ fx,fy });
- m[fx][fy] = 'f';
- }
- void burn() {
- vector<Fire> fire{};
- for (auto& f:vf){
- for (int k = f.y - 1; k < f.y + 2 && k < w; k++) {
- for (int l = f.x - 1; l < f.x + 2 && l < h; l++) {
- if (m[l][k] == '.') {
- fire.push_back({ l,k });
- }
- }
- }
- }
- //update map
- for (auto& f : fire) {
- vf.push_back({ f.x,f.y });
- m[f.x][f.y] = 'f';
- }
- }
- void print() {
- for (int i = 0; i < sizey; i++) {
- cout << m[i] << "\n";
- }
- cout << '\n';
- }
- };
- //*****************
- //*fff*.......**..*
- //**f.*....*.*.*..*
- //*..e...*.**.**.**
- //*x.**...**..**.**
- //**.....**..*.*..*
- //*....*..........*
- //*.....****.*...**
- //****.*.*........*
- //*****************
- int main() {
- int w = 10;
- int h = 17;
- int t = 4;
- Maze mz(17, 10,t);
- mz.setFire(1, 1);
- mz.setSelf({ 4,1 });
- mz.setExit({ 3,2 });
- mz.print();
- Pos move{};
- while (!mz.nextToExit()) {
- mz.burn();
- mz.print();
- move = mz.movePriority();
- if (move.x == -1 || move.y == -1) {
- cout << "help";
- break;
- }
- else {
- mz.self = move;
- mz.m[move.x][move.y] = 'x';
- t++;
- }
- }
- if (move.x != -1 && move.y != -1) {
- mz.print();
- cout << t + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement