HZAU_1208_color_circle(dfs)
来源:互联网 发布:meshlab shader 编程 编辑:程序博客网 时间:2024/06/14 01:14
原题
Problem J: Color Circle
Time Limit: 1 Sec Memory Limit: 1280 MB
Description
There are colorful flowers in the parterre in front of the door of college and form many beautiful patterns. Now, you want to find a circle consist of flowers with same color. What should be done ?
Assuming the flowers arranged as matrix in parterre, indicated by a N*M matrix. Every point in the matrix indicates the color of a flower. We use the same uppercase letter to represent the same kind of color. We think a sequence of points d1, d2, … dk makes up a circle while:
1. Every point is different.
2. k >= 4
3. All points belong to the same color.
4. For 1 <= i <= k-1, di is adjacent to di+1 and dk is adjacent to d1. ( Point x is adjacent to Point y while they have the common edge).
N, M <= 50. Judge if there is a circle in the given matrix.
Input
There are multiply test cases.
In each case, the first line are two integers n and m, the 2nd ~ n+1th lines is the given n*m matrix. Input m characters in per line.
Output
Output your answer as “Yes” or ”No” in one line for each case.
Sample Input
3 3
AAA
ABA
AAA
Sample Output
Yes
题意
对于一张地图,判断能否找到一条路线,长度大于4,相同字母,并且回到原点。思路
找图中的一个环,可以从第一个点进入开始 dfs(深度优先搜索) ,标记已经访问过的点,如果遍历过程中遇到它们,则找到一个环,输出 Yes ,否则继续从未标记点开始继续搜索。如果所有的点标记后未找到环,则输出 No 。
涉及知识及算法
DFS:深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次。
——百度百科
代码实现
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;#define MAXX 110000int n,m;int mapp[55][55]; //存储字符bool vis[55][55]; //记录是否被访问int mv[4][2]= {{-1,0},{1,0},{0,-1},{0,1}}; //上下左右四个方向bool flag; //标记是否有环void dfs(int x,int y,int xa,int ya) // (x,y) 为当前点坐标, (xa,ya)为上一点{ for(int i=0; i<4; i++) // 搜索四个方向 { int xi=x+mv[i][0]; int yi=y+mv[i][1]; if(xi<0||xi>=n||yi<0||yi>=m||mapp[xi][yi]!=mapp[x][y])continue; // 要求搜索的点相同且不越界 if(!vis[xi][yi]) //如果该点未被访问 { vis[xi][yi]=true; //置为已访问 dfs(xi,yi,x,y); if(flag) return; //若有环则跳出 } else { if(xi==xa&&yi==ya)continue; // 忽略上一点,如果还遇到一个已经访问的点,则是一个环 flag=true; return; //跳出 } }}void solve(){ for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(!vis[i][j]) //只访问未标记点 { vis[i][j]=true; dfs(i,j,-1,-1); if(flag) { cout<<"Yes"<<endl; return; } } cout<<"No"<<endl;}int main(){ while(cin>>n>>m) { string c; memset(vis,false,sizeof(vis)); flag=false; for(int i=0; i<n; i++) { cin>>c; for(int j=0; j<m; j++) mapp[i][j]=c[j]; } solve(); } return 0;}/**************************************************************
Problem: 1208
Language: C++
Result: Accepted
Time:1 ms
Memory:1520 kb
****************************************************************/
转载自博主小坏蛋_千千,向他表示感谢。
阅读全文
1 0
- HZAU_1208_color_circle(dfs)
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- DFS
- dfs
- dfs
- dfs
- 匈牙利算法
- 关于windows安装composer
- bzoj1036 [ ZJOI2008 ] --树链剖分
- 源生实现滚动页面显示悬浮导航
- 使用c#数据库连接池
- HZAU_1208_color_circle(dfs)
- bzoj1503 [ NOI2004 ] --treap
- 数据结构实验——串的加密
- bzoj1001 -- 最短路
- bzoj1878 [ SDOI2009 ] --离线+树状数组
- 求 以孩子-兄弟链表表示的树 的度、深度、叶结点和边
- bzoj1854 [ SCOI2010 ] --并查集
- Java面向对象,单例设计模式
- bzoj4518 [ SDOI2016 ] --斜率优化DP