sicily 1155 Can I Post the letter
题意:判断有向图两点之间是否可通达!
解法:深搜或广搜(注意避免旧路重行)
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的更多相关文章
- [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 ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 什么是Unicode letter
起因:从一段代码说起 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- sicily 1934. 移动小球
Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...
- No.017:Letter Combinations of a Phone Number
问题: Given a digit string, return all possible letter combinations that the number could represent.A ...
- SCI/EI期刊投稿 Reply Letter 常用格式总结
SCI/EI期刊投稿Reply Letter常用格式总结 整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...
随机推荐
- css和js引用图片的路径问题
原文转自:http://www.cnblogs.com/azumia/archive/2012/06/17/2552346.html 在JS文件中书写相对路径:JS文件是指在页面中引用的外部JavaS ...
- iBatis查询结果部分为null的解决办法
今天第一天接触iBatis,没有系统学习过,遇到了一个简单却闹心的错误:用iBatis查询数据库中某个表的多列结果作为一个对象返回时,会出现对象的部分属性为null值得错误.例如,查询用户表中的用户I ...
- Javascript 精髓整理篇之二(函数篇)postby:http://zhutty.cnblogs.com
今天总结的内容是javascript的function, 涉及到function顺便讲讲this. Function 是javascript的函数,也是js的执行单元.函数是JavaScript的一种 ...
- AddForce给物体添加刚体效果并且脚本增加一个力(按空格实现)
using UnityEngine; using System.Collections; public class CubeAddForce : MonoBehaviour { float hor,v ...
- [ES6] Class Inherit
In constructor, you can call parent's constuctor() method by supert(); class ShoppingCart { construc ...
- Android ListView 滚动的N种方法
Android 里面让ListView滚动有N种方法,这儿列举三种: 我的需求是通过按键让Listview滚动起来,当然这些按键不是通过Android标识接口传输过来的,所以不能通过监听按键事件来实现 ...
- Tomcat全攻略
内容: 一:简单介绍 二:安装及配置 三:应用 四:综述 參考资料 关于作者 相关内容: TCP/IP 介绍 TCP/IP 介绍 !== End Related dW Content Area --& ...
- MySQL数据库中的哈希加密
数据库安全是数据库中最为重要的环节,只有确保了数据库中数据的安全,才能够更好的发挥数据库的功能,本文将为大家介绍一种很好的数据库加密方法,即哈希加密. 导读:MySQL数据库加密的方法有很多种,不同的 ...
- Oracle11g主要服务程序
Oracle Orcl VSS Writer Service:Oracle对 VSS(卷影)的支持服务.Oracle卷映射拷贝写入服务VSS(Volume Shadow Copy Service)能够 ...
- Web的工作机制
简要的介绍一下Web的工作机制,以便对开发JavaWeb项目有个更好的理解. 一.Web的概念 1.1 何为Web:Web是万维网(World Wide Web)的简称.Web出现以前, ...