暴力枚举即可(也可用dp实现)~

View Code
1 #include <cstdio>
2 #include <cstring>
3
4  using namespace std;
5
6  const int SIZE = 64;
7 const int TYPE = 26;
8
9 const int dx[] = {-1,0,1,0};
10 const int dy[] = {0,1,0,-1};
11 const int mx[] = {1,1,-1,-1};
12 const int my[] = {1,-1,-1,1};
13
14 const int ddx[] = {-1,1,1,-1};
15 const int ddy[] = {1,1,-1,-1};
16 const int mmx[] = {1,0,-1,0};
17 const int mmy[] = {0,-1,0,1};
18
19 char board[SIZE][SIZE];
20
21 bool visited[TYPE];
22 int count[TYPE];
23
24 int nn;
25
26 inline bool legal(int x,int y)
27 {
28 return x >= 0 && x < nn && y >= 0 && y < nn;
29 }
30
31 void work(int x,int y,char ch)
32 {
33 visited[ch-'A'] = true;
34 for (int i = 0;i < 4;i++)
35 {
36 int j = (i + 1) % 4;
37 for (int k = 1;;k++)
38 {
39 int sx = x + k * dx[i];
40 int sy = y + k * dy[i];
41
42 int ex = x + k * dx[j];
43 int ey = y + k * dy[j];
44
45 if (!legal(sx,sy) || !legal(ex,ey)) break;
46 if (board[sx][sy] != ch || board[ex][ey] != ch) break;
47 while (sx != ex || sy != ey)
48 {
49 sx += mx[i];
50 sy += my[i];
51 if (!legal(sx,sy) || board[sx][sy] != ch) break;
52 }
53 if (sx == ex && sy == ey) count[ch-'A']++;
54 else break;
55 }
56 }
57
58 for (int i = 0;i < 4;i++)
59 {
60 int j = (i + 1) % 4;
61 for (int k = 1;;k++)
62 {
63 int sx = x + k * ddx[i];
64 int sy = y + k * ddy[i];
65
66 int ex = x + k * ddx[j];
67 int ey = y + k * ddy[j];
68
69 if (!legal(sx,sy) || !legal(ex,ey)) break;
70 if (board[sx][sy] != ch || board[ex][ey] != ch) break;
71
72 while (sx != ex || sy != ey)
73 {
74 sx += mmx[i];
75 sy += mmy[i];
76 if (!legal(sx,sy) || board[sx][sy] != ch) break;
77 }
78 if (sx == ex && sy == ey) count[ch-'A']++;
79 else break;
80 }
81 }
82 }
83
84 int main()
85 {
86 int cas;
87
88 scanf("%d",&cas);
89
90 for (int cc = 1;cc <= cas;cc++)
91 {
92 memset(count,0,sizeof(count));
93 memset(visited,false,sizeof(visited));
94
95 scanf("%d",&nn);
96 for (int i = 0;i < nn;i++)
97 scanf("%s",board[i]);
98
99 for (int i = 0;i < nn;i++)
100 for (int j = 0;j < nn;j++)
101 work(i,j,board[i][j]);
102
103 printf("Case %d:\n",cc);
104 for (int i = 0;i < TYPE;i++)
105 if (visited[i])
106 printf("%c %d\n",'A' + i,count[i]);
107 }
108 return 0;
109 }