（图文无关    雾

# BFS（广度优先搜索）

### P1451 求细胞数量

```struct node{
// 你要表示的状态，例如：坐标
}
node _node(/*参数*/){
node r;
//初始化
return r;
}
/*

struct node{
int x,y;
}
node _node(int x,int y){
node r;
r.x=x;
r.y=y;
return r;
}
*/
queue<node>q;
bool vis[...]...[...];//判重数组，保证每个状态只访问一次。
void bfs(node s/*起始元素*/){
q.push(s);
vis[...]...[...]=1;//初始状态纪录
while(!q.empty()){
q.pop();
for(...){//遍历所有能到达的状态
if(...new...&&!vis[...]...[...]){//新状态合法且没被访问过
q.push(new);
vis[...]...[...]=1;//新状态纪录。
}
}
}
}```

```int dx[4] = {-1, 0, 1, 0},
dy[4] = {0, 1, 0, -1};```

```#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
bool b[101][101];
int n, m, a[101][101], ans = 0;
int dx[4] = {-1, 0, 1, 0},
dy[4] = {0, 1, 0, -1};
inline void qaq(int x, int y)
{
int hx[1000], hy[1000], head = 1, tail = 1, tx, ty;
ans++;
hx[1] = x, hy[1] = y;
b[x][y] = false;
{
for (int i = 0; i <= 3; ++i)
{
if (tx > 0 && tx <= m && ty > 0 && ty <= n && b[tx][ty])
{
tail++;
hx[tail] = tx,
hy[tail] = ty;
b[tx][ty] = false;
}
}
}
}
int main()
{
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
b[i][j] = true;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
{
scanf("%1d", &a[i][j]);
if (!a[i][j])
b[i][j] = false;
}
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
if (b[i][j])
qaq(i, j);
printf("%d", ans);
return 0;
}```

T2

# P1162 填涂颜色

```#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n, a[31][31], dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}, X, Y;
bool b[31][31];
void qaq(int x, int y)
{
int q[900][2], head = 1, tail = 0;
tail++;
q[tail][1] = x;
q[tail][2] = y;
b[x][y] = 1;
{
for (int i = 0; i <= 3; ++i)
{
if (X > 0 && X <= n && Y > 0 && Y <= n && b[X][Y] == 0)
{
tail++;
q[tail][1] = X;
q[tail][2] = Y;
b[X][Y] = 1;
}
}
}
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
scanf("%d", &a[i][j]);
if (a[i][j])
b[i][j] = 1;
}
//printf("\n\n\n\n\n\n");
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (!a[i][j] && (i == 1 || j == 1 || i == n || j == n))//这里就是保证连通块必然有至少一个数字在边上
qaq(i, j);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (!b[i][j])
printf("2 ");
else
printf("%d ", a[i][j]);
}
printf("\n");
}
}```

```#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int n, m, sx, sy;
bool mat[501][501];
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
struct pos
{
int x, y, step;
pos(int x, int y, int step) : x(x), y(y), step(step) {}
};
queue<pos> q;
bool vis[501][501];
inline bool pan(int x, int y)//判断合法性
{
return x >= 1 && x <= n && y >= 1 && y <= m && mat[x][y] == 0;
}
bool out(int x, int y)//判断是否已到达迷宫边界，即可以离开迷宫
{
return (x == 1 || x == n || y == 1 || y == m) && mat[x][y] == 0;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> mat[i][j];
}
}
cin >> sx >> sy;
if(mat[sx][sy]){//如果初始位置是墙，则无解
cout<<-1;
return 0;
}
if(out(sx,sy)){//如果初始位置在边界，则直接离开
cout<<0;
return 0;
}
q.push(pos(sx, sy, 0));
vis[sx][sy] = 1;
while (!q.empty())
{
pos h = q.front();
q.pop();
// cout << h.x << ' ' << h.y << endl;
if (out(h.x, h.y))//如果到达出口
{
cout << h.step - 1;//因为原题求的是撞墙次数，而我们知道最后一次是不撞墙的
return 0;//直接退出，因为已求得最优解
}
for (int i = 0; i < 4; i++)
{
int xx = h.x, yy = h.y;
while (pan(xx + dx[i], yy + dy[i]))//如果没撞到墙，就继续走
{
xx += dx[i];
yy += dy[i];
}
if ((xx == h.x && yy == h.y) || vis[xx][yy])//如果并没有移动，或者最终位置已到达过
continue;
vis[xx][yy] = 1;
q.push(pos(xx, yy, h.step + 1));
}
}
cout << -1;//如果所有可能节点都已遍历，而始终没有到达边界，则无解
}```

# P1605 迷宫

迷宫的操作其实就是把滑的部分简化，这里直接用到连通块那个dx和dy就行

```#include <iostream>
#include <cstdio>
using namespace std;
int n, m, t, ex, ey, ans = 0, bx, by, tx, ty, X, Y;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
bool pos[6][6], map[6][6];
void qaq(int x, int y)
{
if (x == ex && y == ey)
{
ans++;
return;
}
else
{
for (int i = 0; i <= 3; ++i)
{
X = x + dx[i];
Y = y + dy[i];
if (!pos[X][Y] && map[X][Y] && X<=m && x>0 && y<=n && Y>0) //没被走过而且不是障碍
{
pos[x][y] = 1;
qaq(X, Y);
pos[x][y] = 0;
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &t);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
map[i][j] = true;
scanf("%d%d%d%d", &bx, &by, &ex, &ey);
for (int i = 1; i <= t; ++i)
{
scanf("%d%d", &tx, &ty);
map[tx][ty] = false; //障碍
}
qaq(bx, by);
printf("%d", ans);
return 0;
}
/*
5 5 0
1 1 2 2

4846
*/```

0条评论