codeforces 475 B. Strongly Connected City

http://codeforces.com/problemset/status 题意:n行m列的道路网络。共n*m条道路。每条道路都是单向的.问从任何一个路口出发能否到达其他的任何一个路口。

思路:需要注意的是。我从A点能到达B点,不代表B也能到达A.也就是说,某些点满足可以遍历所有点是不够的,只有当所有点都满足才可以。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月07日 星期一 08时55分48秒
  4File Name :code/cf/problem/475B.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25
 26
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=25;
 34int n,m;
 35bool vis[N][N];
 36char hor[N],ver[N];
 37
 38bool direrror (char ch,int d)
 39{
 40    if (ch=='v'&&d==3) return true;
 41    if (ch=='^'&&d==0) return true;
 42
 43    if (ch=='<'&&d==2) return true;
 44    if (ch=='>'&&d==1) return true;
 45
 46	return false;
 47}
 48
 49bool ok ( int x,int y)
 50{
 51    if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]) return true;
 52    return false;
 53}
 54
 55
 56void dfs ( int x,int y)
 57{
 58    vis[x][y] = true;
 59   // cout<<"x:"<<x<<" y:"<<y<<endl;
 60    for ( int i = 0 ; i < 4 ; i++)
 61    {
 62	if (direrror(hor[x],i)) continue;
 63	if (direrror(ver[y],i)) continue;
 64
 65	int nx = x + dx4[i];
 66	int ny = y + dy4[i];
 67	if (ok(nx,ny))
 68	{
 69	    dfs(nx,ny);
 70	}
 71    }
 72}
 73
 74bool solve ( int x,int y)
 75{
 76    ms(vis,false);
 77    dfs(x,y);
 78    for ( int i = 0 ; i < n ; i++)
 79	for ( int j = 0 ; j < m; j ++)
 80	    if (!vis[i][j]) return false;
 81    return true;
 82
 83}
 84int main()
 85{
 86	#ifndef  ONLINE_JUDGE 
 87	freopen("code/in.txt","r",stdin);
 88  #endif
 89
 90
 91	cin>>n>>m;                      //注意题目要求是从任意一点都可以到达其他任何点。
 92	for ( int i = 0 ; i < n ; i ++) cin>>hor[i]; 
 93	for ( int i = 0 ; i < m ; i ++) cin>>ver[i];
 94
 95	int cnt = 0 ;
 96	bool ok = true;
 97	for ( int i = 0 ; i< n ; i++)
 98	    for ( int j = 0 ;j < m;  j++)
 99		if (!solve(i,j))
100		{
101		    ok = false;
102		    break;
103		}
104//	cout<<"cnt:"<<cnt<<endl;
105	if (ok)
106	{
107	    puts("YES");
108	}
109	else
110	{
111	    puts("NO");
112	}
113
114  #ifndef ONLINE_JUDGE  
115  fclose(stdin);
116  #endif
117    return 0;
118}