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

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

  

   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. AndroidUI开源组件库BottomView 第三方自定义UI控件

    这里分享一个Android的非常经典实用而且简单方便的第三方UI控件库:BottomView(小米的米UI也用到了这个) 原文  http://blog.csdn.net/opzoonzhuzheng ...

  2. Android之旅十八 百度地图环境搭建

    在android中使用百度地图,我们能够先看看百度地图对应的SDK信息:http://developer.baidu.com/map/index.php? title=androidsdk,它里面基本 ...

  3. _getch() 和 getch() 及 _T()

    带下划线_的函数一般是函数库内部的函数,而不带下划线的一般是提供给用户使用的函数.带下划线的目的是为了防止用户定义的函数和函数库的函数重名冲突,所以直接使用也是可以的.要用getch()必须引入头文件 ...

  4. Android Studio使用教程(一)

    今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...

  5. snappydb 依赖的jar包

    最近看学习了一下snappydb,因为我用的还是Eclipse但是github上的是as项目所以就考虑用jar包来使用. github地址:https://github.com/nhachicha/S ...

  6. for语句应用:乘法表

    乘法表: for语句应用: 1: 2: public class chengfa { 3: public static void main(String[] args) { 4: //int i; 5 ...

  7. eclipse python开发环境搭建

    eclipse python开发环境搭建[非原创] 1.在www.eclipse.org官网下载Eclipse Classic 4.2.2,Win7 64位下载eclipse-SDK-4.2.2-wi ...

  8. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  9. [Mugeda HTML5技术教程之4] Studio 概述

    Mugeda Studio 是基于云平台的制作HTML5动画的专业可视化集成开发环境,可以让你在不需要安装客户端程序的情况下,只通过浏览器就能轻松创作高质量的HTML5动画.HTML5动画相对于传统的 ...

  10. php 之 类,对象

    --恢复内容结束--- 一.类和对象: 1.定义: 对象:我们所见到的东西都可以称之为对象,是类实例化出来的东西 类:是对所有的同类对象抽象出来的东西 eg: 在一张表中记录了全班同学的学号,姓名,性 ...