You are on page 1of 16

TRƯỜNG ĐẠI HỌC GIAO THÔNG VẬN TẢI TP.

HCM
Họ và tên: Nguyễn Văn Lâm
MSSV: 2251120095

BÁO CÁO BÀI TẬP LỚN MÔN KĨ THUẬT LẬP TRÌNH


GV: Bùi Thị Thà
*NỘI DUNG BÁO CÁO:
1. Giới thiệu về đề tài.
2. Yêu cầu của đề tài.
3. Source code.
4. Giải thích code.
1. Giới thiệu về đề tài:
Cho mảng 2 chiều chứa các số nguyên dương (không trùng nhau) mô phỏng mê cung, 1
robot được đặt ở vị trí (x,y). Robot chỉ có thể đi theo 4 hướng (trên, dưới, trái, phải). Robot
sẽ lựa chọn hướng (ô) có giá trị lớn để đi, các ô đi rồi sẽ không đi lại. Điểm được tính bằng
tổng giá trị các ô robot đi qua.
2. Yêu cầu của đề tài:
 Áp dụng các kiến thức trong môn kỹ thuật lập trình đệ quy/struct/ cấp phát động/ đọc
ghi file.
 Cài đặt thuật toán xuất được kết quả tổng số vị trí robot đã đi và tổng điểm của các ô
robot đi qua.
 Trường hợp đặt 2 robot ở 2 vị trí khác nhau. Xuất kết quả 2 robot, so sánh kết quả,
xuất tổng vị trí, tổng điểm của từng robot và những vị trí 2 robot đi trùng ô nhau.
 Cho phép 2 người chơi đặt 2 robot ở vị trí bất kỳ trên ma trận, mỗi lượt chỉ được 1
robot di chuyển, các ô robot trước đi rồi, robot khác không được đi lại. Xuất kết quả
của 2 robot và cho biết robot nào thắng cuộc.
 Bài toán mở, sinh viên có mở rộng, thêm 1 số tính năng sáng tạo.Ví dụ: visualize
đường đi robot, mô phỏng step by step,….
3. Source code:
//Nguyễn Văn Lâm - 2251120095
#include <iostream>
#include <vector>
#include<string>
#include<fstream>
#include <windows.h> // Thêm thư viện windows.h để sử dụng hàm Sleep()
using namespace std;
struct Position {
int x;
int y;
int robot;
};
void writeResultToFile1(const int a[][5], const vector<Position>& robotVisitedPositions,
int robotTotalScore, int count) {
ofstream outputFile("output.txt"); // Mở tệp tin đầu ra

if (outputFile.is_open()) { // Kiểm tra xem việc mở tệp tin có thành công hay không
outputFile << "Cac gia tri robot di qua:" << endl;
for (const Position& pos : robotVisitedPositions) {
outputFile << a[pos.x][pos.y] << " (" << pos.x << ";" << pos.y << ") | "; // In giá trị
tại vị trí hiện tại
}
outputFile << endl;
outputFile << "Tong so vi tri robot da di: " << count << endl;
outputFile << "Tong diem cua cac o robot di qua: " << robotTotalScore << endl;

outputFile.close(); // Đóng tệp tin sau khi ghi xong


}
else {
cout << "Khong the mo file de ghi ket qua." << endl;
}
}
void printMatrixWithVisited1(const int a[][5], const vector<Position>& visitedPositions, int
n, int m, int delay) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
bool visited = false;
for (const Position& pos : visitedPositions) {
if (pos.x == i && pos.y == j) {
visited = true;
break;
}
}
if (visited) {
// Nếu ô đã được đi qua, in ra màu đỏ
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << "|" << a[i][j] << "|" << " ";
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else {
cout << "|" << a[i][j] << "|" << " ";
}
}
cout << endl;
}
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
cout << "*Robot: ";
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); // Đặt màu đỏ cho chữ
cout << "RED" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
Sleep(delay); // Đặt độ trễ giữa các bước in ra ma trận
}
void writeResultToFile2(const int a[][5], const vector<Position>& robot1VisitedPositions,
const vector<Position>& robot2VisitedPositions, int robot1TotalScore, int
robot2TotalScore, int count1, int count2, int counttrung) {
ofstream outputFile("output.txt"); // Mở tệp tin đầu ra

if (outputFile.is_open()) { // Kiểm tra xem việc mở tệp tin có thành công hay không
outputFile << "Cac gia tri robot 1 di qua:" << endl;
for (const Position& pos : robot1VisitedPositions) {
outputFile << a[pos.x][pos.y] << " (" << pos.x << ";" << pos.y << ") | ";
}
outputFile << endl;
outputFile << "Cac gia tri robot 2 di qua:" << endl;
for (const Position& pos : robot2VisitedPositions) {
outputFile << a[pos.x][pos.y] << " (" << pos.x << ";" << pos.y << ") | ";
}
outputFile << endl;
outputFile << "Tong so vi tri robot 1 da di: " << count1 << endl;
outputFile << "Tong so vi tri robot 2 da di: " << count2 << endl;
outputFile << "Tong diem cua cac o robot 1 di qua: " << robot1TotalScore << endl;
outputFile << "Tong diem cua cac o robot 2 di qua: " << robot2TotalScore << endl;
outputFile << "Cac vi tri robot 1 va robot 2 di trung nhau: " << counttrung<<" vi tri"<<
endl;
for (const auto& pos1 : robot1VisitedPositions) {
for (const auto& pos2 : robot2VisitedPositions) {
if (pos1.x == pos2.x && pos1.y == pos2.y) {
outputFile << "(" << pos1.x << ";" << pos1.y << ") - Gia tri: " << a[pos1.x]
[pos1.y] << endl;
}
}
}
outputFile.close(); // Đóng tệp tin sau khi ghi xong
}
else {
cout << "Khong the mo file de ghi ket qua." << endl;
}
}

