标签:
Ignatius.L
比较有意思的模拟题,对于一个方块,用四个bool变量标记方块四个边上是否能连通管道,检查与相邻的方块是否匹配。
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 const int MAX = 60; 5 struct Square{ 6 int p[4];//西北东南 7 }; 8 const Square s[‘K‘ - ‘A‘ + 1] = { { 1, 1, 0, 0 }, { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 0, 0, 1, 1 }, 9 { 0, 1, 0, 1 }, { 1, 0, 1, 0 }, { 1, 1, 1, 0 }, { 1, 1, 0, 1 }, 10 { 1, 0, 1, 1 }, { 0, 1, 1, 1 }, { 1, 1, 1, 1 } }; 11 char farm[MAX][MAX]; 12 bool t[MAX][MAX]; 13 int f[MAX*MAX]; 14 15 int sf(int x) 16 { 17 return x == f[x] ? x : f[x] = sf(f[x]); 18 } 19 int m, n; 20 21 int dx[4] = {0,1, 0,-1}; 22 int dy[4] = {1,0,-1, 0}; 23 void merge(int x, int y) 24 { 25 for (int i = 0, j = 2; i < 4; i++, j++, j %= 4) 26 { 27 int nx = x + dx[i]; 28 int ny = y + dy[i]; 29 if (0 <= nx && nx < m && 0 <= ny && ny < n && s[farm[nx][ny] - ‘A‘].p[i] 30 && s[farm[x][y]-‘A‘].p[j]) //可以吻合 31 { 32 f[sf(nx*n + ny)] = sf(x*n + y); 33 } 34 } 35 } 36 37 int main() 38 { 39 int ans; 40 while (scanf("%d%d", &m, &n) == 2 && n>0 && m>0) 41 { 42 ans = 0; 43 memset(t, 0, sizeof(t)); 44 for (int i = 0; i<m; ++i) 45 scanf("%s", farm[i]); 46 47 for (int i = 0, k = 0; i<m; ++i) 48 for (int j = 0; j<n; ++j, ++k) 49 f[i*n + j] = k; 50 51 for (int i = 0; i<m; ++i) 52 for (int j = 0; j<n; ++j) 53 merge(i, j); 54 for (int i = 0; i<n*m; i++) 55 { 56 if (i == f[i]) 57 ans++; 58 } 59 /* 60 for (int i = 0; i<n*m; i++) 61 { 62 printf("%d ", f[i]); 63 if ((i + 1) % n == 0) printf("\n"); 64 } 65 */ 66 printf("%d\n", ans); 67 } 68 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5169937.html