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 ...
随机推荐
- Linux 之 rsyslog 系统日志转发(转载)
一.rsyslog 介绍 ryslog 是一个快速处理收集系统日志的程序,提供了高性能.安全功能和模块化设计.rsyslog 是syslog 的升级版,它将多种来源输入输出转换结果到目的地,据官网介绍 ...
- C语言 · 数字三角形 · 算法训练
问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●1<三角形行数≤100: ...
- zeptoJS:如何像jQuery一样,让滚动变得优雅?
利用jQuery的animate() 方法,我们很容易实现滚动条的平滑滚动效果: $(function() { $('#top').click( function (e) { $('html, bod ...
- 新浪微博登陆,获取token
用WeiboAuthListener获取code 用下面的代码获取token..半成品的sdk真让人捉急. String code = values.getString("code" ...
- Linux安装Weblogic9.2
1.先确认安装的环境是不是LINUX AS 4 [root@yaoxj ~]# cat /etc/issue Red Hat Enterprise Linux AS release 4 (Nahant ...
- uva 10626 - Buying Coke(记忆化搜索)
题目链接:10626 - Buying Coke 题目大意:给出要买可乐的数量, 以及1元,5元和10元硬币的数量, 每瓶可乐8元,每次照钱会按照最少硬币的方式找回, 问如何投币可使得投入的硬币数最少 ...
- Java基础知识强化32:String类之String类的判断功能
1. String类的判断功能: boolean equals (Object obj ) boolean equalsIgnoreCase (String str ) boolean contain ...
- js中this的指向
在js中this的指向对于新手来说一定是个难题,但是如果你真正理解了的话,也就没什么问题啦,下面就来讲讲this吧. JS中,this的值取决于调用的模式(调用对象),而JS中共有4种调用模式: 1. ...
- js-事件委托
事件委托一般用于动态生成的元素中使用,如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- 业余编程 SQL 编程学习——1 (SQL Server 2008 R2)
1.建立test数据库: 2.创建test1数据表: test1表属性如下: 其中,ID字段设置为标识增量,增量种子为1: 这个实例是实现从第一行数据开始,将每一行最后一个字段值加下一行第二个字段值再 ...