-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10653runtimerrorsolvedwrongans.cpp
More file actions
80 lines (68 loc) · 1.63 KB
/
10653runtimerrorsolvedwrongans.cpp
File metadata and controls
80 lines (68 loc) · 1.63 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
#include<bits/stdc++.h>
using namespace std;
//I need a an array of pairs
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
//map< pair<int,int>, pair<int,int> > parent;
int **visited;
int **G;
int m,n;
int BFS(int x,int y, int x1,int y1) // calling bfs also saves the vertices in visited map
{
//queue< int >q;
queue< pair<int,int> > q;
q.push(pair<int,int>(x,y));
visited[x][y] = 0;
//parent[pair<int,int>(x,y)] = pair<int,int>(-1,-1);
while(!q.empty())
{
pair<int,int> top = q.front();
int xaxis = top.first;
int yaxis = top.second;
for(int k=0;k<4;k++)
{
int tx = xaxis + fx[k];
int ty = yaxis + fy[k];
if( tx >= 0 && tx < m && ty >= 0 && ty < n && G[tx][ty] != 1 && visited[tx][ty] == 0){
//parent[pair<int,int>(tx,ty)] = top;
visited[tx][ty] = visited[top.first][top.second] + 1;
q.push(pair<int,int>(tx,ty));
if(x1 == tx && y1 == ty){
return visited[tx][ty];
}
}
}
q.pop();
}
return -1;
}
int main(){
while(cin>>m>>n && m !=0 && n != 0){
int l;
G= new int*[m]; // row count
for(int i = 0; i < m; ++i){
G[i] = new int[n]; // col count
}
visited= new int*[m]; // row count
for(int i = 0; i < m; ++i){
visited[i] = new int[n]; // col count
}
//int G[m][n]; global problem
//memset(G,0,m*n*sizeof(int)); by declaring two diamentional array by upper term.. we do not need memset
cin>>l;
while(l--){
int r,b;
cin>>r>>b;
while(b--){
int c;
cin>>c;
G[r][c] = 1;
}
}
int sx,sy;
int dx,dy;
cin>>sx>>sy>>dx>>dy;
cout<<BFS(sx,sy,dx,dy)<<endl;
//parent.clear();
}
}