-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLASERP.cpp
102 lines (95 loc) · 2.37 KB
/
MLASERP.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
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define lld long long int
int r[]={1,0,-1,0};
int c[]={0,1,0,-1};
int w,h,vis[101][101];
char arr[101][101];
bool valid(int x,int y)
{
if(x<h&&y<w&&x>=0&&y>=0&&arr[x][y]!='*')
return true;
return false;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lld t;
//cin>>t;
t=1;
while(t--)
{
cin>>w>>h;
int x1,y1,x2,y2,ptr=0,res[h][w];
string s;
for(int i=0;i<h;i++)
{
cin>>s;
for(int j=0;j<w;j++)
{
arr[i][j]=s[j];
res[i][j]=101;
if(arr[i][j]=='C')
{
if(ptr==0)
{
x1=i;
y1=j;
ptr=1;
}
else
{
x2=i;
y2=j;
}
}
}
}
queue <pair <int,int> >q;
pair <int,int> p;
res[x1][y1]=-1;
q.push({x1,y1});
ptr=0;
while(!q.empty())
{
p=q.front();
int x=p.first;
int y=p.second;
q.pop();
for(int i=0;i<4;i++)
{
int new_x=x+r[i], new_y=y+c[i];
while(valid(new_x,new_y))
{
if(res[new_x][new_y]>res[x][y]+1)
{
q.push({new_x,new_y});
res[new_x][new_y]=res[x][y]+1;
if(new_x==x2&&new_y==y2)
{
ptr=1;
break;
}
}
new_x+=r[i];
new_y+=c[i];
}
if(ptr==1)
break;
}
if(ptr==1)
break;
}
/*for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
cout<<res[i][j]<<" ";
cout<<'\n';
}*/
cout<<res[x2][y2];
}
}