类似于推箱子的小游戏 寻找 最短路径

实现效果如下

类似 推箱子小游戏 的变种 C/C++版本 BFS最短路径

黑色代表墙壁 不能越过

蓝色代表HOME点 灰色代表要找的小箱子

绿色代表路径  

最终目标是将灰色的小箱子移动到蓝色的HOME点 

需要两次搜索 第一次是 出发点到灰色小箱子 

第二次是灰色小箱子到蓝色HOME点

BFS 搜索路径之后 找到一条最短路径 

动画效果用的是JAVA的 一个jar包 

完整的代码 包含动画效果已经上传点击这里下载

C语言编译

gcc box.c graphics.c -o box

C++编译

g++ box.cpp graphics.c -o box

需要安装jdk 17版本  

如下图 将 安装包里的所有文件放到 如下的jdk bin目录  

执行如下代码  最后的jar包 用绝对路径就可以 

./box | ./java -jar /home/QMCY/robot/drawppp.jar

代码很乱  临时记录下 

C语言版本


#include <string.h>
#include "graphics.h"#define GRID_SIZE_X 10
#define GRID_SIZE_Y 10#define ROBOT 'R'#define HOME_X	9
#define HOME_Y	2#define MARKER_X	7
#define MARKER_Y	7#define	ROBOT_X	6
#define	ROBOT_Y	5
#define MAX_N	105int g_father[MAX_N][MAX_N];	
int dist[MAX_N][MAX_N];
int last_dir[MAX_N][MAX_N];int dir[MAX_N*MAX_N];	char g_has_visited[MAX_N][MAX_N];		//vis[x][y]表示xy点是否遍历过int Queue[MAX_N*MAX_N];			//用Q来模拟队列 给定两个下标 front和rear 那么入队则是Q[rear++]=u  出队是u=Q[front++]int coordinate[MAX_N*MAX_N];const int squareSize = 50;const int windowSize = 600;// Define the grid elements
const char EMPTY = ' ';
const char BLOCK = '#';
const char MARKER = '*';
const char HOME = 'H';// Define robot directions
typedef enum   { WEST , EAST, NORTH, SOUTH }Direction;// Define the robot struct
typedef struct  {int x;int y;Direction direction;char carryingMarker;
}Robot;void drawStep(int homeX, int homeY)
{background();setColour(pink);homeX = homeX*squareSize;homeY = homeY*squareSize;fillRect(homeX, homeY, squareSize, squareSize);
}void drawMarker(int x,int y)
{background();setColour(gray);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); 
}void drawBlocks(int x,int y)
{background();setColour(black);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); 
}void drawEmpty(int x,int y)
{foreground();setColour(white);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); 
}// Function to initialize the grid
void initializeGrid(char grid[][GRID_SIZE_Y]) {// Initialize the grid with empty spacesfor (int i = 0; i < GRID_SIZE_X; ++i) {for (int j = 0; j < GRID_SIZE_X; ++j) {grid[i][j] = EMPTY;}}// Place blocks, markers, and home squaregrid[8][8] = BLOCK;grid[9][5] = BLOCK;grid[8][5] = BLOCK;grid[7][5] = BLOCK;grid[6][7] = BLOCK;grid[8][7] = BLOCK; grid[7][8] = BLOCK; grid[7][8] = BLOCK; grid[2][2] = BLOCK; grid[3][3] = BLOCK; grid[4][4] = BLOCK; grid[5][5] = BLOCK; grid[6][6] = BLOCK; grid[MARKER_X][MARKER_Y] = MARKER;grid[HOME_X][HOME_Y] = HOME;
}// Function to display the grid
void displayGrid(const char grid[][GRID_SIZE_X]) {setWindowSize(windowSize, windowSize);background();   // Must draw on the background layer.int x;int y;for (x=0; x<GRID_SIZE_X; x++) {for (y=0; y<GRID_SIZE_X; y++){drawRect(x*squareSize, y*squareSize, squareSize, squareSize);}}}void draw_north(int x, int y)
{int x_coords[] = {x, x+50, x+25};int y_coords[] = {y+50, y+50, y};fillPolygon(3, x_coords, y_coords);
}void draw_east(int x, int y)
{int x_coords[] = {x, x, x+50};int y_coords[] = {y, y+50, y+25};fillPolygon(3, x_coords, y_coords);
}void draw_south(int x, int y)
{int x_coords[] = {x, x+50, x+25};int y_coords[] = {y, y, y+50};fillPolygon(3, x_coords, y_coords);
}void draw_west(int x, int y)
{int x_coords[] = {x+50, x+50, x};int y_coords[] = {y, y+50, y+25};fillPolygon(3, x_coords, y_coords);
}// Function to drop a marker
void dropMarker(Robot *robot, char grid[][GRID_SIZE_X]) {if (!robot->carryingMarker) {return; // Robot is not carrying a marker}grid[robot->x][robot->y] = MARKER;robot->carryingMarker = 0;//drawRobot(robot.x, robot.y, (int)robot.direction);}void drawRobot(int x, int y, int direction)
{foreground();clear();setColour(green);x = x*squareSize;y = y*squareSize;switch (direction){case NORTH: draw_north(x, y); break;case EAST: draw_east(x, y); break;case SOUTH: draw_south(x, y); break;case WEST: draw_west(x, y); break;}}void drawRobotWithBg(int x, int y, int direction)
{foreground();clear();setColour(gray);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); setColour(green);switch (direction){case NORTH: draw_north(x, y); break;case EAST: draw_east(x, y); break;case SOUTH: draw_south(x, y); break;case WEST: draw_west(x, y); break;}}void drawHome(int homeX, int homeY)
{background();setColour(blue);homeX = homeX*squareSize;homeY = homeY*squareSize;fillRect(homeX, homeY, squareSize, squareSize);
}void drawShort(int homeX, int homeY)
{background();setColour(orange);homeX = homeX*squareSize;homeY = homeY*squareSize;fillRect(homeX, homeY, squareSize, squareSize);
}void forward(Robot *robot, char grid[][GRID_SIZE_X]) {// Calculate the next position based on the directionint nextX = robot->x;int nextY = robot->y;if (robot->direction == NORTH) {--nextX;} else if (robot->direction == SOUTH) {++nextX;} else if (robot->direction == EAST) {++nextY;} else if (robot->direction == WEST) {--nextY;}// Check if the next position is validif (nextX >= 0 && nextX < GRID_SIZE_X && nextY >= 0 && nextY < GRID_SIZE_X && grid[nextX][nextY] != BLOCK) {// Move the robotgrid[robot->x][robot->y] = EMPTY;robot->x = nextX;robot->y = nextY;grid[robot->x][robot->y] = ROBOT;}drawRobot(robot->x, robot->y, robot->direction);}char markersLeft(char grid[][GRID_SIZE_X]) {for (int i = 0; i < GRID_SIZE_X; ++i) {for (int j = 0; j < GRID_SIZE_X; ++j) {if (grid[i][j] == MARKER) {return 1;}}}return 0;
}void turn_left(Robot *robot) {if (robot->direction == NORTH) {robot->direction = WEST;} else if (robot->direction == SOUTH) {robot->direction = EAST;} else if (robot->direction == EAST) {robot->direction = NORTH;} else if (robot->direction == WEST) {robot->direction = SOUTH;}drawRobot(robot->x, robot->y, robot->direction);}void turn_right(Robot *robot) {if (robot->direction == NORTH) {robot->direction = EAST;} else if (robot->direction == SOUTH) {robot->direction = WEST;} else if (robot->direction == EAST) {robot->direction = SOUTH;} else if (robot->direction == WEST) {robot->direction = NORTH;}drawRobot(robot->x, robot->y, robot->direction);}// Function to pick up a marker
void pickUpMarker(Robot *robot, char grid[][GRID_SIZE_Y]) {if (grid[robot->x][robot->y] == MARKER) {robot->carryingMarker = 1;grid[robot->x][robot->y] = EMPTY;}
}void findAndCollectMarkers(Robot *robot, char grid[][GRID_SIZE_X]) {while (markersLeft(grid)) {int initialX = robot->x;int initialY = robot->y;Direction initialDirection = robot->direction;// Use the "right hand rule" to navigateif (robot->direction == NORTH) {if (grid[robot->x][robot->y + 1] != BLOCK) {turn_right(robot);} else if (grid[robot->x - 1][robot->y] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}} else if (robot->direction == SOUTH) {if (grid[robot->x][robot->y - 1] != BLOCK) {turn_right(robot);} else if (grid[robot->x + 1][robot->y] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}} else if (robot->direction == EAST) {if (grid[robot->x + 1][robot->y] != BLOCK) {turn_right(robot);} else if (grid[robot->x][robot->y + 1] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}} else if (robot->direction == WEST) {if (grid[robot->x - 1][robot->y] != BLOCK) {turn_right(robot);} else if (grid[robot->x][robot->y - 1] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}}if (initialX == robot->x && initialY == robot->y && initialDirection == robot->direction) {// Robot is stuck, rotate 180 degreesturn_left(robot);turn_left(robot);}forward(robot, grid);sleep(500); // Adjust sleep duration for animation speedpickUpMarker(robot, grid);}
}int canMoveForward(Robot *robot, char grid[][GRID_SIZE_X]) 
{int nextX = robot->x;int nextY = robot->y;if (robot->direction == NORTH) {--nextY;} else if (robot->direction == SOUTH) {++nextY;} else if (robot->direction == EAST) {++nextX;} else if (robot->direction == WEST) {--nextX;}// Check if the next position is validif (nextX >=1 && nextX <= GRID_SIZE_X && nextY >= 1 && nextY <= GRID_SIZE_X && grid[nextX][nextY] != BLOCK) {// Move the robot//grid[robot->x][robot->y] = EMPTY;//robot->x = nextX;//robot->y = nextY;//grid[robot->x][robot->y] = 'R'; // Robot represented by 'R'return 1;}return 0;}// Function to turn the robot left (anti-clockwise)
void left(Robot *robot) {if (robot->direction == NORTH) {robot->direction = WEST;} else if (robot->direction ==SOUTH) {robot->direction = EAST;} else if (robot->direction == EAST) {robot->direction =NORTH;} else if (robot->direction == WEST) {robot->direction = SOUTH;}drawRobot(robot->x, robot->y, robot->direction);}void right(Robot *robot) {if (robot->direction == NORTH) {robot->direction = EAST;} else if (robot->direction ==SOUTH) {robot->direction = WEST;} else if (robot->direction ==EAST) {robot->direction =SOUTH;} else if (robot->direction == WEST) {robot->direction =NORTH;}drawRobot(robot->x, robot->y, robot->direction);}#if 0
bool findShortestPath(Robot &robot, char grid[][GRID_SIZE_X]) {// Use BFS to find the shortest pathstd::queue<std::pair<int, int>> q; // Queue for BFSstd::vector<std::vector<bool>> visited(GRID_SIZE_X, std::vector<bool>(GRID_SIZE_X, false));q.push({robot.x, robot.y});visited[robot.x][robot.y] = true;int u=robot.x*GRID_SIZE_X+robot.y;father[robot.x][robot.y]=u;sleep(2000);while (!q.empty()) {int x = q.front().first;int y = q.front().second;q.pop();//drawRobot(x, y, (int)robot.direction);if (grid[x][y] == MARKER) {// Found a marker, pick it uppickUpMarker(robot, grid);//dropMarker(robot, grid);//drawMarker(MARKER_X,MARKER_Y);//drawRobot(x, y, (int)robot.direction);return true;}// Explore neighboring cellsint dx[] = {-1, 1, 0, 0};int dy[] = {0, 0, -1, 1};for (int i = 0; i < 4; ++i) {u=x*GRID_SIZE_X+y;int nx = x + dx[i];int ny = y + dy[i];if (nx >= 0 && nx < GRID_SIZE_X && ny >= 0 && ny < GRID_SIZE_X && !visited[nx][ny] && grid[nx][ny] != BLOCK) {q.push({nx, ny});//drawRobot(nx, ny, (int)robot.direction);//drawStep(nx, ny);//printf("x=%d y=%d \n",nx,ny);visited[nx][ny] = true;father[nx][ny]=u;//dist[nx][ny]=1+dist[x][y];last_dir[nx][ny]=i;}// printf("findShortestPath 2222\n");//sleep(200);}//drawRobot(robot.x, robot.y, (int)robot.direction);//sleep(10);}return false; // No markers found
}#elsechar findShortestPath(int robot_x,int robot_y, char grid[][GRID_SIZE_X]) {// Use BFS to find the shortest path//std::queue<std::pair<int, int>> q; // Queue for BFS//std::vector<std::vector<bool>> visited(GRID_SIZE_X, std::vector<bool>(GRID_SIZE_X, false));int front,rear;rear=front=0;Robot robot;robot.x = robot_x;robot.y = robot_y;//q.push({robot.x, robot.y});g_has_visited[robot.x][robot.y] = 1;int u=robot.x*GRID_SIZE_X+robot.y;g_father[robot.x][robot.y]=u;Queue[rear++]=u;sleep(1000);//while (!q.empty()) while(rear>front){u=Queue[front++];//int x = q.front().first;//int y = q.front().second;//q.pop();//drawRobot(x, y, (int)robot.direction);int x=u/GRID_SIZE_X;int y=u%GRID_SIZE_X;if (grid[x][y] == MARKER) {// Found a marker, pick it uppickUpMarker(&robot, grid);//dropMarker(robot, grid);//drawMarker(MARKER_X,MARKER_Y);//drawRobot(x, y, (int)robot.direction);return 1;}// Explore neighboring cellsint dx[] = {-1, 1, 0, 0};int dy[] = {0, 0, -1, 1};for (int i = 0; i < 4; ++i) {//u=x*GRID_SIZE_X+y;int nx = x + dx[i];int ny = y + dy[i];if (nx >= 0 && nx < GRID_SIZE_X && ny >= 0 && ny < GRID_SIZE_X && !g_has_visited[nx][ny] && grid[nx][ny] != BLOCK) {#if 0q.push({nx, ny});//drawRobot(nx, ny, (int)robot.direction);//drawStep(nx, ny);//printf("x=%d y=%d \n",nx,ny);visited[nx][ny] = true;father[nx][ny]=u;//dist[nx][ny]=1+dist[x][y];last_dir[nx][ny]=i;#elseint v=nx*GRID_SIZE_X+ny;Queue[rear++]=v;g_has_visited[nx][ny]=1;g_father[nx][ny]=u;dist[nx][ny]=1+dist[x][y];last_dir[nx][ny]=i;if (grid[nx][ny] == MARKER){return 1;}#endif}// printf("findShortestPath 2222\n");//sleep(200);}//drawRobot(robot.x, robot.y, (int)robot.direction);//sleep(10);}return 0; // No markers found
}#endifvoid print_path(int x,int y,char has_marker)
{int steps=0;int pos_x,pos_y,direction;while(1){int u=g_father[x][y];int fx=u/GRID_SIZE_X;int fy=u%GRID_SIZE_X;if (fx==x && fy==y){break;}dir[steps]=last_dir[x][y];coordinate[steps++] = x*GRID_SIZE_X+y;x=fx;y=fy;}while(steps--){pos_x = coordinate[steps]/GRID_SIZE_X;pos_y = coordinate[steps]%GRID_SIZE_X;direction = dir[steps];if(has_marker){drawRobotWithBg(pos_x,pos_y,direction);}else{drawRobot(pos_x, pos_y,direction);}sleep(300);}}int main(int argc, char **argv) {char grid[GRID_SIZE_X][GRID_SIZE_X];initializeGrid(grid);Robot robot;robot.x = ROBOT_X; // Initial X positionrobot.y = ROBOT_Y; // Initial Y positionrobot.direction = EAST;robot.carryingMarker = 0;// Display the initial griddisplayGrid(grid);drawHome(HOME_X, HOME_Y);drawMarker(MARKER_X,MARKER_Y);drawBlocks(8,8);	drawBlocks(9,5);drawBlocks(8,5);drawBlocks(7,5);drawBlocks(6,7);drawBlocks(8,7);drawBlocks(7,8);drawBlocks(2,2);drawBlocks(3,3);drawBlocks(4,4);drawBlocks(5,5);drawBlocks(6,6);#if 0findAndCollectMarkers(&robot, grid);#elif 0while (!robot.carryingMarker) {if(canMoveForward(&robot, grid)){forward(robot, grid);}else{left(robot);}sleep(500);//printf("robot.x = %d  y = %d dir = %d\n",robot.x,robot.y,robot.direction);}
#elsewhile (!findShortestPath(robot.x,robot.y, grid)) {forward(&robot, grid);sleep(500); // Adjust sleep duration for animation speed}print_path(MARKER_X,MARKER_Y,0);robot.x = MARKER_X;robot.y = MARKER_Y;grid[MARKER_X][MARKER_Y] = EMPTY;grid[HOME_X][HOME_Y] = MARKER;memset(g_has_visited,0,sizeof(g_has_visited));while (!findShortestPath(robot.x,robot.y, grid)) {forward(&robot, grid);sleep(500); // Adjust sleep duration for animation speed}print_path(HOME_X,HOME_Y,1);#endifreturn 0;
}

C++版本

#include <unistd.h> // For sleep function#include "graphics.h"#define GRID_SIZE_X 10
#define GRID_SIZE_Y 10#define ROBOT 'R'const int maxn=105;#define HOME_X	0
#define HOME_Y	0#define MARKER_X	7
#define MARKER_Y	7#define	ROBOT_X	9
#define	ROBOT_Y	0int father[maxn][maxn];	
int dist[maxn][maxn];
int last_dir[maxn][maxn];int dir[maxn*maxn];	bool visit_first[maxn][maxn];		//vis[x][y]表示xy点是否遍历过
bool visit_second[maxn][maxn];		//vis[x][y]表示xy点是否遍历过int Queue[maxn*maxn];			//用Q来模拟队列 给定两个下标 front和rear 那么入队则是Q[rear++]=u  出队是u=Q[front++]int coordinate[maxn*maxn];const int squareSize = 50;const int windowSize = 600;// Define the grid elements
const char EMPTY = ' ';
const char BLOCK = '#';
const char MARKER = '*';
const char HOME = 'H';// Define robot directions
enum  Direction { WEST , EAST, NORTH, SOUTH };// Define the robot struct
struct Robot {int x;int y;Direction direction;bool carryingMarker;
};void drawStep(int homeX, int homeY)
{background();setColour(pink);homeX = homeX*squareSize;homeY = homeY*squareSize;fillRect(homeX, homeY, squareSize, squareSize);
}void drawMarker(int x,int y)
{background();setColour(gray);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); 
}void drawBlocks(int x,int y)
{background();setColour(black);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); 
}void drawEmpty(int x,int y)
{foreground();setColour(white);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); 
}// Function to initialize the grid
void initializeGrid(char grid[][GRID_SIZE_Y]) {// Initialize the grid with empty spacesfor (int i = 0; i < GRID_SIZE_X; ++i) {for (int j = 0; j < GRID_SIZE_X; ++j) {grid[i][j] = EMPTY;}}// Place blocks, markers, and home squaregrid[8][8] = BLOCK;grid[9][5] = BLOCK;grid[8][5] = BLOCK;grid[7][5] = BLOCK;grid[6][7] = BLOCK;grid[8][7] = BLOCK; grid[7][8] = BLOCK; grid[7][8] = BLOCK; grid[2][2] = BLOCK; grid[3][3] = BLOCK; grid[4][4] = BLOCK; grid[5][5] = BLOCK; grid[6][6] = BLOCK; grid[MARKER_X][MARKER_Y] = MARKER;grid[HOME_X][HOME_Y] = HOME;
}// Function to display the grid
void displayGrid(const char grid[][GRID_SIZE_X]) {setWindowSize(windowSize, windowSize);background();   // Must draw on the background layer.int x;int y;for (x=0; x<GRID_SIZE_X; x++) {for (y=0; y<GRID_SIZE_X; y++){drawRect(x*squareSize, y*squareSize, squareSize, squareSize);}}}void draw_north(int x, int y)
{int x_coords[] = {x, x+50, x+25};int y_coords[] = {y+50, y+50, y};fillPolygon(3, x_coords, y_coords);
}void draw_east(int x, int y)
{int x_coords[] = {x, x, x+50};int y_coords[] = {y, y+50, y+25};fillPolygon(3, x_coords, y_coords);
}void draw_south(int x, int y)
{int x_coords[] = {x, x+50, x+25};int y_coords[] = {y, y, y+50};fillPolygon(3, x_coords, y_coords);
}void draw_west(int x, int y)
{int x_coords[] = {x+50, x+50, x};int y_coords[] = {y, y+50, y+25};fillPolygon(3, x_coords, y_coords);
}// Function to drop a marker
void dropMarker(Robot &robot, char grid[][GRID_SIZE_X]) {if (!robot.carryingMarker) {return; // Robot is not carrying a marker}grid[robot.x][robot.y] = MARKER;robot.carryingMarker = false;//drawRobot(robot.x, robot.y, (int)robot.direction);}void drawRobot(int x, int y, int direction)
{foreground();clear();setColour(green);x = x*squareSize;y = y*squareSize;switch (direction){case Direction::NORTH: draw_north(x, y); break;case Direction::EAST: draw_east(x, y); break;case Direction::SOUTH: draw_south(x, y); break;case Direction::WEST: draw_west(x, y); break;}}void drawRobotWithBg(int x, int y, int direction)
{foreground();clear();setColour(gray);x = x*squareSize;y = y*squareSize;fillRect(x, y, squareSize, squareSize); setColour(green);switch (direction){case Direction::NORTH: draw_north(x, y); break;case Direction::EAST: draw_east(x, y); break;case Direction::SOUTH: draw_south(x, y); break;case Direction::WEST: draw_west(x, y); break;}}void drawHome(int homeX, int homeY)
{background();setColour(blue);homeX = homeX*squareSize;homeY = homeY*squareSize;fillRect(homeX, homeY, squareSize, squareSize);
}void drawShort(int homeX, int homeY)
{background();setColour(orange);homeX = homeX*squareSize;homeY = homeY*squareSize;fillRect(homeX, homeY, squareSize, squareSize);
}void forward(Robot *robot, char grid[][GRID_SIZE_X]) {// Calculate the next position based on the directionint nextX = robot->x;int nextY = robot->y;if (robot->direction == NORTH) {--nextX;} else if (robot->direction == SOUTH) {++nextX;} else if (robot->direction == EAST) {++nextY;} else if (robot->direction == WEST) {--nextY;}// Check if the next position is validif (nextX >= 0 && nextX < GRID_SIZE_X && nextY >= 0 && nextY < GRID_SIZE_X && grid[nextX][nextY] != BLOCK) {// Move the robotgrid[robot->x][robot->y] = EMPTY;robot->x = nextX;robot->y = nextY;grid[robot->x][robot->y] = ROBOT;}drawRobot(robot->x, robot->y, robot->direction);}bool markersLeft(char grid[][GRID_SIZE_X]) {for (int i = 0; i < GRID_SIZE_X; ++i) {for (int j = 0; j < GRID_SIZE_X; ++j) {if (grid[i][j] == MARKER) {return true;}}}return false;
}void turn_left(Robot *robot) {if (robot->direction == NORTH) {robot->direction = WEST;} else if (robot->direction == SOUTH) {robot->direction = EAST;} else if (robot->direction == EAST) {robot->direction = NORTH;} else if (robot->direction == WEST) {robot->direction = SOUTH;}drawRobot(robot->x, robot->y, robot->direction);}void turn_right(Robot *robot) {if (robot->direction == NORTH) {robot->direction = EAST;} else if (robot->direction == SOUTH) {robot->direction = WEST;} else if (robot->direction == EAST) {robot->direction = SOUTH;} else if (robot->direction == WEST) {robot->direction = NORTH;}drawRobot(robot->x, robot->y, robot->direction);}// Function to pick up a marker
void pickUpMarker(Robot *robot, char grid[][GRID_SIZE_Y]) {if (grid[robot->x][robot->y] == MARKER) {robot->carryingMarker = true;grid[robot->x][robot->y] = EMPTY;}
}void findAndCollectMarkers(Robot *robot, char grid[][GRID_SIZE_X]) {while (markersLeft(grid)) {int initialX = robot->x;int initialY = robot->y;Direction initialDirection = robot->direction;// Use the "right hand rule" to navigateif (robot->direction == NORTH) {if (grid[robot->x][robot->y + 1] != BLOCK) {turn_right(robot);} else if (grid[robot->x - 1][robot->y] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}} else if (robot->direction == SOUTH) {if (grid[robot->x][robot->y - 1] != BLOCK) {turn_right(robot);} else if (grid[robot->x + 1][robot->y] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}} else if (robot->direction == EAST) {if (grid[robot->x + 1][robot->y] != BLOCK) {turn_right(robot);} else if (grid[robot->x][robot->y + 1] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}} else if (robot->direction == WEST) {if (grid[robot->x - 1][robot->y] != BLOCK) {turn_right(robot);} else if (grid[robot->x][robot->y - 1] != BLOCK) {forward(robot, grid);} else {turn_left(robot);}}if (initialX == robot->x && initialY == robot->y && initialDirection == robot->direction) {// Robot is stuck, rotate 180 degreesturn_left(robot);turn_left(robot);}forward(robot, grid);sleep(500); // Adjust sleep duration for animation speedpickUpMarker(robot, grid);}
}int canMoveForward(Robot *robot, char grid[][GRID_SIZE_X]) 
{int nextX = robot->x;int nextY = robot->y;if (robot->direction == Direction::NORTH) {--nextY;} else if (robot->direction == Direction::SOUTH) {++nextY;} else if (robot->direction == Direction::EAST) {++nextX;} else if (robot->direction == Direction::WEST) {--nextX;}// Check if the next position is validif (nextX >=1 && nextX <= GRID_SIZE_X && nextY >= 1 && nextY <= GRID_SIZE_X && grid[nextX][nextY] != BLOCK) {// Move the robot//grid[robot->x][robot->y] = EMPTY;//robot->x = nextX;//robot->y = nextY;//grid[robot->x][robot->y] = 'R'; // Robot represented by 'R'return 1;}return 0;}// Function to turn the robot left (anti-clockwise)
void left(Robot *robot) {if (robot->direction == Direction::NORTH) {robot->direction = Direction::WEST;} else if (robot->direction == Direction::SOUTH) {robot->direction = Direction::EAST;} else if (robot->direction == Direction::EAST) {robot->direction = Direction::NORTH;} else if (robot->direction == Direction::WEST) {robot->direction = Direction::SOUTH;}drawRobot(robot->x, robot->y, robot->direction);}void right(Robot *robot) {if (robot->direction == Direction::NORTH) {robot->direction = Direction::EAST;} else if (robot->direction == Direction::SOUTH) {robot->direction = Direction::WEST;} else if (robot->direction == Direction::EAST) {robot->direction = Direction::SOUTH;} else if (robot->direction == Direction::WEST) {robot->direction = Direction::NORTH;}drawRobot(robot->x, robot->y, robot->direction);}// Function to pick up a marker
void pickUpMarker(Robot &robot, char grid[][GRID_SIZE_X]) {if (grid[robot.x][robot.y] == MARKER) {robot.carryingMarker = true;grid[robot.x][robot.y] = EMPTY;//drawRobot(robot.x, robot.y, (int)robot.direction);}
}#if 0
bool findShortestPath(Robot &robot, char grid[][GRID_SIZE_X]) {// Use BFS to find the shortest pathstd::queue<std::pair<int, int>> q; // Queue for BFSstd::vector<std::vector<bool>> visited(GRID_SIZE_X, std::vector<bool>(GRID_SIZE_X, false));q.push({robot.x, robot.y});visited[robot.x][robot.y] = true;int u=robot.x*GRID_SIZE_X+robot.y;father[robot.x][robot.y]=u;sleep(2000);while (!q.empty()) {int x = q.front().first;int y = q.front().second;q.pop();//drawRobot(x, y, (int)robot.direction);if (grid[x][y] == MARKER) {// Found a marker, pick it uppickUpMarker(robot, grid);//dropMarker(robot, grid);//drawMarker(MARKER_X,MARKER_Y);//drawRobot(x, y, (int)robot.direction);return true;}// Explore neighboring cellsint dx[] = {-1, 1, 0, 0};int dy[] = {0, 0, -1, 1};for (int i = 0; i < 4; ++i) {u=x*GRID_SIZE_X+y;int nx = x + dx[i];int ny = y + dy[i];if (nx >= 0 && nx < GRID_SIZE_X && ny >= 0 && ny < GRID_SIZE_X && !visited[nx][ny] && grid[nx][ny] != BLOCK) {q.push({nx, ny});//drawRobot(nx, ny, (int)robot.direction);//drawStep(nx, ny);//printf("x=%d y=%d \n",nx,ny);visited[nx][ny] = true;father[nx][ny]=u;//dist[nx][ny]=1+dist[x][y];last_dir[nx][ny]=i;}// printf("findShortestPath 2222\n");//sleep(200);}//drawRobot(robot.x, robot.y, (int)robot.direction);//sleep(10);}return false; // No markers found
}#elsebool findShortestPath(int robot_x,int robot_y, char grid[][GRID_SIZE_X],bool visit[][maxn]) {// Use BFS to find the shortest path//std::queue<std::pair<int, int>> q; // Queue for BFS//std::vector<std::vector<bool>> visited(GRID_SIZE_X, std::vector<bool>(GRID_SIZE_X, false));int front,rear;rear=front=0;Robot robot;robot.x = robot_x;robot.y = robot_y;//q.push({robot.x, robot.y});bool **visited = NULL;visit[robot.x][robot.y] = true;int u=robot.x*GRID_SIZE_X+robot.y;father[robot.x][robot.y]=u;Queue[rear++]=u;sleep(1000);//while (!q.empty()) while(rear>front){u=Queue[front++];//int x = q.front().first;//int y = q.front().second;//q.pop();//drawRobot(x, y, (int)robot.direction);int x=u/GRID_SIZE_X;int y=u%GRID_SIZE_X;if (grid[x][y] == MARKER) {// Found a marker, pick it uppickUpMarker(robot, grid);//dropMarker(robot, grid);//drawMarker(MARKER_X,MARKER_Y);//drawRobot(x, y, (int)robot.direction);return true;}// Explore neighboring cellsint dx[] = {-1, 1, 0, 0};int dy[] = {0, 0, -1, 1};for (int i = 0; i < 4; ++i) {//u=x*GRID_SIZE_X+y;int nx = x + dx[i];int ny = y + dy[i];if (nx >= 0 && nx < GRID_SIZE_X && ny >= 0 && ny < GRID_SIZE_X && !visit[nx][ny] && grid[nx][ny] != BLOCK) {#if 0q.push({nx, ny});//drawRobot(nx, ny, (int)robot.direction);//drawStep(nx, ny);//printf("x=%d y=%d \n",nx,ny);visited[nx][ny] = true;father[nx][ny]=u;//dist[nx][ny]=1+dist[x][y];last_dir[nx][ny]=i;#elseint v=nx*GRID_SIZE_X+ny;Queue[rear++]=v;visit[nx][ny]=true;father[nx][ny]=u;dist[nx][ny]=1+dist[x][y];last_dir[nx][ny]=i;if (grid[nx][ny] == MARKER){return true;}#endif}// printf("findShortestPath 2222\n");//sleep(200);}//drawRobot(robot.x, robot.y, (int)robot.direction);//sleep(10);}return false; // No markers found
}#endifvoid print_path(int x,int y,bool has_marker)
{int steps=0;int pos_x,pos_y,direction;while(true){int u=father[x][y];int fx=u/GRID_SIZE_X;int fy=u%GRID_SIZE_X;if (fx==x && fy==y){break;}dir[steps]=last_dir[x][y];coordinate[steps++] = x*GRID_SIZE_X+y;x=fx;y=fy;}while(steps--){pos_x = coordinate[steps]/GRID_SIZE_X;pos_y = coordinate[steps]%GRID_SIZE_X;direction = dir[steps];if(has_marker){drawRobotWithBg(pos_x,pos_y,direction);}else{drawRobot(pos_x, pos_y,direction);}sleep(300);}}int main(int argc, char **argv) {char grid[GRID_SIZE_X][GRID_SIZE_X];initializeGrid(grid);Robot robot;robot.x = ROBOT_X; // Initial X positionrobot.y = ROBOT_Y; // Initial Y positionrobot.direction = Direction::EAST;robot.carryingMarker = false;// Display the initial griddisplayGrid(grid);drawHome(HOME_X, HOME_Y);drawMarker(MARKER_X,MARKER_Y);drawBlocks(8,8);	drawBlocks(9,5);drawBlocks(8,5);drawBlocks(7,5);drawBlocks(6,7);drawBlocks(8,7);drawBlocks(7,8);drawBlocks(2,2);drawBlocks(3,3);drawBlocks(4,4);drawBlocks(5,5);drawBlocks(6,6);#if 0findAndCollectMarkers(&robot, grid);#elif 0while (!robot.carryingMarker) {if(canMoveForward(&robot, grid)){forward(robot, grid);}else{left(robot);}sleep(500);//printf("robot.x = %d  y = %d dir = %d\n",robot.x,robot.y,robot.direction);}
#elsewhile (!findShortestPath(robot.x,robot.y, grid,visit_first)) {forward(&robot, grid);sleep(500); // Adjust sleep duration for animation speed}print_path(MARKER_X,MARKER_Y,false);robot.x = MARKER_X;robot.y = MARKER_Y;grid[MARKER_X][MARKER_Y] = EMPTY;grid[HOME_X][HOME_Y] = MARKER;while (!findShortestPath(robot.x,robot.y, grid,visit_second)) {forward(&robot, grid);sleep(500); // Adjust sleep duration for animation speed}print_path(HOME_X,HOME_Y,true);#endifreturn 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/143287.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Web服务Openlab的搭建

Web服务Openlab的搭建 网站需求&#xff1a; 基于域名 www.openlab.com 可以访问网站内容为 welcome to openlab!!! 给该公司创建三个子界面分别显示学生信息&#xff0c;教学资料和缴费网站 基于 www.openlab.com/student 网站访问学生信息&#xff0c; 基于 www.openlab.…

MongoDB入门级别教程全(Windows版,保姆级教程)

下载mongodb 进入官网&#xff1a; Download MongoDB Community Server | MongoDB 选择msi&#xff0c;Windows版本 下载完后直接双击&#xff1a; 选择complete 这里建议改地方&#xff1a; 我这里直接改成d盘&#xff1a;work目录下面&#xff1a; 点击next&#xff1a; 因…

spring cloud-注册中心(Eureka)

一、服务注册中心组件(*) 定义&#xff1a;服务注册中心就是在整个微服务架构单独抽取一个服务&#xff0c;该服务不做项目中任何业务功能&#xff0c;仅用来在微服务中记录微服务、对微服务进行健康状态检查&#xff0c;及服务元数据信息存储常用的注册中心&#xff1a;eurek…

[蓝桥杯复盘] 第 3 场双周赛20231111

[蓝桥杯复盘] 第 3 场双周赛20231111 总结深秋的苹果1. 题目描述2. 思路分析3. 代码实现 鲜花之海1. 题目描述2. 思路分析3. 代码实现 斐波拉契跳跃2. 思路分析3. 代码实现 星石传送阵2. 思路分析3. 代码实现 六、参考链接 总结 做了后4题。https://www.lanqiao.cn/oj-contes…

『亚马逊云科技产品测评』活动征文|阿里云服务器亚马逊服务器综合评测

授权声明&#xff1a;本篇文章授权活动官方亚马逊云科技文章转发、改写权&#xff0c;包括不限于在 Developer Centre, 知乎&#xff0c;自媒体平台&#xff0c;第三方开发者媒体等亚马逊云科技官方渠道 文章目录 引言一、亚马逊&阿里云发展历史介绍1.1 亚马逊发展历史1.2…

Centos, RockyLinux 常用软件安装汇总

一、基本指令&#xff1a; 命令作用clear清屏pwd显示当前路径cat / more显示文本文档uname -a查看当前版本hostnamectl查看当前版本cat /etc/redhat-release查看当前版本free查看剩余内存df -h[查看磁盘剩余空间]du -sh 查看文件夹名"dir"占用的空间lsof -i:8080查看…

【原创】java+swing+mysql车辆维修管理系统设计与实现

摘要&#xff1a; 车辆维修管理系统是一个用于管理和追踪车辆维修过程的系统&#xff0c;它能够提高效率&#xff0c;减少错误&#xff0c;并提供详细的车辆历史记录&#xff0c;可以帮助车辆维修企业实现信息化管理&#xff0c;提高工作效率和客户满意度&#xff0c;降低运营…

DevEco studio配置自己的虚拟环境

开始使用DevEco studio时使用的时华为预置的手机&#xff0c;通过网络访问&#xff0c;但是近期发现有两点问题 网络不稳定的时候机器很卡现在资源很难使用 DevEco提供了自定义环境的搭建&#xff0c;从而解决上面的问题 这里有几点问题需要硬盘至少10G空闲&#xff08;应该问题…

【考研数据结构代码题6】构建二叉树及四大遍历(先中后层)

题目&#xff1a;请你编写完整的程序构建一棵二叉树并对其进行先序遍历、中序遍历、后序遍历与层次遍历&#xff0c;分别打印并输出遍历结果 难度&#xff1a;★★★ 二叉树的存储结构 typedef struct Node{char data;//数据域struct Node* left;//左子树struct Node* right;//…

【评论送书】十本架构师成长和软件架构技术相关的好书(可以任选)

正文开始前给大家推荐个网站&#xff0c;前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。 参与规则 本次送书1~5本参与方式&#xff1a;关注博主、点赞、收藏、评论&#xff08;从评论区…

[工业自动化-18]:西门子S7-15xxx编程 - 软件编程 - PLC用于工业领域的嵌入式系统:硬件原理图、指令系统、系统软件架构、开发架构等

目录 前言&#xff1a; 一、PLC的硬件电路原理 1.1 硬件框图 1.2 硬件模块详解 &#xff08;1&#xff09;CPU &#xff08;2&#xff09;存储器 &#xff08;3&#xff09;输入/输出&#xff08;I/O&#xff09;模块 &#xff08;4&#xff09;编程器 &#xff08;5&a…

Flume(一)【Flume 概述】

前言 今天实在不知道学点什么好了&#xff0c;早上学了3个多小时的 Flink &#xff0c;整天只学一门技术是很容易丧失兴趣的。那就学点新的东西 Flume&#xff0c;虽然 Kafka 还没学完&#xff0c;但是大数据生态圈的基础组件也基本就剩这倆了。 Flume 概述 生产环境中的数据一…

一道 python 数据分析的题目

python 数据分析的题目。 做题方法&#xff1a;使用 pandas 读取数据&#xff0c;然后分析。 知识点&#xff1a;pandas&#xff0c;正则表达式&#xff0c;py知识。 过程&#xff1a;不断使用 GPT&#xff0c;遇到有问题的地方自己分析&#xff0c;把分析的结果告诉 GPT&am…

点成方案丨使用细胞计数仪监控CAR-T细胞疗法的生产

一、概述 嵌合抗原受体&#xff08;CAR&#xff09;是经过改造后赋予T细胞靶向特定抗原的新能力的受体蛋白。这些受体是嵌合的&#xff0c;因为它们将抗原结合和T细胞激活功能结合到一个受体中。CAR-T细胞疗法使用经过CAR改造的T细胞来治疗癌症。CAR-T免疫疗法的前提是修改T细…

PHP原生类总结利用

SPL介绍 SPL就是Standard PHP Library的缩写。据手册显示&#xff0c;SPL是用于解决典型问题(standard problems)的一组接口与类的集合。打开手册&#xff0c;正如上面的定义一样&#xff0c;有许多封装好的类。因为是要解决典型问题&#xff0c;免不了有一些处理文件…

如何使用iPhone15在办公室观看家里电脑上的4k电影?

如何使用iPhone15在办公室观看家里电脑上的4k电影&#xff1f; 文章目录 如何使用iPhone15在办公室观看家里电脑上的4k电影&#xff1f;1.使用环境要求&#xff1a;2.下载群晖videostation&#xff1a;3.公网访问本地群晖videostation中的电影&#xff1a;4.公网条件下使用电脑…

论文浅尝 | 用于开放式文本生成的事实增强语言模型

笔记整理&#xff1a;李煜&#xff0c;东南大学硕士&#xff0c;研究方向为知识图谱 链接&#xff1a;https://proceedings.neurips.cc/paper_files/paper/2022/hash/df438caa36714f69277daa92d608dd63-Abstract-Conference.html 1. 动机 生成式语言模型&#xff08;例如 GPT-3…

CNCC 2023收官,Milvus Cloud与行业大咖共话向量数据库系统

近期,CNCC 2023 在沈阳圆满结束,紧凑、前沿的 129 场技术论坛让人印象深刻。据悉,这 129 场技术论坛涵盖人工智能、安全、计算+、软件工程、教育、网络、芯片、云计算等 30 余个方向。Zilliz 受邀参与【智能时代的大数据系统】技术论坛。 智能时代的到来,无疑给社会经济和日…

【深度学习】SimSwap: An Efficient Framework For High Fidelity Face Swapping 换脸,实战

代码&#xff1a;https://github.com/neuralchen/SimSwap 文章目录 摘要介绍RELATED WORK实验结论代码实操 SimSwap是一个高保真度人脸交换的高效框架。它将源脸的身份转移到目标脸上&#xff0c;同时保留目标脸的属性。该框架包括ID注入模块&#xff08;IIM&#xff09;&#…

Avatar虚拟形象解决方案,趣味化的视频拍摄与直播新体验

企业们正在寻找新的方式来吸引和保持观众的注意力,一种新兴的解决方案就是使用Avatar虚拟形象技术&#xff0c;这种技术可以让用户在视频拍摄或直播场景中&#xff0c;以自定义的数字人形象出现&#xff0c;同时保持所有的表情和脸部驱动。美摄科技正是这个领域的领军者&#x…