POJ 2225 && HDU 1240 Asteroids!(bfs)

470 篇文章 3 订阅
125 篇文章 0 订阅

Description
给定一个三维的迷宫,已知起点和终点,问是否能从起点到达终点,并且最短的距离又是多少
Input
第一行为START n,n为三维迷宫的边长,之后为n个矩阵,每个矩阵为一层迷宫,矩阵顺序按层数从下到上,之后两行每行三个整数分别表示起点和终点的横纵竖坐标,然后以END结束一组用例的输入,以文件尾结束全部输入
Output
对于每组用例,若能从起点到达终点则输出最短距离,否则输出NO ROUTE
Sample Input
START 1
o
0 0 0
0 0 0
END
START 3
XXX
XXX
XXX
ooo
ooo
ooo
XXX
XXX
XXX
0 0 1
2 2 1
END
START 5
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
ooooo
0 0 0
4 4 4
END
Sample Output
1 0
3 4
NO ROUTE
Solution
三维迷宫,简单dfs
Code

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct data
{
    int x,y,z;
    bool operator==(const data &ne)const
    {
        return x==ne.x&&y==ne.y&&z==ne.z;
    }
    data(){}
    data(int _x,int _y,int _z){x=_x;y=_y;z=_z;}
};
int mark[15][15][15];
char map[15][15][15];//第一维为竖坐标,第二维为纵坐标,第三维为横坐标 
int dr[][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};//上下左右前后六个方向 
int main()
{
    char s[10];
    while(scanf("%s",s)!=EOF)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)//输入迷宫 
            for(int j=0;j<n;j++)
                scanf("%s",map[i][j]);
        data a,t;
        scanf("%d%d%d",&a.z,&a.y,&a.x);//起点坐标,注意顺序 
        scanf("%d%d%d",&t.z,&t.y,&t.x);//终点坐标,注意顺序 
        queue<data> Q;
        memset(mark,-1,sizeof(mark));//初始化 
        mark[a.x][a.y][a.z]=0;//记录步数 
        Q.push(a);//将起点压入队列 
        scanf("%s",s);//输入END 
        bool flag=false;//标志变量 
        while(!Q.empty())
        {
            a=Q.front();//出列 
            Q.pop(); 
            if(a==t)//到达终点 
            {
                flag=true;
                break;
            }
            int tp=mark[a.x][a.y][a.z];
            for(int i=0;i<6;i++)//六个方向枚举 
            {
                data b(a.x+dr[i][0],a.y+dr[i][1],a.z+dr[i][2]);
                if(b.x<0||b.x>=n||b.y<0||b.y>=n||b.z<0||b.z>=n||mark[b.x][b.y][b.z]!=-1||map[b.x][b.y][b.z]=='X')
                    continue;
                mark[b.x][b.y][b.z]=tp+1;//步数加一 
                Q.push(b);//压入队列 
            }
        }
        if(flag)//能够到达终点 
            printf("%d %d\n",n,mark[t.x][t.y][t.z]);
        else//不能到达终点 
            printf("NO ROUTE\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个 $N \times M$ 的矩阵,其中 "." 表示水洼,"W" 表示水。请计算有多少个水洼。 解题思路: 这是一道非常经典的搜索题目。我们可以使用 DFS 或 BFS 进行求解。 首先,我们需要遍历整个矩阵,当我们遇到一个 "." 时,我们就从该点开始向四周搜索,将所有相邻的 "." 变为 "W" ,并继续向下搜索。每次搜索完毕后,我们就可以找到一个完整的水洼,计数器加一。最后,当我们遍历完整个矩阵后,就可以得到所有的水洼数量。 代码实现: 使用 DFS 进行搜索: ```c++ #include <iostream> using namespace std; const int maxn = 110; char field[maxn][maxn]; bool vis[maxn][maxn]; int n, m; void dfs(int x, int y) { vis[x][y] = true; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int nx = x + dx, ny = y + dy; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && field[nx][ny] == '.') { dfs(nx, ny); } } } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> field[i][j]; } } int res = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!vis[i][j] && field[i][j] == '.') { dfs(i, j); res++; } } } cout << res << endl; return 0; } ``` 使用 BFS 进行搜索: ```c++ #include <iostream> #include <queue> using namespace std; const int maxn = 110; char field[maxn][maxn]; bool vis[maxn][maxn]; int n, m; void bfs(int x, int y) { queue<pair<int, int>> q; q.push(make_pair(x, y)); vis[x][y] = true; while (!q.empty()) { int cx = q.front().first, cy = q.front().second; q.pop(); for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int nx = cx + dx, ny = cy + dy; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && field[nx][ny] == '.') { q.push(make_pair(nx, ny)); vis[nx][ny] = true; } } } } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> field[i][j]; } } int res = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!vis[i][j] && field[i][j] == '.') { bfs(i, j); res++; } } } cout << res << endl; return 0; } ``` 时间复杂度: 两种方法的时间复杂度均为 $O(NM)$,其中 N 和 M 分别为矩阵的行数和列数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值