Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab3 #1195

Open
wants to merge 73 commits into
base: master
Choose a base branch
from
Open

Lab3 #1195

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
bcdd08d
Create Леванин Николай 3092
nikolaievanin Sep 11, 2024
7d2c258
Change commit
Nov 25, 2024
e09012a
Lab1 commit
Nov 25, 2024
fd24ba9
Lab1 commit!
Nov 25, 2024
e770618
lab1 commit!
Nov 25, 2024
fb667ee
lab1change commit!
Nov 28, 2024
c66fdfc
change2 commit!
Nov 28, 2024
f8473f7
labaratorka commit
Nov 29, 2024
75609da
labar commit
Nov 29, 2024
20a57b1
labar1 commit
Nov 29, 2024
5704c60
laba1 commit
Nov 29, 2024
055a929
labir1 commit
Nov 29, 2024
4952611
parapapam
Nov 30, 2024
4da5b9b
ppppp
Nov 30, 2024
e143c12
pppppppppp
Nov 30, 2024
c546565
ppppppppppppp
Dec 1, 2024
1013204
qwerty
Dec 1, 2024
b88fc6d
qwertyu
Dec 1, 2024
3c32a67
qwertyuiop
Dec 1, 2024
39b02e0
qwe
Dec 1, 2024
e39ae44
qwe1
Dec 1, 2024
80f6cc7
qwe2
Dec 1, 2024
d05cd63
blahblahblah
Dec 1, 2024
ec2a66c
blahblahblahblah
Dec 1, 2024
14c0b2d
blahblahblahblah1
Dec 1, 2024
76792f6
blahblahblahblah2
Dec 1, 2024
d91da16
blahblahblahblah3
Dec 2, 2024
fb8e226
blahblahblahblah4
Dec 2, 2024
1208c83
labbbbb
Dec 2, 2024
0bda001
labbbbb1
Dec 2, 2024
90841dd
labbbbbbbbbb1
Dec 2, 2024
2c3ba9c
labbbbbbbbbb21
Dec 2, 2024
1c8e71a
labbbbbbbbbb2221
Dec 2, 2024
6d9362c
lab2221
Dec 2, 2024
d90c5a4
lab22221
Dec 2, 2024
7476476
lab222221
Dec 2, 2024
77b36d3
lab2222211
Dec 2, 2024
594c2e0
lab22222111
Dec 2, 2024
4096b24
labaP1
Dec 2, 2024
1bcd78b
labaaaaaaaaa1
Dec 3, 2024
09c5eb0
finallab
Dec 4, 2024
2559cfe
finallab1
Dec 4, 2024
fa156c5
FlLab1
Dec 6, 2024
e7009da
FlLab21
Dec 6, 2024
12389cd
Lab2
Dec 12, 2024
56f6d5f
Lab2!
Dec 14, 2024
42b0e12
Llab2!
Dec 14, 2024
8fe40e3
Lllab2!
Dec 14, 2024
d25b16c
Llllab2!
Dec 14, 2024
f788199
Llllab2!!
Dec 14, 2024
e8e4b7d
Llllab2!!!
Dec 14, 2024
a7d8c4a
Llllab2!!!!!!!
Dec 14, 2024
cda3ed7
Merge branch 'master' into lab2
nikolaievanin Dec 14, 2024
45ff1af
Llllab22221!
Dec 14, 2024
0cc8e03
Merge branch 'lab2' of https://github.com/nikolaievanin/Algorithms in…
Dec 14, 2024
16a93ed
LAab22222221!
Dec 15, 2024
f2b1208
Lab3 commit
Dec 21, 2024
01932ce
Lab3 commit!
Dec 21, 2024
6399e84
Labb3 commit!
Dec 21, 2024
3b2bac7
Labb3 commit!!!!
Dec 21, 2024
726c748
lab3 comit!!!!
Dec 21, 2024
76aaf5a
lab3 comit!!!
Dec 21, 2024
a4b6235
lab3 com!!!
Dec 22, 2024
a9542a2
Lab3!!!
Dec 22, 2024
2a30d92
Lab3!!!!
Dec 22, 2024
4d16860
Lab3!!!!!!!
Dec 22, 2024
42e035c
FLab3!
Dec 22, 2024
5a73c2b
FfLab3!
Dec 23, 2024
70841a5
FfLaab3!
Dec 23, 2024
97c55d5
FfLaab3!!
Dec 23, 2024
957fea8
FfLaab3!!!
Dec 23, 2024
98ff1b2
FfLaab3!!!!
Dec 23, 2024
47321d4
FfiiLab3!!!!
Dec 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

