-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAvoidTraps.cpp
More file actions
79 lines (78 loc) · 1.34 KB
/
Copy pathAvoidTraps.cpp
File metadata and controls
79 lines (78 loc) · 1.34 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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t>0)
{
t--;
int r1,r2;
cin>>r1;
cin>>r2;
int n,i,j;
cin>>n;
string text;
cin>>text;
vector<int> cells(n+1,-1);
cells[1]=0;
for(i=2;i<=n;i++)
{
if(cells[i]==-1)
{
cells[i]=1;
for(j=i+i;j<=n;j=j+i)
{
cells[j]=0;
}
}
}
for(i=2;i<=n;i++)
{
cells[i]=cells[i]+cells[i-1];
}
queue<pair<int,int> >q;
std::vector<bool> visited(n+1,false);
if(text[0]!='*')
{
q.push(make_pair(1,0));
visited[1]=true;
}
else
return -1;
int A;
while(!q.empty())
{
pair<int,int> t=q.front();
if(t.first==n && text[t.first-1]!='*')
return t.second;
q.pop();
if(!visited[t.first+1] && text[t.first]!='*')
{
if(t.first+1==n)
return t.second+1;
q.push(make_pair(t.first+1,t.second+1));
visited[t.first+1]=true;
}
if(!visited[t.first+2] && text[t.first+1]!='*')
{
if(t.first+2==n)
return t.second+1;
q.push(make_pair(t.first+2,t.second+1));
visited[t.first+2]=true;
}
A=cells[t.first];
if((float)A/i >= (float)r1/r2 && t.first+A<=n )
{
if(!visited[t.first+A] && text[t.first+A-1]!='*')
{
if(t.first+A==n)
return t.second+1;
q.push(make_pair(t.first+A,t.second+1));
visited[t.first+A]=true;
}
}
}
return -1;
}
}