-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchef_and_tic_tac_toe.cpp
120 lines (119 loc) · 1.7 KB
/
chef_and_tic_tac_toe.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
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<bits/stdc++.h>
using namespace std;
bool check_row(vector<string> ,int i, int n, int k)
{
int j,count=0;
for(j=0;j<n;j++)
{
if(a[i][j]=='X')
{
count++;
if(count>=k)
return true;
if((j+1)<n && (count==(k-1) && a[i][j+1]=='.'))
return true;
}
else
{
count=0;
}
}
return false;
}
bool check_col(vector<string> ,int j, int n, int k)
{
int i,count=0;
for(i=0;i<n;i++)
{
if(a[i][j]=='X')
{
count++;
if(count>=k)
return true;
if((i+1<n) && (count==(k-1) && a[i+1][j]=='.'))
return true;
}
else
count=0;
}
return false;
}
bool check_diag(vector<string> , int i, int j, int n, int k)
{
for(i=0;i<n;)
{
for(j=0;j<n;)
{
if(a[i][j]=='X')
{
count++;
if(count>=k)
return true;
if((((i+1<n && j+1< n)&&(a[i+1][j+1]=='.'))||((i-1>0 && j-1>0)&&(a[i-1][j-1]=='.')))&&(count==(k-1)))
return true;
}
else
{
count=0;
}
i++;
j++;
}
}
count=0;
for(i=(n-1);i>0;)
{
for(j=0;j<n;)
{
if(a[i][j]=='X')
{
count++;
if(count>=k)
return true;
if((((i+1<n && j-1>0)&&(a[i+1][j-1]=='.'))||((i-1>0 && j+1<n)&&(a[i-1][j+1]=='.')))&&(count==(k-1)))
return true;
}
else
{
count=0;
}
j++;
i--;
}
}
int main()
{
ios_base::sync_with_stdio(false);
int t,n,k;
vector<string> a;
cin >> t;
while(t--)
{
cin >> n >> k;
a.resize(n);
for(j=0;j<n;j++)
{
cin >> a[j];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]=='.')
{
if(check_row(a,i,n,k) || check_col(a,j,n,k) || check_diag(a,i,j,n,k))
{
flag=1;
cout << "YES\n";
break;
}
}
}
if(flag==1)
break;
}
if(flag==0)
cout << "NO\n";
}
return 0;
}