CF Bayan 2015 Contest Warm Up B题 Strongly Connected City


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组成的网格里面有好多点  为这些点能否到达其他所有的点

n行中  每行中的东西方向都是唯一的 要么向东 要么向西

m列中  每列中的南北方向都是唯一的 要么向北 要么向南

就是有这n行m列确定了每点只能运动的方向

因为给的点很少 所以可以暴力     对每个点进行bfs   看是否遍历到了每个点

做的时候  因为有一块是差不多的  就直接复制的 有个地方忘记改了  第一次交过了  但是大数据没有过。。

刚刚看才知道。。。简直坑啊。。。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>

#define eps 1e-8
#define op operator
#define MOD  10009
#define MAXN  100100
#define INF 0x7fffffff

#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define FOV(i,a,b)  for(int i=a;i>=b;i--)
#define REP(i,a,b)  for(int i=a;i<b;i++)
#define REV(i,a,b)  for(int i=a-1;i>=b;i--)
#define MEM(a,x)    memset(a,x,sizeof a)
#define ll __int64

using namespace std;

char we[30];
char ns[30];
int dir[500][4];

bool vis[30][30];

struct point
{
    int x,y;
};
int n,m;

point p,q;
queue<point> Q;

bool bfs(int x,int y)
{
//cout<<x<<"  "<<y<<endl;
    MEM(vis,0);
    vis[x][y]=1;
    p.x=x; p.y=y;
    Q.push(p);
    while(!Q.empty())
    {
        q=Q.front(); Q.pop();
        int xx=q.x*m+q.y;
        if(dir[xx][0])
        {
            p.x=q.x-1; p.y=q.y;
            if(p.x>=0&&p.x<n&&!vis[p.x][p.y])
            {
//            cout<<"点:  "<<p.x<<"   "<<p.y<<endl;
                vis[p.x][p.y]=1;
                Q.push(p);
            }
        }
        if(dir[xx][2])
        {
            p.x=q.x+1; p.y=q.y;
//            cout<<"点:  "<<p.x<<"   "<<p.y<<endl;
            if(p.x>=0&&p.x<n&&!vis[p.x][p.y])
            {
//                cout<<"点:  "<<p.x<<"   "<<p.y<<endl;
                vis[p.x][p.y]=1;
                Q.push(p);
            }
        }
        if(dir[xx][1])
        {
            p.x=q.x; p.y=q.y+1;
//            cout<<"点:  "<<p.x<<"   "<<p.y<<endl;
            if(p.y>=0&&p.y<m&&!vis[p.x][p.y])
            {
//                cout<<"点:  "<<p.x<<"   "<<p.y<<endl;
                vis[p.x][p.y]=1;
                Q.push(p);
            }
        }
        if(dir[xx][3])
        {
            p.x=q.x; p.y=q.y-1;

            if(p.y>=0&&p.y<m&&!vis[p.x][p.y])
            {
//                cout<<"点:  "<<p.x<<"   "<<p.y<<endl;
                vis[p.x][p.y]=1;
                Q.push(p);
            }
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
    {
        if(vis[i][j]==0)
        {
//            cout<<"不到的点: "<<i<<"  "<<j<<endl;
            return 0;
        }
    }
    return 1;
}


int main()
{
//freopen("ceshi.txt","r",stdin);

    while(scanf("%d %d",&n,&m)!=EOF)
    {
        getchar();
        scanf("%s",we);
        scanf("%s",ns);
        MEM(dir,0);
        for(int i=0;i<n;i++)
        {
            if(we[i]=='>')
            {
                for(int j=i*m;j<(i+1)*m;j++)
                    dir[j][1]=1;
            }
            if(we[i]=='<')
            {
                for(int j=i*m;j<(i+1)*m;j++)
                    dir[j][3]=1;
            }
        }
        for(int i=0;i<m;i++)
        {
            if(ns[i]=='^')
            {
                for(int j=i;j<n*m;j+=m)
                    dir[j][0]=1;
            }
            if(ns[i]=='v')
            {
                for(int j=i;j<n*m;j+=m)
                    dir[j][2]=1;
            }
        }
        int f=1;
        for(int i=0;i<n&&f;i++)
            for(int j=0;j<m;j++)
            {
                bool flag=bfs(i,j);
                if(flag==0)
                {
                    puts("NO");
                    f=0;
                    break;
                }
            }
        if(f==1)
            puts("YES");
    }
    return 0;
}



  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值