Codeforces 475B Strongly Connected City(思维-所有结点能否相互到达)

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".

Examples

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.

题意:   给你每行每列的方向,问你所有结点是否是连通的(可以互相到达)

思路:  只要外环路成环,所有结点就可以相互到达;因为只要外环路成环了,任意点就可以先走到外环路上,再走到任意点

             判断外环路成环就2种情况:只要满足其中一种就能相互到达了

                      

代码:

     


import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
	   Scanner scan=new Scanner(System.in);
	   int n=scan.nextInt();
	   int m=scan.nextInt();
	   char h[]=new char[25];
	   char v[]=new char[25];
	   String s1=scan.next();
	   String s2=scan.next();
	   h=s1.toCharArray();
	   v=s2.toCharArray();
	   boolean flag=false;
	   if(h[0]=='>'&&v[0]=='^'&&h[n-1]=='<'&&v[m-1]=='v')flag=true;
	   if(h[0]=='<'&&v[0]=='v'&&h[n-1]=='>'&&v[m-1]=='^')flag=true;
	   if(flag) System.out.println("YES");
	   else System.out.println("NO");
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OUC_lkc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值