标签:des blog http io os ar java for strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5989 Accepted Submission(s): 2589
#include <cstdio> #include <cstring> #include <queue> #include <set> #include <cmath> using namespace std; const int N=55; int n,m; int f[N*N]; char maze[N][N]; int t[12][4] ={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1}, {0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1}, {1,0,1,1},{0,1,1,1},{1,1,1,1},{0,0,0,0}}; int find(int x) { if(f[x] == x) return x; else return f[x] = find(f[x]); } set<int> st; void init() { for(int i = 1; i <= n*m; i++) f[i] = i; memset(maze, ‘A‘+11, sizeof(maze)); st.clear(); } int fun(int i, int j) { return (i-1)*m + j; } void Union(int x, int y) { x = find(x); y = find(y); if(x != y) f[x] = y; } void judge(int i, int j) { char now = maze[i][j]; char up = maze[i-1][j]; char left = maze[i][j-1]; if(t[now-‘A‘][1] == 1 && t[up-‘A‘][3] == 1) { int a = fun(i,j); int b = fun(i-1,j); Union(a,b); } if(t[now-‘A‘][0] == 1 && t[left-‘A‘][2] == 1) { int a = fun(i,j); int b = fun(i,j-1); Union(a,b); } } int main(){ while(scanf("%d %d", &n, &m)){ if(n == -1 && m == -1) break; init(); for(int i = 1; i <= n; i++) scanf("%s", maze[i]+1); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++){ judge(i,j); } for(int i = 1; i <= n*m; i++) st.insert(find(i)); printf("%d\n", st.size()); } return 0; }
标签:des blog http io os ar java for strong
原文地址:http://www.cnblogs.com/brokesb/p/4046915.html