add_subdirectory(LibraryC)

add_subdirectory(LibraryCPP)
add_subdirectory(LibraryCPPClass)
add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)


add_subdirectory(Lab3CPP)
8 changes: 8 additions & 0 deletions Lab3CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_executable(Lab3CPP labb.cpp)
target_include_directories(Lab3CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab3CPP LibraryCPP)

add_test(NAME TestLab3CPP COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_BINARY_DIR}/output.txt)



5 changes: 5 additions & 0 deletions Lab3CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
######
#...X#
#.####
#...Y#
######
175 changes: 175 additions & 0 deletions Lab3CPP/labb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#include <iostream>

Check notice on line 1 in Lab3CPP/labb.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab3CPP/labb.cpp

File Lab3CPP/labb.cpp does not conform to Custom style guidelines. (lines 1, 12, 13, 14, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 47, 48, 49, 51, 52, 54, 55, 56, 57, 59, 60, 62, 63, 64, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 82, 83, 84, 89, 90, 91, 92, 93, 94, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 115, 120, 121, 122, 123, 124, 125, 127, 128, 129, 131, 136, 137, 138, 139, 140, 142, 143, 145, 146, 147, 149, 150, 151, 152, 154, 155, 156, 158, 159, 160, 161, 162, 163, 164, 165, 167, 168, 169, 170, 172)
#include <vector>
#include <queue>
#include <fstream>
#include <string>
using namespace std;

// Тип для лабиринта
using Maze = vector<string>;

// Функция для обработки соседей
bool process_neighbors(int x, int y, int nx, int ny, Maze& maze, vector<vector<bool>>& visited, vector<vector<int>>& dist, queue<pair<int, int>>& q, int end_x, int end_y, int dx[], int dy[]) {
int n = maze.size();
int m = maze[0].size();

// Проверяем, можно ли двигаться в новую точку
if (nx >= 0 && ny >= 0 && nx < n && ny < m && !visited[nx][ny] && maze[nx][ny] != '#') {
visited[nx][ny] = true;
dist[nx][ny] = dist[x][y] + 1;
q.push({ nx, ny });

// Если достигли цели, восстанавливаем путь
if (nx == end_x && ny == end_y) {
int cx = end_x, cy = end_y;
while (dist[cx][cy] != 0) {
maze[cx][cy] = 'x';
for (int j = 0; j < 4; ++j) {
int px = cx - dx[j];
int py = cy - dy[j];
if (px >= 0 && py >= 0 && px < n && py < m && dist[px][py] == dist[cx][cy] - 1) {
cx = px;
cy = py;
break;
}
}
}
maze[x][y] = 'X';
maze[end_x][end_y] = 'Y';
return true;
}
}

return false;
}

// Функция для поиска пути с помощью BFS
bool bfs(Maze& maze, int start_x, int start_y, int end_x, int end_y) {
int n = maze.size();
int m = maze[0].size();

vector<vector<bool>> visited(n, vector<bool>(m, false));
vector<vector<int>> dist(n, vector<int>(m, -1));

queue<pair<int, int>> q;
q.push({ start_x, start_y });
visited[start_x][start_y] = true;
dist[start_x][start_y] = 0;

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

while (!q.empty()) {
auto [x, y] = q.front();
q.pop();

// Обрабатываем все 4 соседних клетки
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];

// Вызываем функцию для обработки соседа
if (process_neighbors(x, y, nx, ny, maze, visited, dist, q, end_x, end_y, dx, dy)) {
return true; // Путь найден
}
}
}

return false; // Путь не найден
}

// Функция для вывода лабиринта в консоль
void print_maze(const Maze& maze) {
for (const auto& row : maze) {
cout << row << endl;
}
}

