-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBYTESE1_bfs.cpp
119 lines (111 loc) · 1.93 KB
/
BYTESE1_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//be careful with indexing as it is from one not zero
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
int time;
node(int a1,int b1,int time_var)
{
x=a1;
y=b1;
time=time_var;
}
};
bool operator<(const node& a, const node& b)
{
return a.time > b.time;
}
priority_queue<node> q;
int k,arr[101][101],m,n,a,b,T,min_time,x,y,x_var,y_var;
bool vis[101][101];
int temp_x[]={-1, 0, 1, 0};
int temp_y[]={ 0, 1, 0,-1};
int flag=0;
void bfs()
{
while(!q.empty())
{
node src=q.top();
q.pop();
x_var=src.x;
y_var=src.y;
if(x_var==a && y_var==b)
{
min_time=src.time;
//cout<<"Source time:"<<src.time<<"\n";
//cout<<"Minimum Time: "<<min_time<<"\n";
flag=1;
while(!q.empty())
q.pop();
return;
}
//cout<<"Function started x:"<<src.x <<" y:"<<src.y <<"time:"<<src.time<<"\n";
for(int i=0;i<4;i++)
{
x=x_var+temp_x[i];
y=y_var+temp_y[i];
if(x<1||x>m||y<1||y>n||vis[x][y]==true)
continue;
node val=node(x,y,src.time+arr[x][y]);
// cout<<"Pushing values for postions x:"<<x <<" y:"<<y <<"time:"<<val.time<<"\n";
vis[x][y]=true;
q.push(val);
}
}
}
int main()
{
cin>>k;
for (int l=0;l<k;l++)
{
flag=0;
cin>>m;
cin>>n;
for(int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
cin>>arr[i][j];
for(int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
vis[i][j]=false;
cin>>a;
cin>>b;
cin>>T;
min_time=T;
// cout<<"Input Array is:\n";
/* for(int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
cout<<arr[i][j]<<" ";
}
// cout<<"\n";
}*/
if(a==1&&b==1)
{
if(T>=arr[1][1])
{
cout<<"YES\n";
cout<<T-arr[1][1]<<"\n";
}
else
cout<<"NO\n";
continue;
}
node source=node(1,1,arr[1][1]);
q.push(source);
vis[1][1]=true;
bfs();
//cout<<"min time"<<min_time;
int save_time=T-min_time;
if(save_time>=0 && flag==1)
{
cout<<"YES\n";
cout<<save_time<<"\n";
}
else if(flag==0)
cout<<"NO\n";
}
}