5.8总结

· · 个人记录

P4328 [COCI 2006/2007 #1] Slikar

思路:
1.我们发现,有两个要动,洪水和画家和三只小刺猬。
2.所以本题要写2个bfs,这样问题就简单了。
3.1个bfs记录记录洪水扩延到每个点的时间,另一个记录画家和三只小刺猬到海狸的巢穴的时间。
void bfs()
{
    queue<stu>op;
    memset(vis, 0, sizeof vis);
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            if(a[i][j] == '*')
            {
                sum[i][j] = 0;
                op.push({i, j, 0});
            }
        }
    }
    while(op.empty())
    {
        stu cur =op.front();
        op.pop();
        sum[cur.x][cur.y] = cur.step;
        for(int i = 0; i < 4; i ++)
        {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if(nx <= 0 || ny <= 0 || nx > n || ny > m)
                continue;
            if(a[nx][ny] == 'X' || a[nx][ny] == 'D')
                continue;
            if(vis[nx][ny] == 1)
                continue;
            vis[nx][ny] = 1;
            op.push({nx, ny, cur.step + 1});
        }
    }
}
int bfs2(int sx, int sy)
{
    queue<stu>op;
    memset(vis, 0, sizeof vis);
    op.push({sx, sy, 0});
    vis[sx][sy] = 1;
    while(op.empty())
    {
        stu cur = op.front();
        op.pop();
        if(a[cur.x][cur.y] == 'D')
            return cur.step;
        for(int i = 0; i < 4; i ++)
        {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if(nx <= 0 || ny <= 0 || nx > n || ny > m)
                continue;
            if(a[nx][ny] == 'X')
                continue;
            if(sum[nx][ny] <= cur.step + 1)
                continue;
            if(vis[nx][ny] == 1)
                continue;
            vis[nx][ny] = 1;
            op.push({nx, ny, cur.step + 1});
        }
    }
    return -1;
}

P2199 最后的迷宫

思路:
1.如果能在x,y发现奖杯,那么从x,y出发到奖杯的时间=dis[x][y]
2.所以我们只需要要找所有能直接看到奖杯的位置,然后从中最小的就行。
3.那我们该如何搜索呢,首先从起点出发,向外面搜,如果发现空地,就记录步数。
4.然后我们从奖杯同样向外面搜索,收集所有能发现奖杯的空地然后去步数最小值就可以了。
bool check(int x, int y)
{
    return x >= 0 && x < n && y >= 0 && y < m && s[x][y] == 'O';
}
void bfs(int sx, int sy)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            dis[i][j] = INF;
    op.push(stu{sx, sy});
    dis[sx][sy] = 0;
    while (!op.empty())
    {
        int x = op.front().x;
        int y = op.front().y;
        op.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (check(nx, ny) && dis[nx][ny] == INF)
            {
                dis[nx][ny] = dis[x][y] + 1;
                op.push({nx, ny});
            }
        }
    }
}

P1649 [USACO07OCT] Obstacle Course S

1.本题有1个与众不同的点,他是要求转弯数,所以我们要用分层bfs。
2.我们的dis就完全不同了,以前的dis是步数,而现在的是转弯数。
3.我们每次入队1个新格子,如果方向没变,那么转弯次数不变,如果方向变了,那么转弯次数就要+1了。
int bfs()
{
    queue<stu>op;
    vis[sx][sy]=1;
    op.push({sx,sy,0});
    while(op.size())
    {
        stu zzh=op.front();
        op.pop();
        for(int i=0;i<4;++i)
        {
            int xx=zzh.x+dx[i];
            int yy=zzh.y+dy[i];
            while(check(xx,yy))
            {
                if(vis[xx][yy]==0)
                {
                    if(xx==ex && yy==ey)
                    {
                        return zzh.test;
                    }
                    vis[xx][yy]=1;
                    op.push({xx,yy,zzh.test+1});
                }
                xx+=dx[i];
                yy+=dy[i];
            }
        }
    }
    return -1;
}