还是没写好,,看的别人的
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <stack> 8 #include <cctype> 9 #include <string> 10 #include <malloc.h> 11 #include <queue> 12 #include <map> 13 14 using namespace std; 15 const int INF = 0xffffff; 16 const double Pi = 4 * atan(1); 17 const int Maxn = 200 + 10; 18 int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}}; 19 int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; 20 int m,n; 21 int graph[Maxn][Maxn]; 22 char alpha[] = "ADJKSW"; 23 int con[6]; 24 int dic[6][2] = {{1,0},{3,2},{5,1},{4,4},{0,5},{2,3}}; 25 int num[16][4] = {{0,0,0,0},{0,0,0,1},{0,0,1,0},{0,0,1,1}, 26 {0,1,0,0},{0,1,0,1},{0,1,1,0},{0,1,1,1}, 27 {1,0,0,0},{1,0,0,1},{1,0,1,0},{1,0,1,1}, 28 {1,1,0,0},{1,1,0,1},{1,1,1,0},{1,1,1,1}}; 29 int cnt; 30 31 void dfsZero(int r,int c){ 32 if(r < 0 || r >= n || c < 0 || c >= m || graph[r][c] != 0) 33 return; 34 graph[r][c] = -1; 35 for(int i = 0;i < 4;i++){ 36 int xx = dir[i][0] + r; 37 int yy = dir[i][1] + c; 38 dfsZero(xx,yy); 39 } 40 } 41 42 void dfs(int r,int c){ 43 if(r < 0 || r >= n || c < 0 || c >= m || graph[r][c] == -1) 44 return; 45 if(graph[r][c] == 0){ 46 cnt++; 47 dfsZero(r,c); 48 return; 49 } 50 graph[r][c] = -1; 51 for(int i = 0;i < 4;i++){ 52 int xx = dir[i][0] + r; 53 int yy = dir[i][1] + c; 54 dfs(xx,yy); 55 } 56 } 57 58 int main() 59 { 60 #ifndef ONLINE_JUDGE 61 freopen("inpt.txt","r",stdin); 62 #endif 63 int cas = 0; 64 while(cin >> n >> m && n && m){ 65 memset(graph,0,sizeof(graph)); 66 memset(con,0,sizeof(con)); 67 char str[Maxn]; 68 for(int i = 0;i < n;i++){ 69 cin >> str; 70 int len = 0; 71 for(int j = 0;j < m;j++){ 72 if(str[j] == ‘0‘){ 73 for(int k = 0;k < 4;k++) 74 graph[i][len++] = 0; 75 continue; 76 } 77 int tmp; 78 if(isalpha(str[j])) 79 tmp = str[j] - ‘a‘ + 10; 80 else 81 tmp = str[j] - ‘0‘; 82 for(int k = 0;k < 4;k++){ 83 graph[i][len++] = num[tmp][k]; 84 } 85 } 86 } 87 m *= 4; 88 for(int i = 0;i < n;i++){ 89 if(graph[i][0] == 0) 90 dfsZero(i,0); 91 if(graph[i][m-1] == 0) 92 dfsZero(i,m-1); 93 } 94 for(int j = 0;j < m;j++){ 95 if(graph[0][j] == 0) 96 dfsZero(0,j); 97 if(graph[n-1][j] == 0) 98 dfsZero(n-1,j); 99 } 100 for(int i = 0;i < n;i++){ 101 for(int j = 0;j < m;j++){ 102 if(graph[i][j] == 1){ 103 cnt = 0; 104 dfs(i,j); 105 for(int k = 0;k < 6;k++){ 106 if(cnt == dic[k][0]){ 107 con[ dic[k][1] ]++; 108 break; 109 } 110 } 111 } 112 } 113 } 114 cout << "Case " << ++cas << ": "; 115 for(int i = 0;i < 6;i++){ 116 for(int j = 0;j < con[i];j++){ 117 cout << alpha[i]; 118 } 119 } 120 cout << endl; 121 } 122 return 0; 123 }
原文:http://www.cnblogs.com/yijiull/p/7138950.html