题意:判断有向图两点之间是否可通达!

解法:深搜或广搜(注意避免旧路重行)

  

   DFS:

 #include<iostream>
#include<vector>
#include<string.h>
using namespace std; struct Road{
int From;
int Dest;
bool Vist;
Road(int f,int d,bool v){
From = f;
Dest = d;
Vist = v;
}
};
vector<Road> roads[];
bool reach=false;
int N,M; void dfs(int n){
if(n == N-){
reach = true;
return;
}
for(int i=;i<roads[n].size();i++){
if(roads[n][i].Vist && roads[n][i].Dest !=){
int gonext =roads[n][i].Dest;
//use to avoid repeat search
roads[n][i].Vist = false;
dfs(gonext);
}
}
}
int main(){
while(cin>>N && N>){
cin>>M;
reach = false;
memset(roads,,sizeof(roads));
int from,dest;
bool exist = true;
for(int i=;i<M;i++){
cin >> from>>dest;
Road r = Road(from,dest,exist);
roads[from].push_back(r);
}
//deep search
dfs();
if(reach){
cout<<"I can post the letter"<<endl;
}
else{
cout<<"I can't post the letter"<<endl;
}
}
return ;
}

BFS:

 #include<iostream>
#include<string.h>
#include<queue>
using namespace std; int N,M;
int roads[][];
bool visited[]; int main(){
while(cin>>N && N>){
cin>>M;
memset(roads,,sizeof(roads));
memset(visited,false,sizeof(visited));
int from=,dest=;
for(int i=;i<M;i++){
cin >> from>>dest;
roads[from][dest] = ;
}
//breadth-first search
queue<int> que;
que.push();
int i;
while(!que.empty() && visited[N-] !=true){
i = que.front();
for(int j=;j<N;j++){
if(roads[i][j] == && visited[j] == false)
{
visited[j] = true;
que.push(j);
}
}
que.pop();
}
if(visited[N-] == true){
cout<<"I can post the letter"<<endl;
}
else{
cout<<"I can't post the letter"<<endl;
}
}
return ;
}

sicily 1155 Can I Post the letter的更多相关文章

  1. [SOJ] can I post the letter?

    1155. Can I Post the letter Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description I am a t ...

  2. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  4. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  5. 什么是Unicode letter

    起因:从一段代码说起 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  6. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  8. No.017:Letter Combinations of a Phone Number

    问题: Given a digit string, return all possible letter combinations that the number could represent.A ...

  9. SCI/EI期刊投稿 Reply Letter 常用格式总结

    SCI/EI期刊投稿Reply Letter常用格式总结          整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...

随机推荐

  1. Oracle_Q&A_04

    2014-12-19作业 [JSU]LJDragon's Oracle course tasks In the first semester, junior year --1.在管理员权限下创建一个新 ...

  2. Ajax解析

    1.Ajax Asynchronous(异步的) javascript and xml 技术组成: CSS + xml +JavaScript +DOM Ajax核心对象: XmlHttpReques ...

  3. 三、Solr多核心及分词器(IK)配置

    多核心的概念 多核心说白了就是多索引库.也可以理解为多个"数据库表" 说一下使用multicore的真实场景,比若说,产品搜索和会员信息搜索,不使用多核也没问题,这样带来的问题是 ...

  4. [Polymer] Introduction

    install Polymer and explore creating our first custom element: bower install polymer index.html: < ...

  5. c# winform InvokeRequired 解决跨线程访问控件

    C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它. Windows 窗体中 ...

  6. js中时间戳与日期转换-js日期操作

    常用的一些日期操作. 用js获取一个时间戳. <script type="text/javascript"> var date = new Date();//当前时间 ...

  7. SOAP 简单对象访问协议

    webService三要素 SOAP.WSDL(WebServicesDescriptionLanguage).UDDI(UniversalDescriptionDiscovery andIntegr ...

  8. Intent MIME 打开各种类型的文件

    使用 public class MainActivity extends ListActivity {     public static final String path = Environmen ...

  9. Accordion( 分类) 组件

    一. 加载方式 //class 加载方式<div id="box" class="easyui-accordion"style="width:3 ...

  10. $http post传值的问题

    var app = angular.module("myApp", [], function ($httpProvider) { $httpProvider.defaults.he ...