5.8下午bfs进阶--东塘--蒋
liuyilun123 · · 个人记录
P4328 [COCI 2006/2007 #1] Slikar
错误原因:
没把记录洪水时间的数组设置成最大值 ,还在 bfs 函数里面漏判了 a[nx][ny] != 'X' 。
思路:
就是搞两个 bfs ,第一个是把洪水看做起的求每个点被洪水淹没的时间 。第二个就是正常的的求到海狸巢穴的广搜 。
代码:
#include<bits/stdc++.h>
using namespace std;
int n, m, qx, qy, zx, zy, dis[55][55];
char a[55][55];
bool vis[55][55];
struct Node
{
int x, y, t;
};
queue<Node> q1;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int bfs(int qx, int qy)
{
queue<Node> q;
memset(vis, 0, sizeof vis);
q.push({qx, qy, 0});
vis[qx][qy] = 1;
while(q.empty() == 0)
{
Node c = q.front();
q.pop();
if(c.x == zx && c.y == zy)
{
return c.t;
}
for(int i = 0; i < 4; i++)
{
int nx = c.x + dx[i], ny = c.y + dy[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == 0 && c.t < dis[nx][ny] - 1 && a[nx][ny] != 'X')
{
q.push({nx, ny, c.t + 1});
vis[nx][ny] = 1;
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] == 'S')
{
qx = i, qy = j;
}
else if(a[i][j] == 'D')
{
zx = i, zy = j;
}
if(a[i][j] == '*')
{
q1.push({i, j, 0});
vis[i][j] = 1;
}
}
}
memset(dis, 0x3f, sizeof dis);
while(q1.empty() == 0)
{
Node c = q1.front();
q1.pop();
dis[c.x][c.y] = c.t;
for(int i = 0; i < 4; i++)
{
int nx = c.x + dx[i], ny = c.y + dy[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == 0 && a[nx][ny] != 'X' && !(nx == zx && ny == zy))
{
q1.push({nx, ny, c.t + 1});
vis[nx][ny] = 1;
}
}
}
dis[zx][zy] = 1e9;
int o = bfs(qx, qy);
if(o == -1)
{
cout << "KAKTUS";
}
else
{
cout << o;
}
return 0;
}
P2199 最后的迷宫
错误原因:
太复杂了 。
思路:
就是把终点变成起点的多起点广搜 ,先把所有起点都做标记 ,在进行广搜 。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 16500 + 5;
int n, m, qx, qy, zx, zy;
string a[N];
vector<bool> vis[N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int dx2[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy2[] = {1, -1, 0, 1, -1, 0, 1, -1};
struct Node
{
int x, y, bu;
};
queue<Node> q;
int bfs()
{
while(q.empty() == 0)
{
Node c = q.front();
q.pop();
if(c.x == qx && c.y == qy)
{
return c.bu;
}
for(int i = 0; i < 4; i++)
{
int nx = c.x + dx[i], ny = c.y + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] != 'X' && vis[nx][ny] == 0)
{
q.push({nx, ny, c.bu + 1});
vis[nx][ny] = 1;
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
while(cin >> zx >> zy >> qx >> qy)
{
if(qx == 0 && qy == 0 && zx == 0 && zy == 0)
{
return 0;
}
while(q.empty() == 0)
{
q.pop();
}
qx--, qy--, zx--, zy--;
for(int i = 0; i < n; i++)
{
vis[i].clear();
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
vis[i].push_back(0);
}
}
q.push({zx, zy, 0});
vis[zx][zy] = 1;
for(int i = 0; i < 8; i++)
{
int nx = zx + dx2[i], ny = zy + dy2[i];
while(nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if(a[nx][ny] != 'X')
{
vis[nx][ny] = 1;
q.push({nx, ny, 0});
nx += dx2[i], ny += dy2[i];
}
else
{
break;
}
}
}
int o = bfs();
if(o == -1)
{
cout << "Poor Harry\n";
}
else
{
cout << o << "\n";
}
}
return 0;
}
P1649 [USACO07OCT] Obstacle Course S
错误原因:
不会 。
思路:
就是我们可以从起点把没个方向都走一遍 ,在 bfs里面只需要看下一步的方向是否统一 ,如果不一样次数就加一 ,做否判断如果不越界也不是墙且次数小与 vis[nx][ny] 就让他入队 ,vis[nx][ny] 等于次数 。
代码:
#include<bits/stdc++.h>
using namespace std;
int n, qx, qy, zx, zy;
struct Node
{
int x, y, Dx, Dy, ci;
};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int vis[105][105];
char a[105][105];
void bfs(int qx, int qy, int xt, int yt)
{
queue<Node> q;
q.push({qx, qy, xt, yt, 0});
vis[qx][qy] = 0;
while(q.empty() == 0)
{
Node c = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
int nx = c.x + dx[i], ny = c.y + dy[i], t = c.ci;
if(c.Dx != dx[i] || c.Dy != dy[i])
{
t++;
}
if(nx < 1 || nx > n || ny < 1 || ny > n || a[nx][ny] == 'x')
{
continue;
}
if(t < vis[nx][ny])
{
vis[nx][ny] = t;
q.push({nx, ny, dx[i], dy[i], t});
}
}
}
return ;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
if(a[i][j] == 'B')
{
zx = i;
zy = j;
}
if(a[i][j] == 'A')
{
qx = i;
qy = j;
}
vis[i][j] = 1e9;
}
}
for(int i = 0; i < 4; i++)
{
bfs(qx, qy, dx[i], dy[i]);
}
if(vis[zx][zy] == 1e9)
{
cout << -1;
}
else
{
cout << vis[zx][zy];
}
return 0;
}