// Функция для чтения лабиринта из файла
bool read_maze_from_file(const string& filename, Maze& maze, int& start_x, int& start_y, int& end_x, int& end_y) {
ifstream input_file(filename);
if (!input_file.is_open()) {
cerr << "Ошибка при открытии файла: " << filename << endl;
return false;
}

string line;
maze.clear();
int row = 0;

while (getline(input_file, line)) {
maze.push_back(line);
for (std::string::size_type col = 0; col < line.size(); ++col) {
if (line[col] == 'X') {
start_x = row;
start_y = col;
}
if (line[col] == 'Y') {
end_x = row;
end_y = col;
}
}
row++;
}

input_file.close();
return true;
}

// Функция для записи лабиринта в файл
bool write_maze_to_file(const string& filename, const Maze& maze) {
ofstream output_file(filename);
if (!output_file.is_open()) {
cerr << "Ошибка при открытии выходного файла: " << filename << endl;
return false;
}

for (const auto& row : maze) {
output_file << row << endl;
}

output_file.close();
return true;
}

// Основная функция
int main(int argc, char* argv[]) {
if (argc != 3) {
cerr << "Использование: " << argv[0] << " <входной файл> <выходной файл>" << endl;
return 1;
}

string input_filename = argv[1]; // Имя входного файла
string output_filename = argv[2]; // Имя выходного файла

// Переменные для координат старта и финиша
int start_x = -1, start_y = -1, end_x = -1, end_y = -1;
Maze maze;

// Чтение лабиринта из файла
if (!read_maze_from_file(input_filename, maze, start_x, start_y, end_x, end_y)) {
return 1;
}

// Выводим исходный лабиринт на экран
cout << "Исходный лабиринт:" << endl;
print_maze(maze);

// Применяем BFS для нахождения пути
if (bfs(maze, start_x, start_y, end_x, end_y)) {
cout << "\nМинимальный путь найден:" << endl;
print_maze(maze);
}
else {
cout << "\nIMPOSSIBLE" << endl;
}

// Запись результата в выходной файл
if (!write_maze_to_file(output_filename, maze)) {
return 1;
}

cout << "Решенный лабиринт записан в файл: " << output_filename << endl;

return 0;
}
Empty file added Lab3CPP/output.txt
Empty file.
4 changes: 2 additions & 2 deletions LibraryCPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp)
add_library(LibraryCPP STATIC list.cpp queue.cpp)

add_subdirectory(Tests)
add_subdirectory(Tests)
25 changes: 15 additions & 10 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
# target_link_libraries(TestArrayCPP LibraryCPP)
# add_test(TestArrayCPP TestArrayCPP)

# add_executable(TestListCPP list.cpp)
# target_include_directories(TestListCPP PUBLIC ..)
# target_link_libraries(TestListCPP LibraryCPP)
# add_test(TestListCPP TestListCPP)
# add_executable(TestArrayCPP array.cpp)
# target_include_directories(TestArrayCPP PUBLIC ..)
# target_link_libraries(TestArrayCPP LibraryCPP)
# add_test(TestArrayCPP TestArrayCPP)

add_executable(TestListCPP list.cpp)
target_include_directories(TestListCPP PUBLIC ..)
target_link_libraries(TestListCPP LibraryCPP)
add_test(TestListCPP TestListCPP)

# add_executable(TestQueueCPP queue.cpp)
# target_include_directories(TestQueueCPP PUBLIC ..)
# target_link_libraries(TestQueueCPP LibraryCPP)
# add_test(TestQueueCPP TestQueueCPP)
# set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)
add_executable(TestQueueCPP queue.cpp)
target_include_directories(TestQueueCPP PUBLIC ..)
target_link_libraries(TestQueueCPP LibraryCPP)
add_test(TestQueueCPP TestQueueCPP)
set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)

# add_executable(TestStackCPP stack.cpp)
# target_include_directories(TestStackCPP PUBLIC ..)
Expand All @@ -23,4 +28,4 @@
# target_include_directories(TestVectorCPP PUBLIC ..)
# target_link_libraries(TestVectorCPP LibraryCPP)
# add_test(TestVectorCPP TestVectorCPP)
# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
46 changes: 28 additions & 18 deletions LibraryCPP/Tests/list.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/list.cpp

File LibraryCPP/Tests/list.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 20, 21, 23, 24, 26, 27, 28, 29, 30, 32, 33, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55)
#include "list.h"

