#include<stdio.h>
#include<stdlib.h>
#include<opencv.hpp>
#include<Windows.h>
#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
#define MAX 100
#define LONG 20
typedef struct {
short int row;
short int col;
}element;
element stack[MAX];
int top = -1;
int maze[LONG][LONG] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,0,1,1,0,1,0,0,0,1,1,1,1,1,0,0},
{1,0,0,1,1,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0},
{1,1,0,0,0,1,1,0,0,1,0,0,1,1,1,1,1,1,0,0},
{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},
{1,1,1,1,0,0,0,0,1,1,0,0,0,1,1,1,1,0,1,1},
{1,1,1,0,0,0,0,0,0,1,0,1,1,0,0,0,1,1,1,1},
{1,1,1,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,0,0,0,1,1,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,0,1},
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1},
};
#define ENTRY_ROW 1
#define ENTRY_COL 1
#define EXIT_ROW 18
#define EXIT_COL 18
Mat mat(400, 400,CV_8UC3);
int step = 20;
void draw(int x, int y, int flag);
void Push(element a)//入栈操作
{
top++;
if (top <= MAX - 1)
stack[top] = a;
else
{
printf("堆栈溢出\n");
exit(1);
}
}
void Pop()//删除操作
{
if (top >= 0)
{
top--;
}
else
{
printf("堆栈溢出\n");
exit(1);
}
}
element GetTop(int top)
{
if (top >= 0)
return stack[top];
else
{
printf("堆栈溢出\n");
exit(1);
}
}
void flagfunction(element pos)
{
maze[pos.row][pos.col] = 2;
}
int havepos(element pos)
{
int flag;
if (maze[pos.row+1][pos.col] == 0)//下
{
return 1;
}
else if (maze[pos.row][pos.col-1] == 0)//左
{
return 1;
}
else if (maze[pos.row-1][pos.col] == 0)//上
{
return 1;
}
else
return 0;
}
void path(void)
{
int f = 0;
int count = 0;
int i, j, row, col;
element position;
top = 0; stack[0].row = ENTRY_ROW; stack[0].col = ENTRY_COL;
position.row = stack[top].row; position.col = stack[top].col;
draw(1, 1, 3);
//flagfunction(position);
while (1)
{
if (maze[position.row][position.col] == 0)
{
printf("(%d %d) %d\n", position.col, position.row,maze[position.row][position.col]);
if (count)
{
Push(position);
draw(position.row, position.col, 3);
imshow("test", mat);
waitKey(200);
}
count = 1;
flagfunction(position);
if (position.row == EXIT_ROW && position.col == EXIT_COL)
return ;
else
{
position.col = position.col + 1;//向右移
}
}
else
{
if (position.col == stack[top].col + 1 && position.row == stack[top].row)
f = 1;
else if (position.col == stack[top].col&&position.row == stack[top].row + 1)
f = 2;
else if (position.col == stack[top].col - 1 && position.row == stack[top].row)
f = 3;
position.row = stack[top].row; position.col = stack[top].col;
if (havepos(position))
{
if (f==1)
{
position.col = stack[top].col; position.row = stack[top].row + 1;
}//右边
else if (f==2)
{
position.col = stack[top].col-1; position.row = stack[top].row;
}//下边
else if (f==3)
{
position.col = stack[top].col; position.row = stack[top].row-1;
}//左边
}
else
{
draw(stack[top].row, stack[top].col, 4);
imshow("test", mat);
waitKey(100);
Pop();
position.row = stack[top].row; position.col = stack[top].col;
}
}
}
}
void draw(int x,int y,int flag)
{
int r, g, b;
if (flag == 1)
{
r = 0, g = 0, b = 0;
}
else if (flag == 2)
{
r = 0, g = 255, b = 255;
}
else if (flag == 3)
{
r = 255, g = 0, b = 0;
}
else if (flag == 4)
{
r = 0, g = 255, b = 0;
}
for (int i = x * 20; i < x*20+20; i++)
{
for (int j = y * 20; j < y*20+20; j++)
{
mat.at<Vec3b>(i, j)[0] = b;
mat.at<Vec3b>(i, j)[1] = g;
mat.at<Vec3b>(i, j)[2] = r;
}
}
}
int main()
{
namedWindow("test");
namedWindow("test2");
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (maze[i][j] == 1)
{
draw(i, j,1);
}
else if(maze[i][j]==0)
{
draw(i, j, 2);
}
}
}
imshow("test2", mat);
waitKey(0);
path();
/*
int t = top;
for (int i = 0; i <= top; i++)
maze[stack[i].row][stack[i].col]=8;
/*
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (maze[i][j] == 8)
{
/*
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
printf("▇");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
}
else printf("%d ", maze[i][j]);
}
printf("\n");
}*/
}
opencv可视化迷宫搜索过程
猜你喜欢
转载自blog.csdn.net/weixin_43343116/article/details/89060705
今日推荐
周排行