-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
81 lines (68 loc) · 1.31 KB
/
dfs.cpp
File metadata and controls
81 lines (68 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include<stack>
#include<vector>
using namespace cv;
using namespace std;
struct node
{
int x,y;
bool val=true;
};
int main()
{
int i,j;
stack <node> abc;
Mat a= imread("lena.png",0);
Mat b(a.rows,a.cols,CV_8UC1,Scalar(0));
node arr[a.rows][a.cols];
namedWindow("window1",WINDOW_NORMAL);
for(i=0;i<a.rows;i++)
{
for(j=0;j<a.cols;j++)
{
arr[i][j].x=i;
arr[i][j].y=j;
}
}
i=0;
j=0;
do
{
if(arr[i][j+1].val&&(j<a.rows-1))
{
abc.push(arr[i][j]);
b.at<uchar>(arr[i][j].x,arr[i][j].y)=255;
arr[i][j++].val=false;
}
else if (arr[i+1][j].val&&(i<a.cols-1))
{
abc.push(arr[i][j]);
b.at<uchar>(arr[i][j].x,arr[i][j].y)=255;
arr[i++][j].val=false;
}
else if (arr[i-1][j].val&&(i>0))
{
abc.push(arr[i][j]);
b.at<uchar>(arr[i][j].x,arr[i][j].y)=255;
arr[i--][j].val=false;
}
else if (arr[i][j-1].val&&(j>0))
{
abc.push(arr[i][j]);
b.at<uchar>(arr[i][j].x,arr[i][j].y)=255;
arr[i][j--].val=false;
}
else
{
abc.pop();
node temp=abc.top();
i=temp.x;
j=temp.y;
}
imshow("window1",b);
waitKey(1);
}while(!abc.empty());
}