void printMatrixWithVisited2 (const int a[][5], const vector<Position>&


robot1VisitedPositions, const vector<Position>& robot2VisitedPositions, int n, int m,int
delay) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
bool visitedByRobot1 = false;
bool visitedByRobot2 = false;
for (const Position& pos : robot1VisitedPositions) {
if (pos.x == i && pos.y == j) {
visitedByRobot1 = true;
break;
}
}
for (const Position& pos : robot2VisitedPositions) {
if (pos.x == i && pos.y == j) {
visitedByRobot2 = true;
break;
}
}
if (visitedByRobot1 && visitedByRobot2) {
SetConsoleTextAttribute(hConsole, FOREGROUND_RED |
FOREGROUND_BLUE); // Đặt màu tím cho chữ
}
else if (visitedByRobot1) {
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); // Đặt màu đỏ cho
chữ
}
else if (visitedByRobot2) {
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE); // Đặt màu xanh
dương cho chữ
}
else {
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
}
cout << "|" << a[i][j] << "|" << " ";
}
cout << endl;
}
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
cout << "*Robot 1: ";
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); // Đặt màu đỏ cho chữ
cout << "RED" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
cout << "*Robot 2: ";
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE); // Đặt màu xanh dương
cho chữ
cout << "BLUE" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
cout << "*Ca 2 Robot: ";
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE); //
Đặt màu tím cho chữ
cout << "PURPLE" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
Sleep(delay); // Đặt độ trễ giữa các bước in ra ma trận
}

void writeResultToFile3(const vector<Position>& robot1VisitedPositions, int


robot1TotalScore, int robot2TotalScore, const string& robot1Values, const string&
robot2Values, int count3, int count4) {
ofstream outputFile("output.txt"); // Mở tệp tin đầu ra

if (outputFile.is_open()) { // Kiểm tra xem việc mở tệp tin có thành công hay không
outputFile << "Cac gia tri robot 1 di qua: " << robot1Values << endl;
outputFile << "Cac gia tri robot 2 di qua: " << robot2Values << endl;
outputFile << "Tong so vi tri robot 1 da di: " << count3 << endl;
outputFile << "Tong so vi tri robot 2 da di: " << count4 << endl;
outputFile << "Tong diem robot 1: " << robot1TotalScore << endl;
outputFile << "Tong diem robot 2: " << robot2TotalScore << endl;

if (robot1TotalScore > robot2TotalScore) {


outputFile << "Robot 1 chien thang!" << endl;
}
else if (robot1TotalScore < robot2TotalScore) {
outputFile << "Robot 2 chien thang!" << endl;
}
else {
outputFile << "2 robot hoa nhau!" << endl;
}

outputFile.close(); // Đóng tệp tin sau khi ghi xong


}
else {
cout << "Khong the mo file de ghi ket qua." << endl;
}
}
void printMatrixWithVisited3(const int a[][5], const vector<Position>&
robot3VisitedPositions, const vector<Position>& robot4VisitedPositions, int n, int m, int
delay) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
bool visitedByRobot3 = false;
bool visitedByRobot4 = false;
for (const Position& pos : robot3VisitedPositions) {
if (pos.x == i && pos.y == j) {
visitedByRobot3 = true;
}
}
for (const Position& pos : robot4VisitedPositions) {
if (pos.x == i && pos.y == j) {
visitedByRobot4 = true;
}
}
if (visitedByRobot3) {
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); // Đặt màu đỏ cho
chữ
}
else if (visitedByRobot4) {
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE); // Đặt màu xanh
dương cho chữ
}
else {
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
}
cout <<"|" << a[i][j] << "|"<<" ";
}
cout << endl;
}
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
cout << "*Robot 1: ";
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); // Đặt màu đỏ cho chữ
cout << "RED" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
cout << "*Robot 2: ";
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE); // Đặt màu xanh dương
cho chữ
cout << "BLUE" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // Đặt màu
trắng cho chữ
Sleep(delay); // Đặt độ trễ giữa các bước in ra ma trận
}

