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 ...
随机推荐
- 你需要知道的九大排序算法【Python实现】源码
#coding: utf-8 #!/usr/bin/python import randomimport math #随机生成0~100之间的数值def get_andomNumber(num): l ...
- Java 泛型数组
Java 不支持泛型数组.也就是说, List<String>[] ls = new ArrayList<String>[10]; 是不支持的,而 List<String ...
- Face recognition using Histograms of Oriented Gradients
Face recognition using Histograms of Oriented Gradients 这篇论文的主要内容是将Hog算子应用到人脸识别上. 转载请注明:http://blog. ...
- Java 将自己定义的对象作为HashMap的key
须要继承Map的equals函数和hashCode函数 package com.category; import java.util.HashMap; public class GenCategory ...
- HDU2841 Visible Trees (容斥原理)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意: 一个人在(0,0)点,然后前面有一个m*n的格子 ,每一个格子的节点上有一棵树.问这个人 ...
- web性能优化——JSP
一.啰嗦 做web开发的都知道,性能的重要性就不必强调了.就前端展示的工作来说,jsp大家都熟悉html更熟悉:web服务器tomcat应该是最熟悉的了:web方面的基础知识上来说,静态页面比动态页面 ...
- somethings about QSplitter
m_splitter = new QSplitter(Qt::Horizontal); m_splitter->addWidget(this->m_leftWidget); m ...
- 【录音】Android录音--AudioRecord、MediaRecorder
Android提供了两个API用于实现录音功能:android.media.AudioRecord.android.media.MediaRecorder. 网上有很多谈论这两个类的资料.现在大致总结 ...
- Java基础知识强化46:StringBuffer类之判断一个字符串是否对称案例
1. 分析:判断一个字符串是否是一个对称的字符串,我们只需要把字符串的第1个字符和最后1个字符,第2个字符和倒数第2个字符,…… 比较的次数是长度除以2. 方法1:通过取取索引对应值来进行一一比对 ...
- USB HID介绍
HID是一种USB通信协议,无需安装驱动就能进行交互,在学习HID之前,先来复习一下USB协议的相关内容. USB设备描述符-概述 当插入USB设备后,主机会向设备请求各种描述符来识别设备.那什么是设 ...