You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.
Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).
You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?
Input
The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.
Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).
The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.
The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
Output
If you can reach the destination, print 'YES', otherwise print 'NO'.
//DFS,注意访问两次
#include<stdio.h>
#include<string.h>
char visited[505][505];
int n, m;
int ok;
int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int endi, endj;
int starti, startj;
void Dfs(int x,int y)
{
if (ok)
return;
for (int i = 0; i < 4; i++)
{
int a = x + dir[i][0];
int b = y + dir[i][1];
if (a < 0 || a > n || b < 0 || b > m)
continue;
if (a == endi&&b == endj&&visited[endi][endj] == 'X')
{
ok = 1;
return;
}
else if (visited[a][b] == '.')
{
visited[a][b] = 'X';
Dfs(a, b);
}
}
}
int main()
{
while (~scanf_s("%d%d", &n, &m))
{
memset(visited, 0, sizeof(visited));
int i, j;
for (i = 1; i <= n; i++)
{
getchar();
for (j = 1; j <= m; j++)
{
visited[i][j] = getchar();
}
}
scanf_s("%d%d", &starti, &startj);
scanf_s("%d%d", &endi, &endj);
ok = 0;
Dfs(starti, startj);
if (ok == 1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
BFS(常用)
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
int dir[4][2] = { { 0,1 },{ 1,0 },{ -1,0 },{ 0,-1 } };
char mat[505][505];
int n, m;
int sx, sy;//初始坐标
int ex, ey;//结束坐标
bool judge(int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m)
return true;
return false;
}
struct position
{
int x;
int y;
};
bool bfs() {
position p,temp;
p.x = sx;
p.y = sy;
queue<position>q;
q.push(p);
while (!q.empty()) {
int x = q.front().x;
int y = q.front().y;
q.pop();
for (int i = 0; i < 4; i++) {
temp.x = x + dir[i][0];
temp.y = y + dir[i][1];
if (judge(temp.x, temp.y)) {
if (mat[temp.x][temp.y] == 'X') {
if (temp.x == ex && temp.y == ey)
return true;
}
else {
mat[temp.x][temp.y] = 'X';
q.push(temp);
}
}
}
}
return false;
}
int main() {
while (~scanf_s("%d%d", &n, &m))
{
for (int i = 0; i < n; i++) {
getchar();
for (int j = 0; j < m; j++)
scanf_s("%c", &mat[i][j]);
}
scanf_s("%d%d%d%d", &sx, &sy, &ex, &ey);
sx--;
sy--;
ex--;
ey--;
if (bfs())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
第一个是用BFS写的,第二个是把BFS函数体放到了main函数里,第三个是DFS写的,可以看到DFS消耗的内存多
BFS:在树的层次较深或者子节点个数较多的情况下,消耗内存现象十分严重。适用于节点的子节点个数不多,并且树的层次不会太深的情况。优点:可以得到最优解。
DFS:缺点:难以寻找最优解,仅仅只能寻找有解。
一般说来,能用DFS解决的问题都能用BFS解决。DFS通过递归实现,易于实现,DFS的常数时间开销会比较少,所以大多数情况下优先考虑DFS实现。总的来说多数情况下运行BFS所需的内存会大于DFS需要的内存(DFS一次访问一条路,BFS一次访问多条路),DFS容易爆栈(栈不易”控制”),BFS通过控制队列可以很好解决”爆队列”风险。