forked from Motu-Lootera/Task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
85 lines (74 loc) · 1.2 KB
/
bfs.cpp
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
82
83
84
85
#include<iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <queue>
using namespace std;
using namespace cv;
Mat a(100,100,CV_8UC1,Scalar(255));
//bool vis[a.rows][a.cols]={};
//bool add[a.rows][a.cols]={};
//bool vis[100][100]={};
bool ad[100][100]={};
struct node
{
int i;
int j;
};
queue<node> qu;
bool isValid(int i,int j)
{
if(i<0||j<0||i>=a.rows||j>=a.cols)
return 0;
if(ad[i][j])
return 0;
return 1;
}
void add_qu(node x)
{
if(isValid(x.i,x.j+1))
{
node tmp={x.i,x.j+1};
qu.push(tmp);
ad[x.i][x.j+1]=1;
}
if(isValid(x.i+1,x.j))
{
node tmp={x.i+1,x.j};
qu.push(tmp);
ad[x.i+1][x.j]=1;
}
if(isValid(x.i-1,x.j))
{
node tmp={x.i-1,x.j};
qu.push(tmp);
ad[x.i-1][x.j]=1;
}
if(isValid(x.i,x.j-1))
{
node tmp={x.i,x.j-1};
qu.push(tmp);
ad[x.i][x.j-1]=1;
}
}
int main()
{
int i=0,j=0;
node curr={i,j};
qu.push(curr);
ad[i][j]=1;
//vis[50][50]=1;
namedWindow("window1",WINDOW_NORMAL);
while(!qu.empty())
{
curr=qu.front();
qu.pop();
add_qu(curr);
//vis[curr.i][curr.j]=1;
a.at<uchar>(curr.i,curr.j)=0;
imshow("window1",a);
waitKey(1);
}
imshow("window1",a);
waitKey(10000);
}