int main()
{
List *list = list_create();
int main() {
List* list = list_create();

if (!list)
{
if (!list) {
std::cout << "List creation error\n";
return 1;
}

// Вставляем элементы 1, 2, 3 в конец списка.
list_insert(list, 1);
list_insert(list, 2);
list_insert(list, 3);

if (list_item_data(list_first(list)) != 3)
{
std::cout << "list_insert error\n";
// Проверяем первый элемент (он должен быть 1).
if (list_item_data(list_first(list)) != 1) {
std::cout << "list_insert error: incorrect first element\n";
return 1;
}

// Вставляем 4 после первого элемента (1).
list_insert_after(list, list_first(list), 4);

if (list_item_data(list_item_next(list_first(list))) != 4)
{
std::cout << "list_insert_after error\n";
// Проверяем, что следующий элемент после первого - 4.
if (list_item_data(list_item_next(list_first(list))) != 4) {
std::cout << "list_insert_after error: incorrect next element\n";
return 1;
}

// Удаляем первый элемент (1).
list_erase_first(list);

if (list_item_data(list_first(list)) != 4)
{
std::cout << "list_erase error\n";
// Проверяем, что первый элемент теперь 4 (первый следующий после старого 1).
if (list_item_data(list_first(list)) != 4) {
std::cout << "list_erase error: incorrect first element after erase\n";
return 1;
}

// Печатаем список.
std::cout << "List: ";
for (ListItem *item = list_first(list) ; item ; item = list_item_next(item))
{
std::cout << list_item_data(item) << " ";
ListItem* first = list_first(list);
if (first) {
ListItem* current = first;
do {
std::cout << list_item_data(current) << " ";
current = list_item_next(current);
} while (current != first);
}
std::cout << "\n";

// Удаляем список.
list_delete(list);
}
std::cout << "Test completed successfully.\n";

return 0;
}
6 changes: 4 additions & 2 deletions LibraryCPP/Tests/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/vector.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/vector.cpp

File LibraryCPP/Tests/vector.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 69, 70, 71, 73)
#include "vector.h"

int main()
Expand All @@ -17,7 +17,8 @@

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
{
if (vector_get(vector, i) != (int)i)
// Приведение i к Data (беззнаковому типу)
if (vector_get(vector, i) != (Data)i)
{
std::cout << "Invalid vector element " << i << "\n";
return 1;
Expand Down Expand Up @@ -45,7 +46,8 @@

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
{
if (vector_get(vector, i) != (int)i)
// Приведение i к Data (беззнаковому типу)
if (vector_get(vector, i) != (Data)i)
{
std::cout << "Invalid vector element " << i << "\n";
return 1;
Expand Down
2 changes: 1 addition & 1 deletion LibraryCPP/array.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "array.h"

Check notice on line 1 in LibraryCPP/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/array.cpp

File LibraryCPP/array.cpp does not conform to Custom style guidelines. (lines 3, 4, 8, 9, 10, 14, 15, 16, 20, 21, 22, 26, 27, 31, 32, 33)

struct Array
{
Expand Down Expand Up @@ -31,4 +31,4 @@
size_t array_size(const Array *arr)
{
return 0;
}
}
11 changes: 6 additions & 5 deletions LibraryCPP/array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ARRAY_H

Check notice on line 1 in LibraryCPP/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/array.h

File LibraryCPP/array.h does not conform to Custom style guidelines. (lines 13, 16, 19, 22, 25, 27)
#define ARRAY_H

#include <cstddef>

Check failure on line 4 in LibraryCPP/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/array.h:4:10 [clang-diagnostic-error]

'cstddef' file not found

// Non-resizeable array
// Stores integer values inside
Expand All @@ -10,18 +10,19 @@
struct Array;

// create array
Array *array_create(size_t size);
Array* array_create(size_t size);

// delete array, free memory
void array_delete(Array *arr);
void array_delete(Array* arr);

// returns specified array element
Data array_get(const Array *arr, size_t index);
Data array_get(const Array* arr, size_t index);

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value);
void array_set(Array* arr, size_t index, Data value);

// returns array size
size_t array_size(const Array *arr);
size_t array_size(const Array* arr);

#endif

Loading
Loading