475B Strongly Connected City

2024-08-31 12:58
文章标签 city connected strongly 475b

本文主要是介绍475B Strongly Connected City,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 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".

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.

题目意思:

画n条水平线,m条垂直线,有n*m个交点,然后每条直线只给定一个通行方向,问所有交点能不能走通。

后来看了一种方法,现在添加进去,如果外围是顺时针或则逆时针,则YES

分析:刚开始想是不是博弈,想到每条线的方向有影响,就不考虑了。后来,看到数据范围比较小,问能不能走通嘛,floyd求出所有交点的最小距离。因此,这题就转成了图去。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define INF 10000
#define eps 1e-6
#define pb push_back
#define pf printf
#define sf scanf
#define si(n) sf("%d",&n)
#define pi(n) pf("%d\n",n)
#define ms memset
#define bk break
#define rt return
#define ct continue
#define LL long long
#define REP0(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define mp(x,y) make_pair((x),(y))
#define maxn 500
#define MOD
#define db double
#define op operator
#define PI acos(-1)
using namespace std;
int n,m;
int mapt[maxn][maxn];
int num[30][30];
void init(){REP(i,1,n*m){REP(j,1,n*m){mapt[i][j]=INF;}mapt[i][i]=0;}
}
bool judge(){REP(i,1,n*m)REP(j,1,n*m)if(mapt[i][j]==INF)rt false;rt true;
}
void 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++){if(mapt[i][j]>mapt[i][k]+mapt[k][j] )mapt[i][j]=mapt[i][k]+mapt[k][j];}}}
}
void out(){REP(i,1,n*m){REP(j,1,n*m){cout<<mapt[i][j]<<" ";}cout<<endl;}cout<<endl;
}
int main(){#ifdef ACBangfreopen("in.txt","r",stdin);#endifwhile(~sf("%d%d",&n,&m)){init();
//out();int cnt=1,a,b;REP(i,1,n)REP(j,1,m)num[i][j]=cnt++;char s[100];sf("%s",s);REP(i,1,n){if(s[i-1]=='>'){for(int t=1;t<=m;t++){for(int k=t+1;k<=m;k++){a=num[i][t];b=num[i][k];mapt[a][b]=1;}}}else {for(int t=1;t<=m;t++){for(int k=t+1;k<=m;k++){a=num[i][t];b=num[i][k];mapt[b][a]=1;}}}}
//out();sf("%s",s);REP(j,1,m){if(s[j-1]=='^'){for(int t=1;t<=n;t++){for(int k=t+1;k<=n;k++){a=num[t][j];b=num[k][j];mapt[b][a]=1;}}}else {for(int t=1;t<=n;t++){for(int k=t+1;k<=n;k++){a=num[t][j];b=num[k][j];mapt[a][b]=1;}}}}
//out();floyd();
//out();if(judge()==true)puts("YES");else puts("NO");}rt 0;
}


这篇关于475B Strongly Connected City的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123971

相关文章

OpenCV_连通区域分析(Connected Component Analysis-Labeling)

申明:本文非笔者原创,原文转载自:http://blog.csdn.net/icvpr/article/details/10259577 OpenCV_连通区域分析(Connected Component Analysis/Labeling) 【摘要】 本文主要介绍在CVPR和图像处理领域中较为常用的一种图像区域(Blob)提取的方法——连通性分析法(连通区域标

VirtualBox安装VirtualBox Extension Pack,支持USB No USB devices connected after upgrade

安装步骤及出现问题解决No USB devices connected after upgrade: 一、本要主机ubuntu14.04,安装virtualbox,支持usb设置步骤: 1.安装VirtualBox. 可以从https://www.virtualbox.org官方站点下载或者从软件中心。 2.在VirtualBox里安装Windows; 3.为USB2.0,你需要

GNN-频域-2014:Spectral Networks and Locally Connected Networks on Graphs(频谱图卷积神经网络)【第一篇从频域角度分析】

《原始论文:Spectral Networks and Locally Connected Networks on Graphs》 空域卷积非常直观地借鉴了图像里的卷积操作,但缺乏一定的理论基础。 而频域卷积则不同,相比于空域卷积而言,它主要利用的是**图傅里叶变换(Graph Fourier Transform)**实现卷积。 简单来讲,它利用图的**拉普拉斯矩阵(Laplacian ma

C++卷积神经网络实例:tiny_cnn代码详解(9)——partial_connected_layer层结构类分析(下)

在上一篇博文中我们着重分析了partial_connected_layer类的成员变量的结构,在这篇博文中我们将继续对partial_connected_layer类中的其他成员函数做一下简要介绍。   一、构造函数   由于partial_connected_layer类是继承自基类layer,因此在构造函数中同样分为两部分,即调用基类构造函数以及初始化自身成员变量: partial

C++卷积神经网络实例:tiny_cnn代码详解(8)——partial_connected_layer层结构类分析(上)

在之前的博文中我们已经将顶层的网络结构都介绍完毕,包括卷积层、下采样层、全连接层,在这篇博文中主要有两个任务,一是整体贯通一下卷积神经网络在对图像进行卷积处理的整个流程,二是继续我们的类分析,这次需要进行分析的是卷积层和下采样层的公共基类:partial_connected_layer。   一、卷积神经网络的工作流程   首先给出经典的5层模式的卷积神经网络LeNet-5结构模型:

C++卷积神经网络实例:tiny_cnn代码详解(7)——fully_connected_layer层结构类分析

之前的博文中已经将卷积层、下采样层进行了分析,在这篇博文中我们对最后一个顶层层结构fully_connected_layer类(全连接层)进行分析:   一、卷积神经网路中的全连接层   在卷积神经网络中全连接层位于网络模型的最后部分,负责对网络最终输出的特征进行分类预测,得出分类结果:   LeNet-5模型中的全连接层分为全连接和高斯连接,该层的最终输出结果即为预测标签,例如

今天你City了吗?维乐Angel Revo带你穿梭都市自由随风~

当7月的热浪在都市中翻滚,你是否渴望逃离钢筋水泥的束缚,寻找一片属于自己的绿意盎然?今天你City了吗?快带上VELO Angel Revo一起抓住夏日的尾巴,用一场骑行与这座城市的风景共舞!      轻巧出行,City的轻盈      维乐Angel Revo坐垫,轻如鸿毛,却承载着都市骑行的所有需求。尺寸完美,净重轻盈,仿佛是为都市骑行量身定制的艺术品。无论是穿梭在繁忙的都市街道,还是悠闲地

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题。。稍微处理一下然后用求面积并的方法求即可。 代码如下: #include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#

[google Autocomplete API 调用 查询 address,city,state]

主要功能:调用google  Autocomplete api  完成如下图功能。  第一步 页面调用 google api 【https://maps.googleapis.com/maps/api/js?key=" + GoogleAPIKey + "&libraries=places&callback=InitAutocompleteCityState&language=en】 G

【论文阅读】Semantic Segmentation with deep convolutional nets and fully connected CRFs

一、摘要 深度卷积神经网络(DCNN)最近在高级视觉任务中展示了最先进的性能,例如图像分类和对象检测。这项工作汇集了来自DCNN和概率图形模型的方法,用于解决像素级分类(也称为“语义图像分割”)的任务。我们表明DCNN最后一层的响应没有充分定位,无法进行精确的对象分割。这是由于非常不变的属性使DCNN有利于高级任务。 我们通过将最终DCNN层的响应与完全连接的条件随机场(CRF