标签:nyoj
这有一个迷宫,有0~8行和0~8列:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,1
0表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
2 3 1 5 7 3 1 6 7
12 11
01.
#include<iostream>
02.
#include<algorithm>
03.
using
namespace
std;
04.
int
r[9][9]={{1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,1},{1,0,0,1,1,0,0,0,1},{1,0,1,0,1,1,0,1,1},{1,0,0,0,0,1,0,0,1},{1,1,0,1,0,1,0,0,1},{1,1,0,1,0,1,0,0,1},{1,1,0,1,0,0,0,0,1},{
1,1,1,1,1,1,1,1,1}};
05.
int
n,a,b,c,d,m;
06.
void
dfs(
int
p,
int
q,
int
s)
07.
{
08.
if
(r[p][q])
return
;
09.
if
(p==c&&q==d)
10.
{
11.
m=min(s,m);
12.
return
;
13.
}
14.
s++;
15.
r[p][q]=1;
16.
dfs(p-1,q,s);
17.
dfs(p+1,q,s);
18.
dfs(p,q-1,s);
19.
dfs(p,q+1,s);
20.
r[p][q]=0;
21.
}
22.
int
main()
23.
{
24.
25.
cin>>n;
26.
while
(n--)
27.
{
28.
m=10000;
29.
cin>>a>>b>>c>>d;
30.
dfs(a,b,0);
31.
cout<<m<<endl;
32.
}
33.
return
0;
34.
}
标签:nyoj
原文地址:http://blog.csdn.net/justesss/article/details/38062889