CF 510B(Fox And Two Dots-图上找环)
2015-02-03 14:59
447 查看
B. Fox And Two Dots
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:
![](http://img.blog.csdn.net/20150203145623708?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmlrZTBnb29k/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if
and only if it meets the following condition:
These k dots are different: if i ≠ j then di is
different from dj.
k is at least 4.
All dots belong to the same color.
For all 1 ≤ i ≤ k - 1: di and di + 1 are
adjacent. Also, dk and d1 should
also be adjacent. Cells x and y are called adjacent
if they share an edge.
Determine if there exists a cycle on the field.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 50):
the number of rows and columns of the board.
Then n lines follow, each line contains a string consisting of m characters,
expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output
Output "Yes" if there exists a cycle, and "No"
otherwise.
Sample test(s)
input
output
input
output
input
output
input
output
input
output
Note
In first sample test all 'A' form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above ('Y' = Yellow, 'B'
= Blue, 'R' = Red).
对于每个‘颜色连通块’当成树dfs(不走父边),观察是否有走向已走过点的边
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if
and only if it meets the following condition:
These k dots are different: if i ≠ j then di is
different from dj.
k is at least 4.
All dots belong to the same color.
For all 1 ≤ i ≤ k - 1: di and di + 1 are
adjacent. Also, dk and d1 should
also be adjacent. Cells x and y are called adjacent
if they share an edge.
Determine if there exists a cycle on the field.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 50):
the number of rows and columns of the board.
Then n lines follow, each line contains a string consisting of m characters,
expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output
Output "Yes" if there exists a cycle, and "No"
otherwise.
Sample test(s)
input
3 4 AAAA ABCA AAAA
output
Yes
input
3 4 AAAA ABCA AADA
output
No
input
4 4 YYYR BYBY BBBY BBBY
output
Yes
input
7 6 AAAAAB ABBBAB ABAAAB ABABBB ABAAAB ABBBAB AAAAAB
output
Yes
input
2 13 ABCDEFGHIJKLM NOPQRSTUVWXYZ
output
No
Note
In first sample test all 'A' form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above ('Y' = Yellow, 'B'
= Blue, 'R' = Red).
对于每个‘颜色连通块’当成树dfs(不走父边),观察是否有走向已走过点的边
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (50+10) long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} typedef long long ll; int n,m; char a[MAXN][MAXN]; bool b[MAXN][MAXN]={0}; bool dfs(int i,int j,int t) { if (b[i][j]==1) return 1; b[i][j]=1; if (a[i][j]==a[i-1][j]&&t!=2) if (dfs(i-1,j,1)) return 1; if (a[i][j]==a[i+1][j]&&t!=1) if (dfs(i+1,j,2)) return 1; if (a[i][j]==a[i][j-1]&&t!=4) if (dfs(i,j-1,3)) return 1; if (a[i][j]==a[i][j+1]&&t!=3) if (dfs(i,j+1,4)) return 1; return 0; } int main() { // freopen("Dots.in","r",stdin); // freopen(".out","w",stdout); MEM(a) cin>>n>>m; For(i,n) scanf("%s",a[i]+1); For(i,n) For(j,m) { if (!b[i][j]) { if (dfs(i,j,0)) { cout<<"Yes"<<endl; return 0; } } } cout<<"No"<<endl; return 0; }
相关文章推荐
- 【CodeForces】510B - Fox And Two Dots(bfs)
- 【CodeForces 510B】Fox And Two Dots
- 【codeforce-510B】fox and two dots
- 【codeforces 510B】Fox And Two Dots
- DFS CodeForces 510B (Fox And Two Dots)
- CF B. Fox And Two Dots
- CF Fox And Two Dots (DFS)
- codeforces-510B-Fox And Two Dots【DFS】
- 【Codeforces】-510B-Fox And Two Dots(dfs)
- codeforce-B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
- Fox And Two Dots(DFS)
- D - Fox And Two Dots (codeforces 510B)
- B. Fox And Two Dots( Codeforces Round #290 (Div. 2))
- Fox And Two Dots
- codeforces-Fox And Two Dots【DFS】(思维)
- codeforces#290 B&&510 B Fox And Two Dots(简单dfs)
- Fox And Two Dots
- codeforces510--A. Fox And Snake--B. Fox And Two Dots
- B. Fox And Two Dots———简单DFS