bfs
The general format of broad search:
1. Define a weighted array st
2. Queue initialization
3.while (queue is not empty)
- Remove elements from the head of the team
- Expand and connect the elements that meet the conditions and join the queue
. 4. You can use the queue provided by C ++, but it is not complicated to define a structure array to simulate the queue.
A bouquet dedicated to Algion
Code 1-Using queue:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define x first
#define y second
typedef pair<int ,int> PII;
const int N = 210;
int r,c;
char maz[N][N];
int cnt[N][N];
bool st[N][N];
int bfs(PII start , PII end)
{
queue<PII> q;
int d[4] = {0 , 0, 1 ,-1} , f[4] = {1 , -1 , 0 , 0};
memset(cnt , 0 ,sizeof cnt);
memset(st , 0 , sizeof st);
q.push(start); st[start.x][start.y] = true;
while(q.size())
{
PII t = q.front();
q.pop();
for(int i = 0 ; i < 4; i++)
{
int x = t.x + d[i], y = t.y + f[i];
//边界判断
if(x < 0 || x >= r || y < 0 || y >= c) continue;
//如果已经访问或者是墙
if(st[x][y] || maz[x][y] == '#') continue;
cnt[x][y] = cnt[t.x][t.y] + 1;
st[x][y] = true;
if(x == end.x && y == end.y) return cnt[x][y];
q.push({x,y});
}
}
return -1;
}
int main(void)
{
int t;
cin >> t;
PII start, end;
while(t--)
{
cin >> r >> c;
for(int i = 0; i < r; i++) scanf("%s" ,maz[i]);
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
if(maz[i][j] == 'E') start = {i , j};
else if(maz[i][j] == 'S') end = {i , j};
int ans = bfs(start , end);
if(ans != -1) printf("%d\n",ans);
else printf("oop!\n");
}
}
Code 2-Simulated queue
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define x first
#define y second
typedef pair<int ,int> PII;
const int N = 210;
int r,c;
char maz[N][N];
int cnt[N][N];
bool st[N][N];
int bfs(PII start , PII end)
{
//如果不每一次重新定义queue,那么上一次的会残留
PII q[r * c];
int d[4] = {0 , 0, 1 ,-1} , f[4] = {1 , -1 , 0 , 0};
memset(cnt , 0 ,sizeof cnt);
memset(st , 0 , sizeof st);
int hh = 0,tt = 0;
q[hh] = start;
st[start.x][start.y] = true;
while(hh <= tt)
{
//pop的返回值是void ,所以需要使用front
PII t = q[hh++];
for(int i = 0 ; i < 4; i++)
{
int x = t.x + d[i], y = t.y + f[i];
//边界检查
if(x < 0 || x >= r || y < 0 || y >= c) continue;
//是否已经访问,是否为墙
if(st[x][y] || maz[x][y] == '#') continue;
cnt[x][y] = cnt[t.x][t.y] + 1;
st[x][y] = true;
if(x == end.x && y == end.y) return cnt[x][y];
q[++tt] = {x, y};
}
}
return -1;
}
int main(void)
{
int t;
cin >> t;
PII start, end;
while(t--)
{
cin >> r >> c;
for(int i = 0; i < r; i++) scanf("%s" ,maz[i]);
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
if(maz[i][j] == 'E') start = {i , j};
else if(maz[i][j] == 'S') end = {i , j};
int ans = bfs(start , end);
if(ans != -1) printf("%d\n",ans);
else printf("oop!\n");
}
}
dfs
In general, deep search can find a solution, while wide search will traverse all the solutions. For example, when the shortest path search width with a better (but may be found deep trouble)
wide search stack will not burst, but it may be found deep
, but deep search code is relatively simple
global warming
Code 1 -dfs
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1010;
int d[4] = {-1 , 0 , 1 , 0} , f[4] = {0 , 1 , 0 , -1};
char g[N][N];
bool st[N][N];
int n;
int ch;
void dfs(int x ,int y)
{
g[x][y] = '!';
bool is = true;
for(int i = 0 ; i < 4; i++)
{
int a = x + d[i] , b = y + f[i];
if(a < 0 || a >= n || b < 0 || b >= n) continue;
if(g[a][b] == '.') is = false;
if(g[a][b] == '#') dfs(a , b);
}
if(is) ch = 1;
}
int main(void)
{
cin >> n;
for(int i = 0; i < n; i++) scanf("%s" , g[i]);
bool is;
int cnt = 0;
for(int i = 0; i < n ; i++)
for(int j = 0; j < n; j++)
if(g[i][j] == '#')
{
dfs(i , j);
if(!ch) cnt++;
ch = 0;
}
cout << cnt << endl;
return 0;
}
Code 2 -bfs
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n;
char g[N][N];
bool st[N][N];
PII q[N * N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void bfs(int sx, int sy, int &total, int &bound)
{
int hh = 0, tt = 0;
q[0] = {sx, sy};
st[sx][sy] = true;
while (hh <= tt)
{
PII t = q[hh ++ ];
total ++ ;
bool is_bound = false;
for (int i = 0; i < 4; i ++ )
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= n) continue; // 出界
if (st[x][y]) continue;
if (g[x][y] == '.')
{
is_bound = true;
continue;
}
q[ ++ tt] = {x, y};
st[x][y] = true;
}
if (is_bound) bound ++ ;
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
int cnt = 0;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
if (!st[i][j] && g[i][j] == '#')
{
int total = 0, bound = 0;
bfs(i, j, total, bound);
if (total == bound) cnt ++ ;
}
printf("%d\n", cnt);
return 0;
}