-
Notifications
You must be signed in to change notification settings - Fork 2
/
10977.cpp
95 lines (88 loc) · 1.79 KB
/
10977.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
//BISMILLAHIRRAHMANIRRAHIM
/* Author :Shakil_AUST
problem: uva 10977 - Enchanted Forest
Type : bfs
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
int row,col;
struct temp
{ int x;
int y;
int c;
temp(int p,int q,int r)
{ x=p;
y=q;
c=r;
}
temp() {}
} in;
char matrix[205][205];
int cost[205][205];
int x_axis[4]={1,-1,0,0};
int y_axis[4]={0,0,-1,1};
void intialization(int row,int col)
{ for(int i=1;i<=row;i++)
for(int j=1;j<=col;j++)
{ matrix[i][j]='.';
cost[i][j]=-1;}
}
void dangerous(int xC,int yC,int L)
{ int xl,yl,xr,yr;
xl=max(xC-L,1),yl=max(yC-L,1),xr=min(xC+L,row),yr=min(yC+L,col);
for(int i=xl;i<=xr;i++)
{
for(int j=yl;j<=yr;j++)
{
if((xC-i)*(xC-i)+(yC-j)*(yC-j)<=L*L)
{
matrix[i][j]='#';
}
}
}
}
int bfs()
{ queue <temp> q;
q.push(temp(1,1,0));
if(row==1 && col==1) return 0;
while(!q.empty())
{ in=q.front();
q.pop();
for(int i=0;i<4;i++)
{ int nx=in.x+x_axis[i];
int ny=in.y+y_axis[i];
if(nx>=1 && nx<=row && ny>=1 && ny<=col && matrix[nx][ny]!='#' && cost[nx][ny]==-1)
{ cost[nx][ny]=in.c+1;
if(nx==row && ny==col)
return cost[nx][ny];
q.push(temp(nx,ny,in.c+1));
}
}
}
return -1;
}
int main()
{ while(cin>>row>>col)
{ if(row==0 && col==0) return 0;
int m,n,x,y,l;
intialization(row,col);
cin>>m;
for(int i=1;i<=m;i++)
{ cin>>x>>y;
matrix[x][y]='#';
}
cin>>n;
for(int i=1;i<=n;i++)
{ cin>>x>>y>>l;
matrix[x][y]='#';
dangerous(x,y,l);
}
int result=bfs();
if(result==-1) printf("Impossible.\n");
else printf("%d\n",result);
}
return 0;
}