CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting
来源:互联网 发布:英语基础入门软件 编辑:程序博客网 时间:2024/05/16 06:32
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.
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.
If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".
3 3><>v^v
NO
4 6<><>v^v^v^
YES
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的快= = 好迷啊……
- CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting
- CodeForces #Bayan# -- C Kamal-ol-molk's Painting 模拟
- 【CODEFORCES】 C. Kamal-ol-molk's Painting
- CF Bayan 2015 Contest Warm Up B.(dfs+暴力)
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- Codeforce 475 C. Kamal-ol-molk's Painting
- CF Bayan 2015 Contest Warm Up B题 Strongly Connected City
- Bayan 2015 Contest Warm Up
- Bayan 2015 Contest Warm Up
- CF Bayan 2015 Contest Warm Up A.(模拟+预处理)
- Bayan 2015 Contest Warm Up (D题)
- Bayan 2015 Contest Warm Up D题(GCD)
- Bayan 2015 Contest Warm Up C题(DP+模拟)
- codeforce Bayan 2015 Contest Warm Up D CGCDSSQ
- Bayan 2015 Contest Warm Up D. CGCDSSQ (数论)
- Bayan 2015 Contest Warm Up(C,D)
- map+pair Bayan 2015 Contest Warm Up D题
- CODEFORCES Bayan 2015 Contest Warm Up A题= =WA啦!~~!!!
- Sqrt(x)
- 【软考】软件工程
- 奇异值分解
- Android开发之Activity的生命周期
- CCObject的分析:release、retain 基于2.2.3,增加3.2 ref对比,Director游戏主线分析
- CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting
- 二进制到十六进制转换法加各种进制的运算
- HDU - 2825 Wireless Password(AC自动机+DP)
- Hibernate学习_017_Hibernate查询语言(QL)的几点说明
- MySQL 高可用架构在业务层面的分析研究
- C语言编程 - 清空键盘缓冲区
- Hibernate学习_018_1+N问题
- 两个常见Qt编译错误的解决
- hdu5058 BC #12 So easy