广度优先搜索.docx
- 文档编号:8734119
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:8
- 大小:24.07KB
广度优先搜索.docx
《广度优先搜索.docx》由会员分享,可在线阅读,更多相关《广度优先搜索.docx(8页珍藏版)》请在冰豆网上搜索。
广度优先搜索
广度优先搜索
5、细胞(cell.pas)
【问题描述】
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。
如阵列:
0234500067103456050020456006710000000089有4个细胞。
【输入格式】整数m,n(m行,n列)矩阵
【输出格式】细胞的个数。
【输入样例】cell.in
410
023*******
1034560500
2045600671
0000000089
【输出样例】cell.out
4
【参考程序】
const
di:
array[1..4]of-1..1=(0,0,-1,1);
dj:
array[1..4]of-1..1=(1,-1,0,0);
varm,n:
byte;
b:
array[0..51,0..61]ofboolean;
que:
array[1..3000]ofrecordi,j:
byte;end;
num:
word;
procedureinit;
vari,j:
byte;
ch:
char;
begin
assign(input,'cell.in');reset(input);
fillchar(b,sizeof(b),false);
num:
=0;
readln(m,n);
fori:
=1tomdo
begin
forj:
=1tondo
begin
read(ch);
ifch<>'0'thenb[i,j]:
=true;
end;
readln;
end;
close(input);
end;{init}
procedurefind(newi,newj:
byte);
vark:
byte;
head,tail:
word;
begin
b[newi,newj]:
=false;
inc(num);
withque[1]dobegini:
=newi;j:
=newj;end;
head:
=0;tail:
=1;
repeat
inc(head);
fork:
=1to4do
if(que[head].i+di[k]in[1..m])and(que[head].j+dj[k]in[1..n])and(b[que[head].i+di[k],que[head].j+dj[k]])then
begin
inc(tail);
withque[tail]dobegini:
=que[head].i+di[k];j:
=que[head].j+dj[k];end;
b[que[tail].i,que[tail].j]:
=false;
end;
untilhead=tail;
end;{find}
procedurework;
vari,j:
byte;
begin
fori:
=1tomdo
forj:
=1tondo
ifb[i,j]thenfind(i,j);
end;{work}
procedureprint;
begin
assign(output,'cell.out');rewrite(output);
writeln(num);
close(output);
end;{print;}
begin{main}
init;
work;
print;
end.
6、营救
【问题描述】
铁塔尼号遇险了!
他发出了求救信号。
距离最近的哥伦比亚号收到了讯息,时间就是生命,必须尽快赶到那里。
通过侦测,哥伦比亚号获取了一张海洋图。
这张图将海洋部分分化成n*n个比较小的单位,其中用1标明的是陆地,用0标明是海洋。
船只能从一个格子,移到相邻的四个格子。
为了尽快赶到出事地点,哥伦比亚号最少需要走多远的距离。
【输入格式】
第一行为n,下面是一个n*n的0、1矩阵,表示海洋地图
最后一行为四个小于n的整数,分别表示哥伦比亚号和铁塔尼号的位置。
【输出格式】
哥伦比亚号到铁塔尼号的最短距离,答案精确到整数。
【输入样例】save.in
3
001
101
100
1133
【输出样例】save.out
4
【数据范围】
N<=1000
【参考程序】
const
dx:
array[1..4]ofshortint=(1,-1,0,0);
dy:
array[1..4]ofshortint=(0,0,-1,1);
varn,x1,y1,x2,y2:
word;
map:
array[0..1001,0..1001]ofchar;
que:
array[1..1000000]ofrecordx,y,dep:
word;end;
procedureinit;
vari,j:
word;
begin
assign(input,'save.in');reset(input);
assign(output,'save.out');rewrite(output);
fillchar(map,sizeof(map),'1');
readln(n);
fori:
=1tondo
begin
forj:
=1tondo
read(map[i,j]);
readln;
end;
readln(x1,y1,x2,y2);
close(input);
end;{init}
procedurework;
varhead,tail:
longword;
i:
byte;
begin
withque[1]dobeginx:
=x1;y:
=y1;dep:
=0;end;
head:
=0;tail:
=1;
repeat
inc(head);
fori:
=1to4do
if(que[head].x+dx[i]>0)and(que[head].x+dx[i]<=n)
and(que[head].y+dy[i]>0)and(que[head].y+dy[i]<=n)
and(map[que[head].x+dx[i],que[head].y+dy[i]]='0')then
begin
inc(tail);
withque[tail]dobeginx:
=que[head].x+dx[i];y:
=que[head].y+dy[i];dep:
=que[head].dep+1end;
map[que[tail].x,que[tail].y]:
='1';
if(que[tail].x=x2)and(que[tail].y=y2)thenbeginwriteln(que[tail].dep);close(output);halt;end;
end;
untilhead=tail;
end;{work}
begin{main}
init;
work;
end.
7、硬币翻转(coin.pas)
【问题描述】
在桌面上有一排硬币,共N枚,每一枚硬币均为正面朝上。
现在要把所有的硬币翻转成反面朝上,规则是每次可翻转任意N-1枚硬币(正面向上的被翻转为反面向上,反之亦然)。
求一个最短的操作序列(将每次翻转N-1枚硬币成为一次操作)。
【输入格式】输入只有一行,包含一个自然数N(N为不大于100的偶数)。
【输出格式】输出文件的第一行包含一个整数S,表示最少需要的操作次数。
接下来的S行每行分别表示每次操作后桌上硬币的状态(一行包含N个整数(0或1),表示每个硬币的状态:
0——正面向上,和1——反面向上,不允许出现多余空格)。
对于有多种操作方案的情况,则只需输出一种。
【样例输入】coin.in
4
【样例输出】coin.out
4
0111
1100
0001
1111
【参考程序】
varn,head,tail,turn0,turn1:
byte;
que:
array[word]ofrecordpre:
word;n0,turn0:
byte;end;
b:
array[0..100]ofboolean;
procedureinit;
begin
assign(input,'coin.in');reset(input);
readln(n);
close(input);
end;{init}
procedureprint;
vari,j,k:
byte;
status:
string[100];
a:
array[word]ofbyte;
begin
assign(output,'coin.out');rewrite(output);
writeln(n);
fori:
=1tondostatus[i]:
='0';
j:
=0;i:
=tail;
repeat
inc(j);
a[j]:
=i;
i:
=que[i].pre;
untili=0;
fori:
=j-1downto1do
begin
turn0:
=que[a[i]].turn0;
turn1:
=n-1-que[a[i]].turn0;
fork:
=1tondo
casestatus[k]of
'0':
ifturn0>0thenbeginstatus[k]:
='1';dec(turn0);end;
'1':
ifturn1>0thenbeginstatus[k]:
='0';dec(turn1);end;
end;
fork:
=ndownto1dowrite(status[k]);
writeln;
end;
close(output);
halt;
end;{print}
procedurework;
begin
fillchar(b,sizeof(b),true);b[n]:
=false;
que[1].pre:
=0;que[1].n0:
=n;que[1].turn0:
=0;
head:
=0;tail:
=1;
repeat
inc(head);
forturn0:
=0ton-1do
begin
turn1:
=n-1-turn0;
if(turn0<=que[head].n0)and(turn1<=n-que[head].n0)and(b[que[head].n0-turn0+turn1])then
begin
inc(tail);
b[que[head].n0-turn0+turn1]:
=false;
que[tail].pre:
=head;
que[tail].n0:
=que[head].n0-turn0+turn1;
que[tail].turn0:
=turn0;
ifque[tail].n0=0thenprint;
end;
end;
untilhead=tail;
end;{work}
begin{main}
init;
work;
end.
8、最少转弯问题(TURN.PAS)
【问题描述】
给出一张地图,这张地图被分为n×m(n,m<=100)个方块,任何一个方块不是平地就是高山。
平地可以通过,高山则不能。
现在你处在地图的(x1,y1)这块平地,问:
你至少需要拐几个弯才能到达目的地(x2,y2)?
你只能沿着水平和垂直方向的平地上行进,拐弯次数就等于行进方向的改变(从水平到垂直或从垂直到水平)的次数。
例如:
如图1,最少的拐弯次数为5。
(x1,y1)
【输入格式】
第1行:
n m
第2至n+1行:
整个地图地形描述(0:
空地;1:
高山),
如(图1)第2行地形描述为:
1000010
第3行地形描述为:
0010100
……
第n+2行:
x1y1x2y2(分别为起点、终点坐标)
【输出格式】
s(即最少的拐弯次数)
【输入输出样例】(见图1):
TURN.IN
TURN.OUT
57
1000010
0010100
0000101
0110000
0000110
1317
5
【参考程序】
varn,m,x1,y1,x2,y2:
byte;
head,tail:
word;
map:
array[0..101,0..101]of0..1;
que:
array[1..10000]ofrecordx,y,turns:
shortint;end;
procedureinit;
vari,j:
byte;
begin
assign(input,'turn.in');reset(input);
assign(output,'turn.out');rewrite(output);
fillchar(map,sizeof(map),1);
readln(n,m);
fori:
=1tondo
begin
forj:
=1tomdo
read(map[i,j]);
readln;
end;
readln(x1,y1,x2,y2);
if(x1=x2)and(y1=y2)thenbeginwriteln(0);close(output);halt;end;
close(input);
end;{init}
procedurego(newx,newy:
byte);
begin
inc(tail);
withque[tail]dobeginx:
=newx;y:
=newy;turns:
=que[head].turns+1;end;
if(newx=x2)and(newy=y2)thenbeginwriteln(que[tail].turns);close(output);halt;end;
map[newx,newy]:
=1;
end;{go}
procedurework;
vard:
byte;
begin
withque[1]dobeginx:
=x1;y:
=y1;turns:
=-1;end;
head:
=0;tail:
=1;
repeat
inc(head);
d:
=que[head].x-1;whilemap[d,que[head].y]=0dobegingo(d,que[head].y);dec(d);end;
d:
=que[head].x+1;whilemap[d,que[head].y]=0dobegingo(d,que[head].y);inc(d);end;
d:
=que[head].y-1;whilemap[que[head].x,d]=0dobegingo(que[head].x,d);dec(d);end;
d:
=que[head].y+1;whilemap[que[head].x,d]=0dobegingo(que[head].x,d);inc(d);end;
untilhead=tail;
end;{work}
begin{main}
init;
work;
end.
.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 广度 优先 搜索