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.
计算强连通性,利用Floyd算法
存储每一行每一列的方向,以便更新地图数组
接下来就要根据方向来书写地图
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int n,m;
static int[] hang=new int[22];
static int[] lie=new int[22];
static int[][] a=new int[420][420];
static int inf=99999999;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
String s=sc.next();
String s1=sc.next();
for(int i=0;i<n;i++)
hang[i]=s.charAt(i);//存储行方向
for(int i=0;i<m;i++)
lie[i]=s1.charAt(i);//存储列方向
for(int i=0;i<420;i++)
Arrays.fill(a[i],inf);//初始化地图
for(int i=1;i<=n*m;i++)
a[i][i]=0;//自己到自己为0
int from,to;
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14
对每一个点如此的编号,观察相邻两点的连通性
最后通过Floyd算法求取任意两点之间的连通性
*/
for(int i=1;i<=n;i++){
from=1+(i-1)*m;//每一行的起始点
if(hang[i-1]=='>'){//向右移动,观察每一行的连通性from-》to
to=from+1;
for(int k=1;k<m;k++){
a[from][to]=1;//更新地图
from=to;
to=from+1;//向右移动,观察每一行的连通性
}
}else if(hang[i-1]=='<'){
to=from+1;
for(int k=1;k<m;k++){
a[to][from]=1;
from=to;
to=from+1;
}
}
}
for(int i=1;i<=m;i++){
from=i;//每一列的起始点
if(lie[i-1]=='v'){//向下移动,观察每一列的联通性
to=from+m;
for(int k=1;k<n;k++){
a[from][to]=1;
from=to;
to=from+m;//向下移动,观察每一列的联通性
}
}else if(lie[i-1]=='^'){
to=from+m;
for(int k=1;k<n;k++){
a[to][from]=1;
from=to;
to=from+m;
}
}
}
//Floyd算法更新地图,利用中间点,注意点的搜索放在最外层循环,否则最多只能放一个点再两个点之间
for(int k=1;k<=n*m;k++){
for(int i=1;i<=n*m;i++){
for(int j=1;j<=n*m;j++){
a[i][j]=Math.min(a[i][j],a[i][k]+a[k][j]);
}
}
}
int flag=1;//检查连通性
for(int i=1;i<=n*m;i++){
for(int j=1;j<=n*m;j++){
if(a[i][j]==inf){
flag=0;
break;
}
}
}
if(flag==0)
System.out.println("NO");
else
System.out.println("YES");
}
}