Skip to content

Instantly share code, notes, and snippets.

@qutal
Created April 22, 2020 15:02
Show Gist options
  • Save qutal/26b3b0c3abf5904c3e064cb4accd4cc6 to your computer and use it in GitHub Desktop.
Save qutal/26b3b0c3abf5904c3e064cb4accd4cc6 to your computer and use it in GitHub Desktop.
#pragma once
#include <iostream>
using namespace std;
class Matrix
{
protected: int N = -1, M = -1; int** matrix = NULL;
int begX = -1, begY = -1, endX = -1, endY = -1;
public:
int getN() {
return N;
}
int getM() {
return M;
}
int getBegX() {
return begX;
}
int getBegY() {
return begY;
}
int getEndX() {
return endX;
}
int getEndY() {
return endY;
}
int** getMatrix() {
return matrix;
}
int getMatrix(int i, int j) {
return matrix[i][j];
}
void setPassedElement(int i, int j) {
matrix[i][j] = 1;
}
Matrix(int n, int m, int bx, int by, int ex, int ey) {
srand(n * m * bx * by * ex * ey);
matrix = new int* [n];
for (int i = 0;i < n;i++) {
matrix[i] = new int[m];
for (int j = 0;j < m;j++) {
if (i == bx && j == by || i == ex && j == ey) {
matrix[i][j] = 0;
continue;
}
matrix[i][j] = rand() % 2 - 1;
}
}
begX = bx;
begY = by;
endX = ex;
endY = ey;
N = n;
M = m;
}
};
#pragma once
#include "Matrix.cpp"
#include "Matrix.h"
#include <iostream>
#include <string>
using namespace std;
class Output {
private:
string message;
void clear() {
message = "";
system("@cls||clear");
}
public:
void debug(Matrix *matrix) {
for (int i = 0;i < matrix->getN();i++) {
for (int j = 0;j < matrix->getM();j++) {
if (i == matrix->getBegX() && j == matrix->getBegY()) {
message += 'x';
}
else {
message += std::to_string(matrix->getMatrix(i, j));
}
message += '\t';
}
message += '\n';
}
print();
userListener();
}
void print() {
cout << message;
}
void move(Matrix *matrix, int currentX, int currentY) {
clear();
for (int i = 0;i < matrix->getN();i++) {
for (int j = 0;j < matrix->getM();j++) {
if (i == currentX && j == currentY) {
//message+= std::to_string(5)+'\t';
message += 'x';
message += '\t';
}
else {
message += std::to_string(matrix->getMatrix(i, j)) + '\t';
}
}
message += '\n';
}
print();
userListener();
}
void userListener() {
cout << "Input something to cintinue" << endl;
string trash;
cin >> trash;
}
void FINISH() {
clear();
message += "\n FINISH";
print();
}
void DEADEND() {
clear();
message += "\n DEADEND";
print();
}
};
#pragma once
#include <iostream>
#include "Matrix.h"
#include "Output.h"
using namespace std;
class Robot
{
private:
struct stack
{
int x = -1;
int y = -1;
int num = -1;
stack* next=NULL;
};
void Push(int x, int y, int num, stack** p) {
if ((p) != NULL) {
stack* q = new stack;
q->x = x;
q->y = y;
q->num = num;
q->next = *p;
(*p) = q;
}
else {
*p = new stack;
(*p)->x = x;
(*p)->y = y;
(*p)->num = num;
(*p)->next = NULL;
}
}
void Pop(int* x, int* y, int* num, stack** p) {
if ((*p) == NULL) {
return;
}
*x = (*p)->x;
*num = (*p)->num;
*y = (*p)->y;
(*p) = (*p)->next;
}
bool move() {
if (x + 1 < matrix->getN() && matrix->getMatrix(x+1,y) != -1 && matrix->getMatrix(x + 1, y) != 1) { // Down
x++;
matrix->setPassedElement(x, y);
step++;
Push(x, y, step, &t);
return true;
}
if (y > 0 && matrix->getMatrix(x, y-1) != -1 && matrix->getMatrix(x, y-1) != 1) { // Left
y--;
matrix->setPassedElement(x, y);
step++;
Push(x, y, step, &t);
return true;
}
if (x > 0 && matrix->getMatrix(x-1,y)!= -1 && matrix->getMatrix(x - 1, y) != 1) { // Up
x--;
matrix->setPassedElement(x, y);
step++;
Push(x, y, step, &t);
return true;
}
if (y + 1 < matrix->getM() && matrix->getMatrix(x,y+1) != -1 && matrix->getMatrix(x, y + 1) != 1) { // Right
y++;
matrix->setPassedElement(x, y);
step++;
Push(x, y, step, &t);
return true;
}
if (t != NULL) {
Pop(&x, &y, &step, &t);
return true;
}
return false;
}
Matrix *matrix = new Matrix(4, 7, 0, 0, 3, 6);
Output* output = new Output();
stack* t = NULL;
int x = 0, y = 0,
step = 0;
public:
void RUN() {
output->debug(matrix);
while (!(x == matrix->getEndX() && y == matrix->getEndY())) {
if (move()) {
output->move(matrix, x, y);
}
else {
output->DEADEND();
return;
}
}
output->FINISH();
}
};
// ПрактикаСРоботом.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#include <iostream>
#include "Robot.h"
using namespace std;
int main()
{
Robot *robot = new Robot();
robot->RUN();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment