将这个图存起来,彼此每两个结点的关系。因为floyd是用于解决点与点之间的关系的,所以每行每列的交点都是作为结点来处理的,按照行优先的顺序将所有的顶点进行编号,咱们就会得到n*m个点,之后就是根据每行每列的方向将这些点之间的连通起来,最后再三重for循环floyd将图的点的连通性计算出来,最后只要判断这些顶点中是否有不可达的两个顶点
3 3
| ><>
| v^v
NO
https://codeforces.com/contest/475/standings
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+5;
const int INF=0x3f3f3f3f;
ll n,m;
ll a[maxn],mp[405][405],from,to;
string s,t;
int main(){
cin>>n>>m;
cin>>s>>t;
memset(mp,INF,sizeof mp);
for(int i=1;i<=n*m;i++)
mp[i][i]=0; //自己到自己没有边
for(int i=1;i<=n;i++){
from=(i-1)*m+1;//每行第一个
if(s[i-1]=='>'){
to=from+1;
for(int j=1;j<m;j++){ //连m-1次
mp[from][to]=1;
from=to;
to=from+1;
}
}
else{
to=from+1;
for(int j=1;j<m;j++){
mp[to][from]=1;
from=to;
to=from+1;
}
}
}
for(int i=1;i<=m;i++){
from=i; //每列第一个编号
if(t[i-1]=='v'){
to=from+m;
for(int j=1;j<n;j++){
mp[from][to]=1;
from=to;
to=from+m;
}
}
else{
to=from+m;
for(int j=1;j<n;j++){ //连n-1次边
mp[to][from]=1;
from=to;
to=from+m;
}
}
}
ll N=n*m; //400个交点
for(int k=1;k<=N;k++){ //400*400*400 floyd判断连通性
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
int flag=1;
for(int i=1;i<=N && flag;i++){
for(int j=1;j<=N && flag;j++){
if(mp[i][j]>=INF){
flag=0;
}
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}