bool isPositionTaken(const vector<Position>& visitedPositions, int x, int y, int


currentRobot) {
for (const auto& pos : visitedPositions) {
if (pos.x == x && pos.y == y && pos.robot != currentRobot) {
return true;
}
}
return false;
}

Position getMaxValueDirection(const int a[][5], const vector<Position>& visitedPositions,


int x, int y, int n, int m, int currentRobot) {
int maxVal = -1 ;
Position maxPos = { x, y };

if (x > 0 && !isPositionTaken(visitedPositions, x - 1, y, currentRobot) && a[x - 1][y] >


maxVal) {
maxVal = a[x - 1][y];
maxPos = { x - 1, y };
}
if (x < n - 1 && !isPositionTaken(visitedPositions, x + 1, y, currentRobot) && a[x + 1]
[y] > maxVal) {
maxVal = a[x + 1][y];
maxPos = { x + 1, y };
}
if (y > 0 && !isPositionTaken(visitedPositions, x, y - 1, currentRobot) && a[x][y - 1] >
maxVal) {
maxVal = a[x][y - 1];
maxPos = { x, y - 1 };
}
if (y < m - 1 && !isPositionTaken(visitedPositions, x, y + 1, currentRobot) && a[x][y +
1] > maxVal) {
maxVal = a[x][y + 1];
maxPos = { x, y + 1 };
}
if (x > 0 && isPositionTaken(visitedPositions, x - 1, y, currentRobot) && x < n - 1 &&
isPositionTaken(visitedPositions, x + 1, y, currentRobot) && y > 0 &&
isPositionTaken(visitedPositions, x, y - 1, currentRobot) && y < m - 1 &&
isPositionTaken(visitedPositions, x, y + 1, currentRobot)) {
maxVal = a[x][y];
maxPos = { x,y };
}
return maxPos;
}

void readMatrixFromFile(const string& filename, int**& matrix, int& n, int& m) {


ifstream inputFile(filename);
if (!inputFile) {
cerr << "Khong the mo file." << filename << endl;
return;
}

inputFile >> n >> m;

matrix = new int* [n];


for (int i = 0; i < n; i++) {
matrix[i] = new int[m];
for (int j = 0; j < m; j++) {
inputFile >> matrix[i][j];
}
}

inputFile.close();
}

void writeMatrixToFile(const string& filename, int** matrix, int n, int m) {


ofstream outputFile(filename);
if (!outputFile) {
cerr << "Khong the mo file." << filename << endl;
return;
}

outputFile << n << " " << m << endl;


outputFile << "Me cung cua robot: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
outputFile << matrix[i][j] << " ";
}
outputFile << endl;
}

outputFile.close();
}

