時間限制:3000 ms | 記憶體限制:65535 kb
難度:4
輸入第一行輸入乙個整數n(0輸出
輸出最少走幾步。
樣例輸入
2
3 1 5 7
3 1 6 7
樣例輸出
12
11
**
[苗棟棟]原創
上傳者苗棟棟
描述這有乙個迷宮,有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)。)
思路:隨緣搜尋!
1)【bfs】:
#include#include#includeusing namespace std;
const int maxn = 10;
int vis[maxn][maxn];
int a[9][9]=;
int x1,y11,x2,y2,ans;
int dir[4][2]=;
struct node
};bool check(int x,int y)
int bfs(int x,int y)
for(int i=0;i<4;i++)}}
if(ans==0)
cout<<0<>t;
while(t--)
return 0;
}
2) 【dfs】:
#include#include#includeusing namespace std;
const int maxn = 10;
int a[9][9]=;
int x1,y11,x2,y2,ans,sum;
int dir[4][2]=;
void dfs(int x,int y,int ans)
return 0;
}
NY 題目58 最少步數 BFS
時間限制 3000 ms 記憶體限制 65535 kb 難度 4 描述 這有乙個迷宮,有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,...
NYOJ 58 最少步數
時間限制 3000 ms 記憶體限制 65535 kb 難度 4 描述 這有乙個迷宮,有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,...
nyoj 58 最少步數
時間限制 3000 ms 記憶體限制 65535 kb 難度 4 描述 這有乙個迷宮,有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,...