思考过程:三维数组???
BFS(广度优先遍历)的扩展。先写一个二维的练练手
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 100;
struct Node {
int x, y;
int step;
}S,T,node;
int n, m;
char maze[maxn][maxn];
bool inq[maxn][maxn] = { false };
int x[4] = { 0,0,1,-1 };
int y[4] = { 1,-1,0,0 };
bool test(int x, int y) {
if (x > n || x<0 || y>m || y < 0)
return false;
if (maze[x][y] == '*')
return false;
if (inq[x][y] == true) //如果已经走过
return false;
return true;
};
int BFS() {
queue<Node> q;
S.step = 0;
inq[S.x][S.y] = true;
q.push(S);
while (!q.empty()) {
Node top = q.front();
q.pop();
if (top.x == T.x && top.y == T.y)
return top.step;
for (int i = 0; i < 4; i++) {
int newX = top.x + x[i];
int newY = top.y + y[i];
if (test(newX, newY)) {
node.x = newX;
node.y = newY;
node.step = top.step + 1;
q.push(node);
inq[newX][newY] = true;
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
getchar();
for (int j = 0; j < m; ++j)
{
maze[i][j] = getchar();
}
}
cin >> S.x >> S.y >> T.x >> T.y;
printf("%d\n", BFS());
return 0;
}
5 5
.....
.*.*.
.*S*.
.***.
...T*
2 2 4 3
11
理解BFS的一个好图 -- 我的简单理解队列 就是将上下左右依次放入队列,出的的时候也是依次的 ,每个方向的扩展都是不相打扰的
DBFS就是双向广度搜索,即从终点和起点都开始BFS,他们两个相遇的时候就行了。但是因为双向会出现多种相遇的情况,这时不能直接输出,而是检查完该层所有的节点以得到最优解,即交替逐层搜索。也即如果这层搜索遇到了另一个已经访问过的,那么已经搜索过的层次就是答案了,可以跳出来了,以后不会出现更优的了,而当某一边队列空时就无解了。
HDOJ-1372
简述:棋盘横坐标a~h,纵坐标1~8,马走日,问起点到终点的最小步数
//DBF
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct position {
int x;
int y;
};
char c[6];
int a[6];
position dir[8] = { {-2,1},{-2,-1},{1,-2},{-1,-2},{2,1},{2,-1},{1,2},{-1,2} };
queue<int> que;
int vis[9][9];
int ans;
bool is_right(int x, int y) {
if (x > 0 && x < 8 && y>0 && y < 8)
return true;
return false;
};
int BFS() {
int row, col, i;
ans = 0;
que.push(a[0]);
que.push(a[1]);
que.push(ans);
vis[a[0]][a[1]] = 1;
while (!que.empty()) {
row = que.front(); que.pop();
col = que.front(); que.pop();
ans = que.front(); que.pop();
if (row == a[2] && col== a[3])
return ans;
for (i = 0; i < 8; i++) {
if (is_right(row + dir[i].x, col + dir[i].y) && !vis[row + dir[i].x][col + dir[i].y]) {
que.push(row + dir[i].x);
que.push(col + dir[i].y);
que.push(ans + 1);
vis[row + dir[i].x][col + dir[i].y] = 1;
}
}
}
return 0;
}
int main()
{
while (gets_s(c)) {
while (!que.empty())
que.pop();
memset(vis, 0, sizeof(vis));
a[0] = c[0] - 'a' + 1;
a[1] = c[1] - '0';
a[2] = c[3] - 'a' + 1;
a[3] = c[4] - '0';
ans = BFS();
cout << ans<<endl;
}
return 0;
}
http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx
https://blog.csdn.net/qq_41759198/article/details/81510147
//DBFS
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct position {
int x;
int y;
};
char c[6];
int a[6];
position dir[8] = { {-2,1},{-2,-1},{1,-2},{-1,-2},{2,1},{2,-1},{1,2},{-1,2} };
queue<int> que1,que2;
int vis[9][9];
int ans,ans1,ans2,cnt;
bool flag;
bool is_right(int x, int y) {
if (x > 0 && x < 8 && y>0 && y < 8)
return true;
return false;
};
int From_Head_BFS() {
int row, col, i,step;
row = que1.front(); que1.pop();
col = que1.front(); que1.pop();
step = que1.front(); que1.pop();
for (i = 0; i < 8; i++) {
if (is_right(row + dir[i].x, col + dir[i].y) && vis[row + dir[i].x][col + dir[i].y]!=1) {
ans1 = step + 1;
if (vis[row + dir[i].x][col + dir[i].y] == 2) {
flag = false;
ans = ans1 + ans2;
return 0;
}
que1.push(row + dir[i].x);
que1.push(col + dir[i].y);
que1.push(ans1);
vis[row + dir[i].x][col + dir[i].y] = 1;
}
}
return 0;
}
int From_Tail_BFS() {
int row, col, i,step;
row = que2.front(); que2.pop();
col = que2.front(); que2.pop();
step = que2.front(); que2.pop();
for (i = 0; i < 8; i++) {
if (is_right(row + dir[i].x, col + dir[i].y) && vis[row + dir[i].x][col + dir[i].y] != 1) {
ans2 = step + 1;
if (vis[row + dir[i].x][col + dir[i].y] == 1) {
flag = false;
ans = ans1 + ans2;
return 0;
}
que2.push(row + dir[i].x);
que2.push(col + dir[i].y);
que2.push(ans2);
vis[row + dir[i].x][col + dir[i].y] = 2;
}
}
return 0;
}
int main()
{
while (gets_s(c)) {
while (!que1.empty())
que1.pop();
while (!que2.empty())
que2.pop();
memset(vis, 0, sizeof(vis));
a[0] = c[0] - 'a' + 1;
a[1] = c[1] - '0';
a[2] = c[3] - 'a' + 1;
a[3] = c[4] - '0';
ans = ans1 = ans2 = 0;
int step = 0;
flag = true;
que1.push(a[0]);
que1.push(a[1]);
que1.push(step);
vis[a[0]][a[1]] = 1;
que2.push(a[2]);
que2.push(a[3]);
que2.push(step);
vis[a[2]][a[3]] = 2;
if (a[0] == a[2] && a[1] == a[3])
return ans;
else {
while (flag) {
cnt = que1.size();//该层的节点数,逐层执行
while (cnt-- && flag)
From_Head_BFS();
cnt = que2.size();
while (cnt-- && flag) {
From_Head_BFS();
}
}
cout << ans << endl;
}
}
return 0;
}
HDOJ-1728
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
const int maxz = 100;
int m, n;
char a[maxz][maxz];
int used[maxz][maxz];
int k, sy, sx, ex, ey;
struct node {
int x;
int y;
};
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int judge(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '*';
}
void BFS(int x,int y) {
queue<node> q;
node S,next;
S.x = x;
S.y = y;
q.push(S);
bool flag = false;
while (!q.empty()) {
node now = q.front();
q.pop();
if (now.x == ex && now.y == ey) {
if (used[ex][ey] <= k&&used[ex][ey]!=-1) {
flag = true;
printf("yes");
}
else
break;
}
for (int i = 0; i < 4; i++) {
next.x = now.x + dir[i][0];;
next.y = now.y + dir[i][1];;
while(judge(next.x, next.y)) {
if (used[next.x][next.y] == -1) {
used[next.x][next.y] = used[now.x][now.y] + 1;
q.push(next);
}
//一直往一个一个方向走,走到尽头
next.x += dir[i][0];
next.y += dir[i][1];
}
}
}
if (!flag)
printf("no\n");
};
int main() {
int t;
cin >> t;
while (t--) {
cin >> m >> n;
memset(used, -1, sizeof(used));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cin >> k >> sy >> sx >> ey >> ex;
sy--, sx--, ex--, ey--;
BFS(sx,sy);
}
}