题目链接:1416 两点
并查集随便搞一下就过了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2505; 4 int n, m; 5 int fa[maxn]; 6 char s[55][55]; 7 //int dx[]={1, 1, 0, 0} 8 int ety(int x, int y) { 9 return x*m+y; 10 } 11 void init(int n) { 12 for(int i=0; i<=n; i++) { 13 fa[i] = i; 14 } 15 } 16 int get(int x) { 17 return fa[x] == x ? x : fa[x] = get(fa[x]); 18 } 19 bool join(int x, int y) { 20 int rx = get(x), ry = get(y); 21 if(rx == ry) return false; 22 fa[rx] = fa[ry] = min(rx, ry); 23 return true; 24 } 25 int main() { 26 27 while(~scanf("%d%d", &n, &m)) { 28 getchar(); 29 for(int i=0; i<n; i++) { 30 gets(s[i]); 31 } 32 init(n*m); 33 bool ok = true; 34 for(int i=0; i<n; i++) { 35 for(int j=0; j<m; j++) { 36 if(i!=n-1 && s[i][j] == s[i+1][j]) ok = join(ety(i, j), ety(i+1, j)); 37 if(!ok) break; 38 if(j!=m-1 && s[i][j] == s[i][j+1]) ok = join(ety(i, j), ety(i, j+1)); 39 if(!ok) break; 40 } 41 if(!ok) break; 42 } 43 if(ok) printf("No\n"); 44 else printf("Yes\n"); 45 } 46 return 0; 47 }
时间: 2024-10-08 06:38:50