B. Fox And Two Dots Codeforces Round #290 (Div. 2)
来源:互联网 发布:北京淘宝视觉营销 编辑:程序博客网 时间:2024/05/18 00:13
题意:在n*m的地图上,每个格子涂有不同的颜色(A~Z),问是否有某一种颜色能组成一个回路且长度至少为4.
思路:我用的bfs,从一个未被访问的点开始bfs,走同一种颜色的格子,同时不能走回头路,如果存在两条路最后能碰头并且长度之和大于4就输出Yes,否则No。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 55using namespace std;struct Node{ int x,y,step,d;};int dir[4][2]={-1,0,0,1,1,0,0,-1};char mp[maxn][maxn];int step[maxn][maxn];bool vis[maxn][maxn];int n,m;bool isok(int x,int y){ if (x>=0&&x<n&&y>=0&&y<m) return true; return false;}bool bfs(int x,int y){ Node st,now; queue<Node>Q; while (!Q.empty()) Q.pop(); st.x=x;st.y=y;st.step=0;st.d=-1; Q.push(st); step[x][y]=0; vis[x][y]=true; while (!Q.empty()) { st=Q.front(); Q.pop(); for (int i=0;i<4;i++) { if (st.d==(i+2)%4) continue; //不能走回头路 now.x=st.x+dir[i][0]; now.y=st.y+dir[i][1]; now.step=st.step+1; now.d=i; //记录当前点走到下一个点是从哪个方向过去的 if (isok(now.x,now.y)&&mp[st.x][st.y]==mp[now.x][now.y]) { if (vis[now.x][now.y]&&(now.step+step[now.x][now.y]+1)>=4) //碰头了并且长度之和大于4 return true; if (!vis[now.x][now.y]) //没有被访问过就入队列 { vis[now.x][now.y]=true; step[now.x][now.y]=now.step; Q.push(now); } } } } return false;}bool solve(){ for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (!vis[i][j]&&bfs(i,j)) return true; return false;}int main(){ while (~scanf("%d%d",&n,&m)) { for (int i=0;i<n;i++) scanf("%s",mp[i]); memset(vis,false,sizeof(vis)); memset(step,0,sizeof(step)); if (solve()) printf("Yes\n"); else printf("No\n"); } return 0;}
3 0
- Codeforces Round #290(Div.2) B.Fox And Two Dots
- B. Fox And Two Dots( Codeforces Round #290 (Div. 2))
- B. Fox And Two Dots Codeforces Round #290 (Div. 2)
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
- Fox And Two Dots (Codeforces Round #290 (Div. 2)B)
- Codeforces Round #290 (Div. 2)B - Fox And Two Dots——并查集/搜索
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
- Codeforces Round #290 (Div. 2)B. Fox And Two Dots(dfs)
- #290 (div.2) B. Fox And Two Dots
- CodeForces 510B Fox And Two Dots
- 【CodeForces 510B】Fox And Two Dots
- Codeforces 510B. Fox And Two Dots
- codeforces B. Fox And Two Dots (dfs)
- codeforces 510b Fox And Two Dots
- TCP/IP 详解卷一 - 路由协议
- 算法导论 13-4 Treap树
- [笔记]socket recv()函数返回0的一种情况
- css 制线条选项卡 选中状态
- opencv在linux下的安装杂谈
- B. Fox And Two Dots Codeforces Round #290 (Div. 2)
- iOS APP可执行文件的组成
- 关于 Free 的 buffers / cached
- 基于Ajax的长连接
- LDAP 中DN、CN、DC的含义
- mysql编码问题
- Mac Yosemite 配置Apache
- merge用法(ORACLE 对比 TERADATA)
- Python基础学习总结(一)