-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathC.cpp
68 lines (68 loc) · 1.44 KB
/
C.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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 4e4 + 1;
const int M = 501;
int n,m,k,p,G[N][M];
bool ed,flag;
void dfs(int x,int y,int s){
if(ed) return;
if((x > 1 || y > 1) && s == k){
ed = 1;
int i,j;
if(!flag){
for(i = 1;i <= n;i++){
for(j = 1;j <= m;j++){
if(G[i][j]) putchar('*');
else putchar('.');
}
puts("");
}
}
else{
for(j = 1;j <= m;j++){
for(i = 1;i <= n;i++){
if(G[i][j]) putchar('*');
else putchar('.');
}
puts("");
}
}
puts("");
return;
}
if(x == n + 1) return;
else if(y == m + 1){
dfs(x + 1,1,s);
}
else if(++p > 8 * n * m) return;
else{
int temp = 0;
if(G[x - 1][y]) temp += G[x][y + 1] + G[x - 1][y - 1] + G[x - 1][y + 1];
if(G[x + 1][y]) temp += G[x][y - 1] + G[x + 1][y - 1] + G[x + 1][y + 1];
if(G[x][y - 1]) temp += G[x - 1][y] + G[x - 1][y - 1] + G[x + 1][y - 1];
if(G[x][y + 1]) temp += G[x - 1][y + 1] + G[x + 1][y] + G[x + 1][y + 1];
if(s + temp <= k && (x == 1 || y == 1 || G[x - 1][y] || G[x][y - 1])){
G[x][y] = 1;
//printf("%d %d\n",x,y);
dfs(x,y + 1,s + temp);
G[x][y] = 0;
}
dfs(x,y + 1,s);
}
}
int main(){
int i,j,t;
scanf("%d",&t);
while(t--){
p = 0;
flag = 0,ed = 0;
memset(G,0,sizeof(G));
scanf("%d %d %d",&n,&m,&k);
if(n < m) swap(n,m),flag = 1;
dfs(1,1,0);
if(!ed) puts("-1\n");
}
return 0;
}