设计方案1
考虑用数组实现二次或是多次换成:
方法: 可以考虑用list 或是用二维数组来保持你从数据库中查询出来的数据。
提示: 二维数组和list用法省略;
从数据库查出来的数据用数组或是list保存;
建议: 比较的时候可以用循环及相关的方法;提示一种list方法用于比较list数组的相同元素;
array_intersect() 返回一个数组,该数组包含了所有在 array1 中也同时出现在所有其它参数数组中的值。注意键名保留不变。
例子:
<?php
$array1 = array(“a” => “green”, “red”, “blue”);
$array2 = array(“b” => “green”, “yellow”, “red”);
$result = array_intersect($array1, $array2);
?>
这使得 $result 成为:
Array
(
[a] => green
[0] => red
)
数据库的建立有如下建议:
用站绑定经过的所有公交车 如:大石桥(1,2,3,4,5);或是:大石桥(1)大石桥(2)大石桥(3)大石桥(4)大石桥(5)可用存储过程建立临时的表来存储查出来的信息,也可以在数据库建立相关的表,这个主要是为了把可能的路线全部查出来。
设计方案2
考虑存储过程和队列和栈来实现二次或是多次换成
方法:用存储过程和栈来保存数据库中的数据及实现二次或是多次换成
存储过程的具体用法:
1. 数据库的数据建成字符串的方式如 大石桥(1,2,3,4,5)
用存储过程比较的方法如下:
CREATE function uf_splitstring
(
@str varchar(8000) --要分拆的字符串
,@spli varchar(10) --字符串分隔符
)
returns @retab table(istr varchar(8000))
as
begin
declare @i int
declare @splen int
select @splen=len(@spli),@i=charindex(@spli,@str)
while @i > 0
begin
insert into @retab
values(left(@str,@i-1))
select @str=substring(@str,@i+@splen,8000)
select @i=charindex(@spli,@str)
end
if @str <> ' ' insert into @retab values(@str)
return
end
GO
if exists(select 1 from uf_splitstring( '001,002,003 ', ', ')t,uf_splitstring( '002,004 ', ', ')t1 where t.istr=t1.istr)
print '1 '
else
print '0 '
2. 具体用存储过程和栈,队列的实现过程:
1. 提示:要明白这样建库的含义
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[t]
GO
CREATE TABLE [dbo].[t] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[lid] [nvarchar] (50) ,
[name] [nvarchar] (50) ,
[type] [int] NOT NULL
)
GO
insert into t (lid,[name],[type])
select "11","城站火车站",0
union all select "11","葵巷建国路口",0
union all select "11","菜市桥",0
union all select "11","潮鸣寺巷",0
union all select "11","宝善桥建国路口",0
union all select "11","宝善桥",0
union all select "11","市体育馆",0
union all select "11","武林广场",0
union all select "11","武林门",0
union all select "57","浙江工商大学",0
union all select "57","文一路口",0
union all select "57","教工路北口",0
union all select "57","大关桥西",0
union all select "57","上塘路口",0
union all select "57","大关西六苑",0
union all select "57","香积寺路口",0
union all select "57","大关小区",0
union all select "14","武林小广场",0
union all select "14","汽车南站",1
union all select "14","近江村",1
union all select "14","华东家具市场",1
union all select "14","总管塘",1
union all select "14","金衙庄",1
union all select "14","庆春门",1
union all select "14","大学路北口",1
union all select "14","浙一医院",1
union all select "14","众安桥",1
union all select "14","中大广场",1
union all select "14","延安路口",1
union all select "14","长寿桥",1
union all select "14","昌化新村",1
union all select "21/K21","浙大西溪校区",0
union all select "21/K21","庆丰村",0
/****** Object: Stored Procedure dbo.Search Script Date: 2005-9-8 10:28:35 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Search]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure dbo.Search Script Date: 2005-9-8 10:28:35 ******/
CREATE proc Search
@name1 nvarchar(50),
@name2 nvarchar(50)
as
--暂存库
create table #tmp
(
tmp_id int identity(1,1),
tmp_name NVARCHAR(50)
)
--站点队列
create table #tmp1
(
tmp1_id int identity(1,1),
tmp1_name NVARCHAR(50)
)
--查找结果
create table #result
(
r_id int,
r_lid nvarchar(50),
r_name nvarchar(50),
r_type int
)
--直达
insert into #result
select c.* from t a,t b,t c where
a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
and a.[name] = @name1 and b.[name] = @name2
and c.id>=a.id and c.id<=b.id order by c.id
if @@rowcount>0 begin
select * from #result
end
else begin
--换车
DECLARE @CurrenName NVARCHAR(50)
SET @CurrenName = @name1
change:
/*
--车次入栈
insert into #tmp (tmp_lid)
select distinct lid from t where [name] = @CurrenName
DECLARE @CurrenBus NVARCHAR(50)
SELECT TOP 1 @CurrenBus = tmp_lid FROM #tmp
*/
INSERT INTO #tmp1 (tmp1_name)
SELECT DISTINCT b.[name] FROM t a,t b WHERE a.[name] = @CurrenName AND b.lid = a.lid AND b.[name] <> @CurrenName
INSERT INTO #tmp (tmp_name)
select d.[tmp1_name] from t a,t b,t c, #tmp1 d where
a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
and a.[name] = d.[tmp1_name] and b.[name] = @name2
and c.id>=a.id and c.id<=b.id
IF @@rowcount>0 BEGIN
select distinct c.* from t a,t b,t c,#tmp d where
a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
and a.[name] = @name1 and b.[name] = d.tmp_name
and c.id>=a.id and c.id<=b.id order by c.id
select distinct c.* from t a,t b,t c,#tmp d where
a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
and a.[name] = d.tmp_name and b.[name] = @name2
and c.id>=a.id and c.id<=b.id order by c.id
END
--SELECT * FROM #tmp
end
drop table #result
drop table #tmp1
drop table #tmp
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
exec search '你输入的站','你输入的站'
以上便是应用存储过程和栈的实现方法;注意的是怎么在C#中条用存储过程和某些地方改成传参的方法;
建立数据库必需有个表示按照以上的方法建立;其他的看其需要而建。在存储过程中出现的条件一定要考虑全面,不能漏掉,也不能多。
设计方案 3 用存储过程
考虑用存储过程来实现二次换成或是多次换成 (以下只是提示二次换成)
方法如下:
这便是建立数据库的内容,一定要理解这样建库有什么好处。
create table Line(lineID int,state nvarchar
(10),orderid int,primary key(lineID,orderid))
insert Line select 1,'鼓楼' ,1
union all select 1,'新街口',2
union all select 1,'汽车站',3
union all select 1,'火车站',4
union all select 2,'新街口',1
union all select 2,'飞机场',2
union all select 2,'天安门',3
union all select 3,'天安门',1
union all select 3,'石门坎',2
union all select 3,'驾校' ,3
go
-- 查询的存储过程
create proc p_qry
@begin_state nvarchar(10),
@end_state nvarchar(10)
as
set nocount on
declare @l int
--正向搜索线路
set @l=0
--把数据插入临时表
select lineID,line=cast('('+rtrim(lineID)
+': '+rtrim(state) as varchar(8000))
,state,orderid=orderid+1,level=@l,gid=1
into #t from Line where state=@begin_state
while @@rowcount>0 and not exists(select * from
#t where state=@end_state) and exists(select * from line where state=@end_state)
begin
set @l=@l+1
-- 如果循环次数大于所有站点数,则跳出循环
if @l>(select count(*) from line)
break
else
--把数据插入临时表
insert #t(line,lineID,state,orderid,level,gid)
select
line=a.line+case
when a.lineID=b.lineID
then '->'+rtrim(b.state)
else ') => ('+rtrim(b.lineID)+': '+rtrim(b.state)
end,
lineID=b.lineID,state=b.state,orderid=b.orderid+1,
@l,
case when a.lineID=b.lineID then a.gid else
a.gid+1 end
from #t a,Line b
where a.level=@l-1
and(
a.lineID=b.lineID and a.orderid=b.orderid
or
a.state=b.state and a.lineID<>b.lineID)
continue
end
--
-- 如果正向思索存在线路,则选择出来
if exists(select 起点站=@begin_state
,终点站=@end_state
,转车次数=gid
,经过站数=case when gid<3 then @l else @l-gid+2
end
,乘车线路=line+')'
from #t where level=@l and state=@end_state)
begin
select 起点站=@begin_state
,终点站=@end_state
,转车次数=gid
,经过站数=case when gid<3 then @l else @l-gid+2
end
,乘车线路=line+')'
from #t where level=@l and state=@end_state
end
--如果正向搜索不存在线路,则反向搜索
if not exists(select 起点站=@begin_state
,终点站=@end_state
,转车次数=gid
,经过站数=case when gid<3 then @l else @l-gid+2
end
,乘车线路=line+')'
from #t where level=@l and state=@end_state)
begin
--反向搜索线路向搜索线路
set nocount on
set @l=0
select lineID,line=cast('('+rtrim(lineID)
+': '+rtrim(state) as varchar(8000))
,state,orderid=orderid-1,level=@l,gid=1
into #m from Line where state=@begin_state
while @@rowcount>0 and not exists(select * from
#m where state=@end_state) and exists(select * from line where state=@end_state)
begin
set @l=@l+1
if @l>(select count(*) from line)
break
else
insert #m(line,lineID,state,orderid,level,gid)
select
line=a.line+case
when a.lineID=b.lineID
then '->'+rtrim(b.state)
else ') => ('+rtrim(b.lineID)+': '+rtrim(b.state)
end,
lineID=b.lineID,state=b.state,orderid=b.orderid-1,
@l,
case when a.lineID=b.lineID then a.gid else
a.gid+1 end
from #m a,Line b
where a.level=@l-1
and(
a.lineID=b.lineID and a.orderid=b.orderid
or
a.state=b.state and a.lineID<>b.lineID)
continue
end
select 起点站=@begin_state
,终点站=@end_state
,转车次数=gid
,经过站数=case when gid<3 then @l else @l-gid+2
end
,乘车线路=line+')'
from #m where level=@l and state=@end_state
-- 从正反两面判断是否存在路线,如果不存在,则提示!
if not exists(select 起点站=@begin_state
,终点站=@end_state
,转车次数=gid
,经过站数=case when gid<3 then @l else @l-gid+2
end
,乘车线路=line+')'
from #t where level=@l and state=@end_state)
and
not exists(select 起点站=@begin_state
,终点站=@end_state
,转车次数=gid
,经过站数=case when gid<3 then @l else @l-gid+2
end
,乘车线路=line+')'
from #m where level=@l and state=@end_state)
-- print '没有符合条件的记录,请重新输入数据!!!'
begin
print '没有找到符合条件的记录'
end
end
go
--调用
exec p_qry '石门坎','鼓楼'
exec p_qry '火车站','鼓楼'
exec p_qry '鼓楼','石门坎'
exec p_qry '石门坎','鼓楼'
go
-- 删除测试
drop table Line
drop proc p_qry
是个我就不解释多少了,方法比较简单,只是你个逻辑问题想通就好了,但是这个方法的效率问题不怎么好,但是比数组要高。一定要注意建立数据库的具体方式一般建议你用提示的。有好的就更不用说了。
设计方案4
此方案是最好的 是用估价函数中的剪枝算法和多进程来实现,其中设计到建模,抱歉的是我不会估价函数,看了也没有看明白,希望你能看懂。
建立数据库也是很重要的 建立的时候想好了在建立