-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountLuck.cpp
107 lines (81 loc) · 1.66 KB
/
CountLuck.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
// problem: "https://www.hackerrank.com/challenges/count-luck/problem"
#include<bits/stdc++.h>
#define ll long long
#define p (ll)(-1e9)
using namespace std;
bool isValid(vector<string>&M,int x,int y,int n,int m)
{
if(x<0 || x>=n || y<0 || y>=m)
return false;
if(M[x][y] == 'X')
return false;
return true;
}
int dfs(vector<string>&M,int x,int y,int n,int m)
{
if(x<0 || x>=n || y<0 || y>=m)
return p;
if(M[x][y] == 'X')
return p;
if(M[x][y] == '*')
return 0;
int c = 0;
M[x][y] = 'X';
if(isValid(M,x+1,y,n,m)) // define
c++;
if(isValid(M,x-1,y,n,m))
c++;
if(isValid(M,x,y+1,n,m))
c++;
if(isValid(M,x,y-1,n,m))
c++;
int C;
C = c;
c = max(dfs(M,x+1,y,n,m),0);
c = max(dfs(M,x-1,y,n,m),c);
c = max(dfs(M,x,y+1,n,m),c);
c = max(dfs(M,x,y-1,n,m),c);
if(C>1)
return c+1;
return c;
}
int countWand(vector<string>&M,int n,int m)
{
int x,y;
int i,j;
for(i=0;i!=n;i++)
for(j=0;j!=m;j++)
if(M[i][j] == 'M')
{
x = i;
y = j;
}
int wand = dfs(M,x,y,n,m); // define
return wand;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int i;
vector<string>M;
for(i=0;i!=n;i++)
{
string x;
cin>>x;
M.push_back(x);
}
int k;
cin>>k;
int K = countWand(M,n,m); // define
if(k == K)
cout<<"Impressed\n";
else
cout<<"Oops!\n";
}
return 0;
}