1 #include<stdio.h>
2 #include<string.h>
3 int map[20][20];
4 int dx[]={1,1,0,-1,-1,-1,0,1};
5 int dy[]={0,1,1, 1, 0,-1,-1,-1};
6 int len=15;
7 int n;
8 bool inside(int x,int y)
9 {
10 return (x>=1&&x<=len&&y>=1&&y<=len);
11 }
12 bool check(int man,int x,int y)
13 {
14 int dir,ex,ey,count=0,i;
15 for(dir=0;dir<4;dir++)
16 {
17 count=0;
18 for(i=1;;i++)
19 {
20 ex=x+i*dx[dir];
21 ey=y+i*dy[dir];
22 if(inside(ex,ey)&&map[ex][ey]==man) count++;
23 else break;
24 }
25 for(i=1;;i++)
26 {
27 ex=x+i*dx[dir+4];
28 ey=y+i*dy[dir+4];
29 if(inside(ex,ey)&&map[ex][ey]==man) count++;
30 else break;
31 }
32 if(count>=4)
33 return true;
34 }
35 return false;
36 }
37 bool canwin(int man,int &x,int &y)
38 {
39 int i,j;
40 for(i=1;i<=len;i++)
41 for(j=1;j<=len;j++)
42 {
43 if(map[i][j]) continue;
44 if(check(man,i,j))
45 {
46 x=i;
47 y=j;
48 return true;
49 }
50 }
51 return false;
52 }
53 bool candefend(int man,int &x,int &y)
54 {
55 int i,j,count=0;
56 for(i=1;i<=len;i++)
57 for(j=1;j<=len;j++)
58 {
59 if(map[i][j]) continue;
60 if(check(3-man,i,j))
61 {
62 count++;
63 if(count>1) return false;
64 x=i;
65 y=j;
66 }
67 }
68 return true;
69 }
70 int main()
71 {
72 int i,j,x,y,c;
73 char color[5][10]={"","white","black"};
74 while(scanf("%d",&n)!=EOF)
75 {
76 if(n==0) break;
77 int black=0,white=0,man;
78 memset(map,0,sizeof(map));
79 for(i=1;i<=n;i++)
80 {
81 scanf("%d%d%d",&x,&y,&c);
82 map[x+1][y+1]=c+1;
83 }
84 for(i=1;i<=len;i++)
85 {
86 for(j=1;j<=len;j++)
87 {
88 if(map[i][j]==1) white++;
89 if(map[i][j]==2) black++;
90 }
91 }
92 if(black==white) man=2;
93 else if(black==white+1) man=1;
94 else
95 {
96 printf("Invalid.\n");
97 continue;
98 }
99 if(canwin(man,x,y))
100 {
101 printf("Place %s at (%d,%d) to win in 1 move.\n",color[man],x-1,y-1);
102 continue;
103 }
104 else
105 {
106 if(!candefend(man,i,j))
107 {
108 printf("Lose in 2 moves.\n");
109 continue;
110 }
111 else
112 {
113 int flag=0;
114 for(i=1;i<=len;i++)
115 {
116 for(j=1;j<=len;j++)
117 {
118 if(map[i][j]) continue;
119 map[i][j]=man;
120 if(!canwin(3-man,x,y)&&!candefend(3-man,x,y))
121 {
122 printf("Place %s at (%d,%d) to win in 3 moves.\n",color[man],i-1,j-1);
123 flag=1;
124 break;
125 }
126 map[i][j]=0;
127 }
128 if(flag) break;
129 }
130 if(flag) continue;
131 printf("Cannot win in 3 moves.\n");
132 }
133 }
134 }
135 return 0;
136 }