CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Imagine a city with n horizontal streets crossing m vertical streets, forming an (n - 1) × (m - 1) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traffic moves only from north to south or only from south to north in each vertical street. It is possible to enter a horizontal street from a vertical street, or vice versa, at their intersection.

The mayor has received some street direction patterns. Your task is to check whether it is possible to reach any junction from any other junction in the proposed street direction pattern.

Input

The first line of input contains two integers n and m, (2 ≤ n, m ≤ 20), denoting the number of horizontal streets and the number of vertical streets.

The second line contains a string of length n, made of characters '<' and '>', denoting direction of each horizontal street. If the i-th character is equal to '<', the street is directed from east to west otherwise, the street is directed from west to east. Streets are listed in order from north to south.

The third line contains a string of length m, made of characters '^' and 'v', denoting direction of each vertical street. If the i-th character is equal to '^', the street is directed from south to north, otherwise the street is directed from north to south. Streets are listed in order from west to east.

Output

If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".

Sample test(s)
Input
3 3
><>
v^v
Output
NO
Input
4 6
<><>
v^v^v^
Output
YES
Note

The figure above shows street directions in the second sample test case.




解题思路:

        题意是说给定n行m列,然后输入两行字符串,> 、< 、v 、^ 分别代表右、左、下、上四个方向,问构成的图是否能保证每个点到另外所有点都连通,能的话输出YES,否则输出NO。

        对每一个点进行暴力dfs,判断是否能到其他各点。



完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
string a , b;
 int n , m;
int vis[1111][1111];
int cnt;
void dfs(int x , int y)
{
    if(x < 0 || x >= n || y < 0 || y >= m || vis[x][y])
        return ;
    vis[x][y] = 1;
    cnt++;
    if(a[x] == '>')
        dfs(x , y + 1);
    else if(a[x] == '<')
        dfs(x , y - 1);
    if(b[y] == '^')
        dfs(x - 1 , y);
    else if(b[y] == 'v')
        dfs(x + 1 , y);
}

bool check()
{
    for(int i = 0 ; i < n ; i ++)
    {
        for(int j =  0 ; j  < m ; j++)
        {
            memset(vis , 0 , sizeof(vis));
            cnt = 0;
            dfs(i , j);
            if(cnt != m * n)
                return false;
        }
    }
    return true;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif

    while(~scanf("%d%d",&n,&m))
    {
        cin >> a >> b;
        if(check())
            printf("YES\n");
        else
            printf("NO\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值