# BFS Examples with Notes

Title Description
The princess was captured by the wicked and held somewhere in her cell. Cells are represented by a matrix of N*M (N, M <= 20). Each item in the matrix can represent roads (@), walls (#), and guards (x).
The heroic Knight (r) decided to save the princess by himself (a). We assume that the successful rescue means that the knight has reached the princess's place. As a guard may be encountered along the way to the princess's position, once the knight encounters the guard, he must kill the guard before he can move on.
It is now assumed that a knight can move up, down, left, and right in four directions, one unit time for each position he moves and one extra unit time for killing a guard. Also assume that the knight is strong enough to kill all the guards.
Given the cell matrix, where the princess, knight and guard are located in the matrix, you can calculate how quickly it will take the rescue operation to succeed.
Input Format
The first line has two integers representing N and M, (N, M <= 20).
Then N lines, each with M characters. '@'stands for the road,'a' for the princess,'r'for the knight,'x' for the guard,'#'for the wall.
Output Format
If the rescue operation succeeds, output an integer indicating the minimum time for the operation.
If unsuccessful, output "Impossible"
Input Sample 1

```#@#####@
#@a#@@r@
#@@#x@@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@
```

Output Sample 1
13
Input Sample 2

```@x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x
xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@
#@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x
@##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@#
@#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@
#xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x#####
#x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x
xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x
x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@
#x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x
x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@
#@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x
#x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@
```

Output Sample 2
7

# thinking

I wanted to use dfs for this question, but wa got the result because it timed out because dfs traversed all the possibilities and took a lot of time, so I changed the method and chose bfs to solve it.
Note that what we need is not the shortest path but the shortest time.
So we can open an array to record the shortest time from the start point to each point and the shortest time to output the destination.
Here is the code with comments attached

```#include <bits/stdc++.h>
#include <algorithm>
#include <queue>
#define ll long long
#define mem(s, i) memset(s, i, sizeof(s))
#define INF 0x3f3f3f3f;
using namespace std;
int n, m;
char p[205][205];
int vim[205][205];
int a, b;
int d[2][4] = {{0, 0, 1, -1}, {1, -1, 0, 0}};
struct node
{
int x, y; //Point coordinates
int len;  //Distance from the starting point to that point
bool check(int x, int y){//Determine if the point is within the map and not a wall
if(x >= 1&&x <= n&&y >= 1&&y <= m&&p[x][y]!='#')return true;
return false;
}
void bfs(int xx, int yy, int l)
{
queue<node> q;
vim[xx][yy] = 1;//The point is initially all 1
while(!q.empty()){
tail = q.front();
q.pop();
/*The head we use in this loop is the previous head, which is the head of q at the beginning of each while loop
head Element, and the head within the loop will be re-queued for subsequent while loops until the queue is empty and I
Every time we bfs traversed to his nearest location*/
for(int i = 0;i < 4;i++){
}
else{
}
//If the point has been calculated and is farther away than previously recorded, we will skip it
continue;
}
}
}
}
}
void solve()
{
cin >> n >> m;
int x, y;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> p[i][j];
if (p[i][j] == 'r')
x = i, y = j; // qidian
if (p[i][j] == 'a')
a = i, b = j; // zhongdian
}
}
bfs(x, y, 0);//Start time we start at 0
if (vim[a][b])cout<<vim[a][b]<<endl;
else cout << "Impossible" << endl;
}
int main()
{
ios::sync_with_stdio(false);
solve();
return 0;
}
```

Tags: C++ bfs

Posted on Sun, 07 Nov 2021 11:19:46 -0500 by gateway69