int main() {
int a[6][5] = {
{ 2, 1, 14, 12, 17 },
{ 3, 16, 22, 91, 23 },
{ 4, 56, 87, 31, 65 },
{ 27, 43, 90, 100, 101 },
{ 76, 54, 32, 99, 66 },
{ 178, 154, 150, 200, 543 } };
string inputFilename = "input.txt";
string outputFilename = "output.txt";
int** p;
int n, m;

// Đọc ma trận từ tệp tin input.txt


readMatrixFromFile(inputFilename, p, n, m);

// In lại ma trận vào tệp tin output.txt


writeMatrixToFile(outputFilename, p, n, m);

cout << "Me cung da duoc dien vao trong " << outputFilename << endl;

int c;
cout << "Lua chon che do: " << endl;
cout << "1.Robot giai me cung." << endl;
cout << "2.So sanh quang duong 2 Robot. " << endl;
cout << "3.Tro choi robot." << endl;
cout << "Lua chon cua ban: ";
cin >> c;
switch (c) {
case(1): {
Position robotPos;
cout << "Nhap vi tri ban dau cua robot: " << endl;
cout << "Nhap toa do x: ";
cin >> robotPos.x;
cout << "Nhap toa do y: ";
cin >> robotPos.y;
vector<Position> robotVisitedPositions;
int robotTotalScore = 0;
int count = 0;
while (true) {
robotVisitedPositions.push_back(robotPos);
robotTotalScore += a[robotPos.x][robotPos.y];
count += 1;
system("CLS"); // Xóa màn hình console (chỉ hoạt động trên Windows)
printMatrixWithVisited1(a, robotVisitedPositions, n, m, 1000); // In ma trận với ô đã
đi được tô màu đỏ và độ trễ 1000ms
Position nextPos = getMaxValueDirection(a, robotVisitedPositions, robotPos.x,
robotPos.y, n, m, 1);
if (nextPos.x == robotPos.x && nextPos.y == robotPos.y) {
break; // Khong co o tiep theo de di
}
robotPos = nextPos;
}
writeResultToFile1(a, robotVisitedPositions, robotTotalScore, count); // Ghi kết quả và
giá trị tại vị trí vào tệp tin
// ...
cout << "Ket qua da duoc ghi vao " << outputFilename << endl;
break;
}
case(2): {
Position robot1Pos, robot2Pos;

cout << "Nhap vi tri ban dau cua robot 1:" << endl;
cout << "Nhap toa do x: ";
cin >> robot1Pos.x;
cout << "Nhap toa do y: ";
cin >> robot1Pos.y;

cout << "Nhap vi tri ban dau cua robot 2:" << endl;
cout << "Nhap toa do x: ";
cin >> robot2Pos.x;
cout << "Nhap toa do y: ";
cin >> robot2Pos.y;

vector<Position> robot1VisitedPositions;
vector<Position> robot2VisitedPositions;
int robot1TotalScore = 0;
int robot2TotalScore = 0;
int count1 = 0;
int count2 = 0;
int counttrung = 0;
while (true) {
robot1VisitedPositions.push_back(robot1Pos);
robot1TotalScore += a[robot1Pos.x][robot1Pos.y];
count1++;
system("CLS"); // Xóa màn hình console (chỉ hoạt động trên Windows)
printMatrixWithVisited2(a, robot1VisitedPositions, robot2VisitedPositions, n, m,
1000);
Position nextPos = getMaxValueDirection(a, robot1VisitedPositions, robot1Pos.x,
robot1Pos.y, n, m, 1);
if (nextPos.x == robot1Pos.x && nextPos.y == robot1Pos.y) {
break; // Khong co o tiep theo de di
}
robot1Pos = nextPos;
}

while (true) {
robot2VisitedPositions.push_back(robot2Pos);
robot2TotalScore += a[robot2Pos.x][robot2Pos.y];
count2++;
system("CLS"); // Xóa màn hình console (chỉ hoạt động trên Windows)
printMatrixWithVisited2(a, robot1VisitedPositions, robot2VisitedPositions, n, m,
1000);
Position nextPos = getMaxValueDirection(a, robot2VisitedPositions, robot2Pos.x,
robot2Pos.y, n, m, 2);
if (nextPos.x == robot2Pos.x && nextPos.y == robot2Pos.y) {
break; // Khong co o tiep theo de di
}
robot2Pos = nextPos;
}
for (const auto& pos1 : robot1VisitedPositions) {
for (const auto& pos2 : robot2VisitedPositions) {
if (pos1.x == pos2.x && pos1.y == pos2.y) {
counttrung++;
}
}
}
writeResultToFile2(a, robot1VisitedPositions, robot2VisitedPositions,
robot1TotalScore, robot2TotalScore, count1, count2, counttrung); // Ghi kết quả vào tệp tin
cout << "Ket qua da duoc ghi vao " << outputFilename << endl;
break;
}
case(3): {
Position robot3Pos;
Position robot4Pos;

cout << "Nhap vi tri ban dau cua robot 1:" << endl;
cout << "Nhap toa do x: ";
cin >> robot3Pos.x;
cout << "Nhap toa do y: ";
cin >> robot3Pos.y;

cout << "Nhap vi tri ban dau cua robot 2:" << endl;
cout << "Nhap toa do x: ";
cin >> robot4Pos.x;
cout << "Nhap toa do y: ";
cin >> robot4Pos.y;

vector<Position> robot3VisitedPositions;
vector<Position> robot4VisitedPositions;
vector<Position> robotsVisitedPositions;
int count = 0; // Biến đếm
int times = 0;
int count3 = 0;
int count4 = 0;
bool inactiveRobot3 = false;
bool inactiveRobot4 = false;
int robot3TotalScore = 0; // Tổng điểm của robot 1
int robot4TotalScore = 0; // Tổng điểm của robot 2
string robot3Values; // Chuỗi để ghi nhận các giá trị của robot 1
string robot4Values; // Chuỗi để ghi nhận các giá trị của robot 2
// Vòng lặp cho đến khi không còn giá trị nào để robot đi
while (times<2) {
if (count % 2 == 0) {
// Lượt di chuyển của robot 2 (số chẵn)
if (!inactiveRobot4) {
robot4VisitedPositions.push_back(robot4Pos);
robotsVisitedPositions.push_back(robot4Pos);
int value = a[robot4Pos.x][robot4Pos.y];
robot4TotalScore += value;
robot4Values += to_string(value) + " ";
count4++;
system("CLS"); // Xóa màn hình console (chỉ hoạt động trên Windows)
printMatrixWithVisited3(a, robot3VisitedPositions, robot4VisitedPositions, n,
m, 1000);
Position nextPos = getMaxValueDirection(a, robotsVisitedPositions,
robot4Pos.x, robot4Pos.y, n, m, 4);
if (nextPos.x == robot4Pos.x && nextPos.y == robot4Pos.y) {
inactiveRobot4 = true;
times++;
}
for (const auto& pos : robotsVisitedPositions) {
if (pos.x == nextPos.x && pos.y == nextPos.y && pos.robot == 3) {
nextPos = getMaxValueDirection(a, robotsVisitedPositions, robot4Pos.x,
robot4Pos.y, n, m, 4);
}
}
robot4Pos = nextPos;
}
}
else {
// Lượt di chuyển của robot 1 (số lẻ)
if (!inactiveRobot3) {
robot3VisitedPositions.push_back(robot3Pos);
robotsVisitedPositions.push_back(robot3Pos);
int value = a[robot3Pos.x][robot3Pos.y];
robot3TotalScore += value;
robot3Values += to_string(value) + " ";
count3++;
system("CLS"); // Xóa màn hình console (chỉ hoạt động trên Windows)
printMatrixWithVisited3(a, robot3VisitedPositions, robot4VisitedPositions, n,
m, 1000);
Position nextPos = getMaxValueDirection(a, robotsVisitedPositions,
robot3Pos.x, robot3Pos.y, n, m, 3);
if (nextPos.x == robot3Pos.x && nextPos.y == robot3Pos.y) {
inactiveRobot3 = true;
times++;
}
for (const auto& pos : robotsVisitedPositions) {
if (pos.x == nextPos.x && pos.y == nextPos.y && pos.robot == 4) {
nextPos = getMaxValueDirection(a, robotsVisitedPositions, robot3Pos.x,
robot3Pos.y, n, m, 3);
}
}
robot3Pos = nextPos;
}
}
count++;
}
// Xuất kết quả
writeResultToFile3(robotsVisitedPositions, robot3TotalScore, robot4TotalScore,
robot3Values, robot4Values, count3, count4);
cout << "Ket qua da duoc ghi vao " << outputFilename << endl;
break;
}
default: {
cout << "Cau tra loi khong hop le!" << endl;
}
}
return 0;
}
4. Giải thích code:
Đây là một đoạn code viết bằng ngôn ngữ C++ để thực hiện một số hoạt động liên quan đến
robot di chuyển trong mê cung.

