Codeforces Round #290 (Div. 2) B (dfs)

题目链接:http://codeforces.com/problemset/problem/510/B

题意:判断图中是否有某个字母成环

思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相同且已搜过,则存在满足题意的环

代码:

 1 #include <bits/stdc++.h>
 2 #define MAXN 60
 3 using namespace std;
 4
 5 int mp[MAXN][MAXN], vis[MAXN][MAXN], m, n;
 6 int dir[4][2]={0, 1, 1, 0, -1, 0, 0, -1};
 7 bool flag=false;
 8
 9 void dfs(int x, int y, int direction){
10     if(flag){
11         return;
12     }
13     for(int i=0; i<4; i++){
14         int xx=x+dir[i][0];
15         int yy=y+dir[i][1];
16         if(xx>=0&&xx<n&&yy>=0&&yy<m){
17             if(i+direction!=3&&vis[xx][yy]&&mp[xx][yy]==mp[x][y]){ //***若下一个数字与当前数字相同且已经搜过,则存在满足题意的环,注意别往回的方向搜了
18                 flag=true;
19                 return;
20             }else if(!vis[xx][yy]&&mp[xx][yy]==mp[x][y]){
21                 vis[xx][yy]=1;
22                 dfs(xx, yy, i);
23             }
24         }
25     }
26 }
27
28 int main(void){
29     char ch;
30     cin >> n >> m;
31     for(int i=0; i<n; i++){
32         for(int j=0; j<m; j++){
33             cin >> ch;
34             mp[i][j]=ch-‘A‘+1;
35         }
36     }
37     for(int i=0; i<n; i++){
38         for(int j=0; j<m; j++){
39             if(!vis[i][j]){
40                 vis[i][j]=1;
41                 dfs(i, j, 0);
42                 if(flag){
43                     cout << "Yes" << endl;
44                     return 0;
45                 }
46             }
47         }
48     }
49     cout << "No" << endl;
50     return 0;
51 }
时间: 2024-10-21 11:09:31

Codeforces Round #290 (Div. 2) B (dfs)的相关文章

Codeforces Round #290 (Div. 2) b

/** * @brief Codeforces Round #290 (Div. 2) b * @file a.cpp * @author mianma * @created 2015/02/04 15:17 * @edited 2015/02/04 15:17 * @type brute * @note */ #include <fstream> #include <iostream> #include <string> #include <cstring>

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

Codeforces Round #290 (Div. 2) 解题报告 A.B.C.D.

A - Fox And Snake 模拟. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h> using na

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

Codeforces Round #290 (Div. 2)

A题: 简单的模拟. 贴样例就知道了. input 3 3 output ###..#### input 3 4 output ####...##### input 5 3 output ###..#####..### input 9 9 output #########........###########........#########........###########........######### 1 #include<cstdio> 2 int main() 3 { 4 in

Codeforces Round #290 (Div. 2) 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

B. Fox And Two Dots Codeforces Round #290 (Div. 2)

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 ce

C. Fox And Names Codeforces Round #290 (Div. 2)

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK