CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting

来源:互联网 发布:英语基础入门软件 编辑:程序博客网 时间:2024/05/16 06:32

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 crossingm 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 andm, (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 thei-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 thei-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.


解析

这道题我一开始用的图遍历做的。后来发真相现其实是贪心……

首先图中间的点必然可以走到四条边上的两条,所以只要四条边上的点都强连通了,那么所有的点都强连通了。而只要四个角强连通了,那么四个边上的点必然强连通。判了四个角后四条边上所有点都可以互相走通,然后中间的点一定能通到外面四条边,而四条边又能通到内部的点,所以任意点联通。因此只要不出现左上角<^或右上角^>或左下角<v或右下角v>就可以保证四个角一定强连通。

暴力遍历版

#include<cstdio>#include<cstring>using namespace std;int N,M;char hor[50],ver[50];bool vis[50][50];int dfs(int h,int l){int ans=0;int dertah=ver[l-1]=='v'?1:-1;int dl=hor[h-1]=='>'?1:-1;if(!vis[h+dh][l]){vis[h+dh][l]=1;ans+=dfs(h+dh,l);}if(!vis[h][l+dl]){vis[h][l+dl]=1;ans+=dfs(h,l+dl);}return ans+1;}void pre_process(int h,int l){memset(vis,1,sizeof(vis));for(int i=1;i<=N;i++)for(int j=1;j<=M;j++)vis[i][j]=0;vis[h][l]=1;}int main(){//freopen("B.in","r",stdin);scanf("%d%d",&N,&M);scanf("%s",hor);scanf("%s",ver);for(int i=1;i<=N;i++)for(int j=1;j<=M;j++){pre_process(i,j);if(dfs(i,j)!=N*M) {printf("NO");return 0;};}printf("YES");//while(1);return 0;}
贪心版

#include<cstdio>using namespace std;int N,M;char s1[50],s2[50];int main(){scanf("%d%d",&N,&M);scanf("%s",s1); scanf("%s",s2);bool flag=1;if(s1[0]=='<' && s2[0]=='^') flag=0;//左上if(s1[N-1]=='<' && s2[0]=='v') flag=0;//左下if(s1[0]=='>' && s2[M-1]=='^') flag=0;//右上if(s1[N-1]=='>' && s2[M-1]=='v') flag=0;//右下if(flag) printf("YES");else printf("NO");return 0;}

但是奇怪的是暴力居然比4个if的快= =  好迷啊……

0 0
原创粉丝点击