遇到该类问题我们的第一思路都是DFS/BFS
1.DFS
首先,如果使用的是DFS,我们要明确该题“地图”中的每一个点要走的路线 实际上都是已经确定的,我们只要模拟每一个点的路线即可(一条路走到底),注意判断return的条件,return有两个条件,其中一个是该点的下一个点已经越界了,另一个条件是“在迷宫里兜圈子”即该点是右但下一步是左| |该点是下但下一步是上| |该点是左但下一步是右| |该点是上但下一步是下;
记得在return之前将成功出迷宫的人数记录下来哦,(也可顺便把没出迷宫的人记下,可以在最后输出的时候通过判断二者相加==100来判断结果的可靠性)
我还做了个小优化,就是使用另外一个数组来记录走到哪一个点是确定可以“逃出迷宫”和“困在迷宫”的,所以我们在判断下一步怎么样的时候可以直接判断是否“逃出迷宫”和“困在迷宫”
接下来就是代码实现了(代码比较长,但易于理解)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
const int MAXN = 10;
char MAP[MAXN][MAXN];
int book[MAXN][MAXN] = { 0 };
int tx, ty,vic=0,loser=0;
int nextt[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
void dfs(int x, int y)
{
if (MAP[x][y] == 'U')//遇到上
{
tx = x + nextt[3][0];//上面那一步
ty = y + nextt[3][1];
if (book[tx][ty] == 1)//小优化
{
loser++;
return;
}
if (book[tx][ty] == 2)//小优化
{
vic++;
return;//记得return
}
if (MAP[tx][ty] == 'D')//兜圈子
{
book[x][y] = 1;
loser++;
return;//记得return
}
if (tx < 0 || ty < 0 || tx >= 10 || ty >= 10)//逃出迷宫
{
book[x][y] = 2;
vic++;
return;
}
dfs(tx, ty);//如果以上条件不满足就走向下一步
}
else if (MAP[x][y] == 'D')//以下内容与上面大同小异便不再赘述
{
tx = x + nextt[1][0];
ty = y + nextt[1][1];
if (book[tx][ty] == 1)
{
loser++;
return;
}
if (book[tx][ty] == 2)
{
vic++;
return;
}
if (MAP[tx][ty] == 'U')
{
book[x][y] = 1;
loser++;
return;
}
if (tx < 0 || ty < 0 || tx >= 10 || ty >= 10)
{
book[x][y] = 2;
vic++;
return;
}
dfs(tx, ty);
}
else if (MAP[x][y] == 'R')
{
tx = x + nextt[0][0];
ty = y + nextt[0][1];
if (book[tx][ty] == 1)
{
loser++;
return;
}
if (book[tx][ty] == 2)
{
vic++;
return;
}
if (MAP[tx][ty] == 'L')
{
book[x][y] = 1;
loser++;
return;
}
if (tx < 0 || ty < 0 || tx >= 10 || ty >= 10)
{
book[x][y] = 2;
vic++;
return;
}
dfs(tx, ty);
}
else if (MAP[x][y] == 'L')
{
tx = x + nextt[2][0];
ty = y + nextt[2][1];
if (book[tx][ty] == 1)
{
loser++;
return;
}
if (book[tx][ty] == 2)
{
vic++;
return;
}
if (MAP[tx][ty] == 'R')
{
book[x][y] = 1;
loser++;
return;
}
if (tx < 0 || ty < 0 || tx >= 10 || ty >= 10)
{
book[x][y] = 2;
vic++;
return;
}
dfs(tx, ty);
}
}
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cin >> MAP[i][j];
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
dfs(i, j);//搜索每一个点代表了那条路
}
}
cout << vic << ' ' << loser << endl;
return 0;
}
2.BFS
对BFS而言,我所采用的思路与上面BFS的思路是一样的,如果上面看得懂下面也应该看得懂
这里便不再赘述
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
const int MAXN = 10;
char MAP[MAXN][MAXN];
int book[MAXN][MAXN] = { 0 };
int tx, ty, vic = 0, loser = 0;
int nextt[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
queue<pair<int, int> >q;
void bfs(int x, int y)
{
pair<int, int>a,now,next;
a.first = x;
a.second = y;
q.push(a);
while (!q.empty())
{
now = q.front();
q.pop();
if (MAP[now.first][now.second] == 'U')
{
next.first = now.first + nextt[3][0];
next.second = now.second + nextt[3][1];
if (book[next.first][next.second] == 1)
{
loser++;
break;
}
if (book[next.first][next.second] == 2)
{
vic++;
break;
}
if (MAP[next.first][next.second] == 'D')
{
book[now.first][now.second] = 1;
loser++;
break;
}
if (next.first < 0 || next.second < 0 || next.first >= 10 || next.second >= 10)
{
book[now.first][now.second] = 2;
vic++;
break;
}
q.push(next);
}
else if (MAP[now.first][now.second] == 'D')
{
next.first = now.first + nextt[1][0];
next.second = now.second + nextt[1][1];
if (book[next.first][next.second] == 1)
{
loser++;
break;
}
if (book[next.first][next.second] == 2)
{
vic++;
break;
}
if (MAP[next.first][next.second] == 'U')
{
book[now.first][now.second] = 1;
loser++;
break;
}
if (next.first < 0 || next.second < 0 || next.first >= 10 || next.second >= 10)
{
book[now.first][now.second] = 2;
vic++;
break;
}
q.push(next);
}
else if (MAP[now.first][now.second] == 'R')
{
next.first = now.first + nextt[0][0];
next.second = now.second + nextt[0][1];
if (book[next.first][next.second] == 1)
{
loser++;
break;
}
if (book[next.first][next.second] == 2)
{
vic++;
break;
}
if (MAP[next.first][next.second] == 'L')
{
book[now.first][now.second] = 1;
loser++;
break;
}
if (next.first < 0 || next.second < 0 || next.first >= 10 || next.second >= 10)
{
book[now.first][now.second] = 2;
vic++;
break;
}
q.push(next);
}
else if (MAP[now.first][now.second] == 'L')
{
next.first = now.first + nextt[2][0];
next.second = now.second + nextt[2][1];
if (book[next.first][next.second] == 1)
{
loser++;
break;
}
if (book[next.first][next.second] == 2)
{
vic++;
break;
}
if (MAP[next.first][next.second] == 'R')
{
book[now.first][now.second] = 1;
loser++;
break;
}
if (next.first < 0 || next.second < 0 || next.first >= 10 || next.second >= 10)
{
book[now.first][now.second] = 2;
vic++;
break;
}
q.push(next);
}
}
}
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cin >> MAP[i][j];
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
bfs(i, j);
}
}
cout << vic << ' ' << loser << endl;
return 0;
}