hdu 1240 Asteroids!

本文深入探讨了在三维空间内利用BFS和DFS算法进行路径搜索的方法,并通过实例代码展示了如何求解从出发点到目标点所需的最短步数。同时,对比了两种算法在解决此类问题时的效率和代码简洁性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Asteroids!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4530    Accepted Submission(s): 2920


Problem Description
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.


Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 5 components:

Start line - A single line, "START N", where 1 <= N <= 10.

Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:

'O' - (the letter "oh") Empty space

'X' - (upper-case) Asteroid present

Starting Position - A single line, "A B C", denoting the <A,B,C> coordinates of your craft's starting position. The coordinate values will be integers separated by individual spaces.

Target Position - A single line, "D E F", denoting the <D,E,F> coordinates of your target's position. The coordinate values will be integers separated by individual spaces.

End line - A single line, "END"

The origin of the coordinate system is <0,0,0>. Therefore, each component of each coordinate vector will be an integer between 0 and N-1, inclusive.

The first coordinate in a set indicates the column. Left column = 0.

The second coordinate in a set indicates the row. Top row = 0.

The third coordinate in a set indicates the slice. First slice = 0.

Both the Starting Position and the Target Position will be in empty space.



Output
For each data set, there will be exactly one output set, and there will be no blank lines separating output sets.

A single output set consists of a single line. If a route exists, the line will be in the format "X Y", where X is the same as N from the corresponding input data set and Y is the least number of moves necessary to get your ship from the starting position to the target position. If there is no route from the starting position to the target position, the line will be "NO ROUTE" instead.

A move can only be in one of the six basic directions: up, down, left, right, forward, back. Phrased more precisely, a move will either increment or decrement a single component of your current position vector by 1.



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


Source
South Central USA 2001

题目大意:在三维空间里搜索从出发点到中止点需要的最短步数
采用了两种方法,BFS和DFS 我个人更喜欢使用BFS,但好几次做
搜索题对比之后发现通常情况下DFS比BFS写起来代码更简洁一些,
思路只要完整,优化剪枝之后效率比BFS更高。此题DFS 0ms通过
看来以后要多使用DFS了。

BFS代码实现:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<string.h>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<cctype>
#include<fstream>
#include<sstream>
#include<iomanip>
#include<set>
#include<vector>
#include<cstdlib>
#include<time.h>
using namespace std;

struct node{
    char c;
    int x, y, z;
    bool book;
    int dis;
    friend bool operator ==(node a,node b)
    {
        return a.x == b.x&&a.y == b.y&&a.z == b.z;
    }
} m[12][12][12];
int fx[6][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { -1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } };
node start, target;
int n;
int good(int a, int b, int c)
{
    if (m[a][b][c].c=='O'&&m[a][b][c].book == 0 && a >= 0 && a < n&&b >= 0 && b < n&&c >= 0 && c < n)
        return 1;
    else if (a == target.x&&b == target.y&&c == target.z)
        return 2;
    else
        return 3;
}
int bfs()
{
    queue<node> duilie;
    duilie.push(start);
    m[start.x][start.y][start.z].book = true;
    while (!duilie.empty())
    {
        node t = duilie.front();
        duilie.pop();
        for (int i = 0; i < 6; i++)
        {
            int a, b, c;
            a = t.x + fx[i][0];
            b = t.y + fx[i][1]; 
            c = t.z + fx[i][2];
            if (good(a, b, c)<=2)
            {
                m[a][b][c].book = 1;
                m[a][b][c].dis = m[t.x][t.y][t.z].dis+1;
                if (good(a, b, c) == 2)
                    return m[a][b][c].dis;
                duilie.push(m[a][b][c]);
            }
        }
    }
    return -1;
}


int main()
{
    string sta;
    while (cin >> sta >> n)
    {
        for (int k = 0; k < n; k++)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    cin >> m[i][j][k].c;
                    m[i][j][k].x = i;
                    m[i][j][k].y = j;
                    m[i][j][k].z = k;
                    m[i][j][k].book = false;
                    m[i][j][k].dis = 0;
                }
            }
        }
        cin >> start.x >> start.y >> start.z;
        cin >> target.x >> target.y >> target.z;
        cin >> sta;
        int s = bfs();
        if (start == target)
            cout << n << ' ' << 0 << endl;
        else
        {
            if (s == -1)
                cout << "NO ROUTE" << endl;
            else
                cout << n << ' ' << s << endl;
        }
    }
    return 0;
}

DFS代码实现

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<string.h>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<cctype>
#include<fstream>
#include<sstream>
#include<iomanip>
#include<set>
#include<vector>
#include<cstdlib>
#include<time.h>
using namespace std;

struct node{
    char c;
    int x, y, z;
    int dis;
    friend bool operator ==(node a, node b)
    {
        return a.x == b.x&&a.y == b.y&&a.z == b.z;
    }
} m[12][12][12];
int fx[6][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { -1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } };
node start, target;
int n;
int good(int a, int b, int c)
{
    if (a == target.x&&b == target.y&&c == target.z)
        return 2;
    else if (m[a][b][c].c == 'O'&& a >= 0 && a < n&&b >= 0 && b < n&&c >= 0 && c < n)
        return 1;
    else if (m[a][b][c].c == 'X')
        return 3;
    else
        return 4;
}
int maxs = INT_MAX;
void dfs(int a, int b, int c, int cnt)
{
    if (good(a, b, c) == 2)
    {
        if (cnt < maxs)
            maxs = cnt;
        return;
    }
    if (a < 0 || b < 0 || c<0 || a >= n || b >= n || c >= n)
        return;
    if (m[a][b][c].c == 'X')
        return;
    if (cnt>m[a][b][c].dis)
        return;
    else
        m[a][b][c].dis = cnt;
    for (int i = 0; i < 6; i++)
    {
        int aa, bb, cc;
        aa = a + fx[i][0];
        bb = b + fx[i][1];
        cc = c + fx[i][2];
        dfs(aa, bb, cc, cnt + 1);
    }
}


int main()
{
    string sta;
    while (cin >> sta >> n)
    {
        for (int k = 0; k < n; k++)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    cin >> m[i][j][k].c;
                    m[i][j][k].x = i;
                    m[i][j][k].y = j;
                    m[i][j][k].z = k;
                    m[i][j][k].dis = INT_MAX;
                }
            }
        }
        cin >> start.x >> start.y >> start.z;
        cin >> target.x >> target.y >> target.z;
        cin >> sta;
        maxs = INT_MAX;
        dfs(start.x, start.y, start.z, 0);
        if (start == target)
            cout << n << ' ' << 0 << endl;
        else
        {
            if (maxs == INT_MAX)
                cout << "NO ROUTE" << endl;
            else
                cout << n << ' ' << maxs << endl;
        }
    }
    return 0;
}

附上效率对比:

DFS :

16563895 2016-03-15 19:31:46 Accepted 1240 0MS 1832K 2084 B C++

BFS:

16560166 2016-03-15 13:29:42 Accepted 1240 15MS 1824K 2097 B C++

内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法不仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文不仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读时应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值