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

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

  

   DFS:

  1. #include<iostream>
  2. #include<vector>
  3. #include<string.h>
  4. using namespace std;
  5.  
  6. struct Road{
  7. int From;
  8. int Dest;
  9. bool Vist;
  10. Road(int f,int d,bool v){
  11. From = f;
  12. Dest = d;
  13. Vist = v;
  14. }
  15. };
  16. vector<Road> roads[];
  17. bool reach=false;
  18. int N,M;
  19.  
  20. void dfs(int n){
  21. if(n == N-){
  22. reach = true;
  23. return;
  24. }
  25. for(int i=;i<roads[n].size();i++){
  26. if(roads[n][i].Vist && roads[n][i].Dest !=){
  27. int gonext =roads[n][i].Dest;
  28. //use to avoid repeat search
  29. roads[n][i].Vist = false;
  30. dfs(gonext);
  31. }
  32. }
  33. }
  34. int main(){
  35. while(cin>>N && N>){
  36. cin>>M;
  37. reach = false;
  38. memset(roads,,sizeof(roads));
  39. int from,dest;
  40. bool exist = true;
  41. for(int i=;i<M;i++){
  42. cin >> from>>dest;
  43. Road r = Road(from,dest,exist);
  44. roads[from].push_back(r);
  45. }
  46. //deep search
  47. dfs();
  48. if(reach){
  49. cout<<"I can post the letter"<<endl;
  50. }
  51. else{
  52. cout<<"I can't post the letter"<<endl;
  53. }
  54. }
  55. return ;
  56. }

BFS:

  1. #include<iostream>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. int N,M;
  7. int roads[][];
  8. bool visited[];
  9.  
  10. int main(){
  11. while(cin>>N && N>){
  12. cin>>M;
  13. memset(roads,,sizeof(roads));
  14. memset(visited,false,sizeof(visited));
  15. int from=,dest=;
  16. for(int i=;i<M;i++){
  17. cin >> from>>dest;
  18. roads[from][dest] = ;
  19. }
  20. //breadth-first search
  21. queue<int> que;
  22. que.push();
  23. int i;
  24. while(!que.empty() && visited[N-] !=true){
  25. i = que.front();
  26. for(int j=;j<N;j++){
  27. if(roads[i][j] == && visited[j] == false)
  28. {
  29. visited[j] = true;
  30. que.push(j);
  31. }
  32. }
  33. que.pop();
  34. }
  35. if(visited[N-] == true){
  36. cout<<"I can post the letter"<<endl;
  37. }
  38. else{
  39. cout<<"I can't post the letter"<<endl;
  40. }
  41. }
  42. return ;
  43. }

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. css和js引用图片的路径问题

    原文转自:http://www.cnblogs.com/azumia/archive/2012/06/17/2552346.html 在JS文件中书写相对路径:JS文件是指在页面中引用的外部JavaS ...

  2. iBatis查询结果部分为null的解决办法

    今天第一天接触iBatis,没有系统学习过,遇到了一个简单却闹心的错误:用iBatis查询数据库中某个表的多列结果作为一个对象返回时,会出现对象的部分属性为null值得错误.例如,查询用户表中的用户I ...

  3. Javascript 精髓整理篇之二(函数篇)postby:http://zhutty.cnblogs.com

    今天总结的内容是javascript的function, 涉及到function顺便讲讲this. Function 是javascript的函数,也是js的执行单元.函数是JavaScript的一种 ...

  4. AddForce给物体添加刚体效果并且脚本增加一个力(按空格实现)

    using UnityEngine; using System.Collections; public class CubeAddForce : MonoBehaviour { float hor,v ...

  5. [ES6] Class Inherit

    In constructor, you can call parent's constuctor() method by supert(); class ShoppingCart { construc ...

  6. Android ListView 滚动的N种方法

    Android 里面让ListView滚动有N种方法,这儿列举三种: 我的需求是通过按键让Listview滚动起来,当然这些按键不是通过Android标识接口传输过来的,所以不能通过监听按键事件来实现 ...

  7. Tomcat全攻略

    内容: 一:简单介绍 二:安装及配置 三:应用 四:综述 參考资料 关于作者 相关内容: TCP/IP 介绍 TCP/IP 介绍 !== End Related dW Content Area --& ...

  8. MySQL数据库中的哈希加密

    数据库安全是数据库中最为重要的环节,只有确保了数据库中数据的安全,才能够更好的发挥数据库的功能,本文将为大家介绍一种很好的数据库加密方法,即哈希加密. 导读:MySQL数据库加密的方法有很多种,不同的 ...

  9. Oracle11g主要服务程序

    Oracle Orcl VSS Writer Service:Oracle对 VSS(卷影)的支持服务.Oracle卷映射拷贝写入服务VSS(Volume Shadow Copy Service)能够 ...

  10. Web的工作机制

    简要的介绍一下Web的工作机制,以便对开发JavaWeb项目有个更好的理解. 一.Web的概念     1.1    何为Web:Web是万维网(World Wide Web)的简称.Web出现以前, ...