Skip to main content
  1. Posts/

codeforces 510 B. Fox And Two Dots

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/510/problem/B 题意:给定一个maze,用不同的字母代表不同的颜色。问能否找到一个颜色相同的环(失少四个点组成) 思路:dfs一遍,如果遇到之前已经访问过的点,说明成环。需要注意的是,要注意由一个点向某方向移动,然后由反方向移动到该点所造成的误判。所以dfs除了要知道当前的坐标x,y,还要记录之前的坐标px,py.

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月05日 星期六 21时35分07秒
  4File Name :code/cf/problem/510B.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25
 26
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=55;
 34int n,m;
 35char maze[N][N];
 36bool vis[N][N];
 37bool flag;
 38
 39
 40bool inmaze( int x,int y)
 41{
 42    if (x>=0&&x<n&&y>=0&&y<m) return true;
 43    return false;
 44}
 45void dfs( int x,int y,int px,int py)  //要记录当前的x,y是由哪里来的。把因为由px,py到x,y再回到px,py引起的误判剔除。
 46{					//判cycle方式为:到达一个之前已经到达过的点。
 47    vis[x][y] = true;
 48   // cout<<"x:"<<x<<" y:"<<y<<" cur:"<<cur<<endl;
 49    if (flag) return;
 50    for ( int i = 0 ; i < 4 ; i++)
 51    {
 52	int nx = x + dx4[i] ;
 53	int ny = y + dy4[i] ;
 54	if (nx==px&&ny==py) continue;
 55	if (inmaze(nx,ny)&&maze[nx][ny]==maze[x][y])
 56	{
 57	    if (!vis[nx][ny])
 58	    {
 59		dfs(nx,ny,x,y);
 60	    }
 61	    else
 62	    {
 63		flag = true;
 64		return ;
 65	    }
 66	}
 67    }
 68
 69}
 70int main()
 71{
 72	#ifndef  ONLINE_JUDGE
 73	freopen("code/in.txt","r",stdin);
 74  #endif
 75
 76	scanf("%d %d",&n,&m);
 77	for ( int i = 0 ; i < n ; i++) scanf("%s",maze[i]);
 78	ms(vis,false);
 79	flag = false;
 80	for ( int i = 0 ; i < n ;i++)
 81	{
 82	    if (flag) break;
 83	    for ( int j = 0 ; j < m ; j++)
 84	    {
 85		if (flag) break;
 86		if (!vis[i][j])
 87		{
 88		    dfs(i,j,-1,-1);
 89		}
 90	    }
 91	}
 92	if (flag)
 93	{
 94	    puts("Yes");
 95	}
 96	else
 97	{
 98	    puts("No");
 99	}
100
101  #ifndef ONLINE_JUDGE
102  fclose(stdin);
103  #endif
104    return 0;
105}

Related

codeforces 277 A. Learning Languages

·2 mins
http://codeforces.com/contest/277/problem/A 题意:有n个人,每个人会一定数目的语言(可能为0),一个人学一门语言的代价为1,人和人之间沟通可以通过任意个中间人翻译。问最少的代价使得这n个人可以相互沟通。

codeforces 217A ice skating

·1 min
http://codeforces.com/problemset/problem/217/A 题意:有n个雪漂(那是啥?,从某个雪漂出发走直线,只有到达另一个雪飘才能停下来。问最少需要添加多少个雪漂,才能使得可以到达任何一个雪漂。 思路:横坐标相同或者纵坐标相同的两个点之间是可以到达的。先O(N2)扫一遍建图。记录这个森林中数的个数为cnt,cnt-1即为答案。因为对于任意两个不能相互到达的点。我们只需要再来一个雪漂就可以使得这两个点相互到达。

codeforces 445 B. DZY Loves Chemistry

·2 mins
http://codeforces.com/contest/445/problem/B 题意:一共有n种化学药品。m对关系,每对关系表示为x,y表示x和y相互反应。初始容器的danger值为1,当向容器中加入一个化学药品A,如果容器中存在化学药品和A反应,那么容器的danger值翻倍。否则不变。问一个最优的放置药品的顺序。

codeforces 522 A. Reposts

·1 min
http://codeforces.com/problemset/problem/522/A 题意:给定某条消息的传播路径。问最远传播的距离。。 思路:其实就是问树的深度。。直接dfs就行了。。

codeforces 377 A maze

·2 mins
http://codeforces.com/contest/377/problem/A 题意:给定一个n*m的maze. ‘.’代表空,‘#’代表墙。要求构造一种方案,使得将k个空格填成墙壁后不影响当前的连通性(即没有被填充的空格之间可以相互到达) 思路:一开始想从上往下从左往右构造。错误的认为四个角一定是可以变成墙的。