1. Đầu tiên, code định nghĩa một struct tên là "Position" để lưu trữ tọa độ của robot trong
mê cung.

2. Hàm "writeResultToFile1" được sử dụng để ghi kết quả về các giá trị mà robot đã đi qua
vào một tệp tin "output.txt".
3. Hàm "printMatrixWithVisited1" được sử dụng để in ma trận mê cung kèm theo các ô đã
được robot đi qua (được tô màu đỏ).

4. Hàm "writeResultToFile2" và "printMatrixWithVisited2" thực hiện công việc tương tự


như hàm "writeResultToFile1" và "printMatrixWithVisited1", nhưng cho hai robot di
chuyển.

5. Hàm "writeResultToFile3" và "printMatrixWithVisited3" thực hiện công việc tương tự


như hàm "writeResultToFile2" và "printMatrixWithVisited2", nhưng trong một trò chơi
giữa hai robot.

6. Hàm "isPositionTaken" kiểm tra xem một vị trí đã được robot khác đi qua hay chưa.

7. Hàm "getMaxValueDirection" tìm vị trí có giá trị lớn nhất xung quanh vị trí hiện tại của
robot.
Hàm "getMaxValueDirection" được sử dụng để tìm vị trí có giá trị lớn nhất trong các ô
xung quanh vị trí hiện tại của robot. Hàm này nhận vào các tham số sau:

- `a`: Một mảng 2D chứa giá trị của mê cung.


- `visitedPositions`: Một vector chứa các vị trí mà robot đã đi qua.
- `x`, `y`: Tọa độ hiện tại của robot.
- `n`, `m`: Kích thước của mê cung.
- `currentRobot`: Giá trị đại diện cho robot hiện tại.

Hàm trả về một đối tượng kiểu `Position`, đại diện cho vị trí tiếp theo mà robot sẽ di chuyển
đến.

Cách thức hoạt động của hàm:

1. Khởi tạo `maxVal` với giá trị âm vô cùng và `maxPos` là vị trí hiện tại của robot.

2. Kiểm tra các ô xung quanh vị trí hiện tại của robot:
a. Nếu ô phía trên (x-1, y) có giá trị lớn hơn `maxVal` và chưa được robot khác
đi qua, cập nhật `maxVal` và `maxPos` với giá trị và vị trí này.
b. Tương tự cho các ô phía dưới (x+1, y), trái (x, y-1) và phải (x, y+1).

3. Kiểm tra trường hợp đặc biệt khi cả 4 ô xung quanh đều đã được robot khác đi qua.
Trong trường hợp này, `maxVal` được cập nhật bằng giá trị hiện tại của ô và
`maxPos` không thay đổi.

4. Trả về `maxPos` là vị trí có giá trị lớn nhất để robot tiếp tục di chuyển đến.

Hàm này giúp robot chọn hướng di chuyển tối ưu dựa trên giá trị của các ô trong mê cung
và tránh đi qua các ô đã được robot khác đi qua trước đó.

8. Hàm "readMatrixFromFile" đọc ma trận mê cung từ một tệp tin đầu vào.

9. Hàm "writeMatrixToFile" ghi ma trận mê cung vào một tệp tin đầu ra.

10. Trong hàm "main", đoạn code đầu tiên khai báo một ma trận mê cung "a" và chuỗi tên
tệp tin đầu vào và đầu ra.

11. Sau đó, code gọi hàm "readMatrixFromFile" để đọc ma trận từ tệp tin đầu vào và ghi lại
ma trận vào tệp tin đầu ra bằng hàm "writeMatrixToFile".

12. Tiếp theo, code yêu cầu người dùng chọn một chế độ hoạt động (1, 2 hoặc 3) và thực
hiện các công việc tương ứng.

- Trong chế độ 1, code yêu cầu người dùng nhập vị trí ban đầu của robot và sau đó thực
hiện việc đi qua các ô trong mê cung, lưu trữ vị trí và tính tổng điểm. Cuối cùng, kết quả
được ghi vào tệp tin đầu ra bằng hàm "writeResultToFile1".
- Trong chế độ 2, code yêu cầu người dùng nhập vị trí ban đầu của hai robot và sau đó
thực hiện việc đi qua các ô trong mê cung cho cả hai robot, lưu trữ vị trí và tính tổng điểm
cho mỗi robot. Cuối cùng, kết quả được ghi vào tệp tin đầu ra bằng hàm
"writeResultToFile2".

- Trong chế độ 3, code yêu cầu người dùng nhập vị trí ban đầu của hai robot và sau đó
thực hiện việc đi qua các ô trong mê cung cho cả hai robot, 2 robot di chuyển lần lượt, robot
1 di chuyển lượt lẻ và robot 2 di chuyển lượt chẵn, sau đó lưu trữ vị trí và tính tổng điểm
cho mỗi robot. Cuối cùng, kết quả được ghi vào tệp tin đầu ra bằng hàm
"writeResultToFile3". Trò chơi này tiếp tục cho đến khi cả hai robot không còn ô nào để đi
qua.

Cuối cùng, code in ra thông báo cho biết kết quả đã được ghi vào tệp tin đầu ra.

You might also like