Fairy tale(BFS + 大模拟)

本文详细解析了一道迷宫寻宝的算法题,通过理解题意和细致处理,实现了saya在变化的地图中寻找宝藏的策略。文章分享了代码实现,包括移动规则、循环检测和距离计算,为读者提供了完整的算法思路。

一.题目链接:

Fairy tale

二.题目大意:

给你一个N × N 的地图,图上的每个点有四种方向(E W S N),代表着移动方向.

在 t = 0 时,saya 在 (1, 1),treasure 在 (n, n).

在每个单位时间内,分为 3 步.

① saya 先按照地图移动一个单位.

② saya 可以向四个方向移动一个单位,saya 会移动到距离 treasure 最近的位置

     如果四个方向距离都一样,那么 saya 不移动.

     否则,按照 E > W > N > S 移动.

③ treasure 按照地图移动. 

在每个单位时间后,地图会改变.

规则如下:E -> W -> S -> N -> E.

在 x<= 100 步时

如果 saya 能得到 treasure,输出 "Get the treasure! At step x."

如果 saya 陷入循环,输出 "Impossible. At step x."

否则输出 "Not sure."

三.分析:

这题本身不难,主要在于对题意的准确理解 和 细节的处理.

一开始就是因为没理解好题意一直 WA.

比如是 saya 的两步先移动完,treasure 才移动.

还有就是必须是四个方向的距离都相等才不动.

如果有一个方向出界,其他方向都相等,此时还是要按照优先级移动的.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-4
#define PI acos(-1.0)
#define ll long long int
using namespace std;

const int M = 35;
const int inf = 0x3f3f3f3f;
int maze[M][M];
int ans1, ans2;
int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};///E > W > N > S (y, x)
struct node
{
    int y1, x1;///saya
    int y2, x2;///treasure
}s[110];

bool loop(int step)
{
    for(int i = step % 4; i < step; i += 4)
    {
        if(s[step].x1 == s[i].x1 && s[step].y1 == s[i].y1 && s[step].x2 == s[i].x2 && s[step].y2 == s[i].y2)
            return 1;
    }
    return 0;
}

void Move1(int step, int n)
{
    int yy = s[step + 1].y1 = s[step].y1;
    int xx = s[step + 1].x1 = s[step].x1;
    if((maze[yy][xx] + step) % 4 == 0 && xx + 1 <= n)
        s[step + 1].x1 = xx + 1;
    else if((maze[yy][xx] + step) % 4 == 1 && xx - 1 >= 1)
        s[step + 1].x1 = xx - 1;
    else if((maze[yy][xx] + step) % 4 == 2 && yy + 1 <= n)
        s[step + 1].y1 = yy + 1;
    else if((maze[yy][xx] + step) % 4 == 3 && yy - 1 >= 1)
        s[step + 1].y1 = yy - 1;
}

void Move2(int step, int n)
{
    int yy = s[step + 1].y2 = s[step].y2;
    int xx = s[step + 1].x2 = s[step].x2;
    if((maze[yy][xx] + step) % 4 == 0 && xx + 1 <= n)
        s[step + 1].x2 = xx + 1;
    else if((maze[yy][xx] + step) % 4 == 1 && xx - 1 >= 1)
        s[step + 1].x2 = xx - 1;
    else if((maze[yy][xx] + step) % 4 == 2 && yy + 1 <= n)
        s[step + 1].y2 = yy + 1;
    else if((maze[yy][xx] + step) % 4 == 3 && yy - 1 >= 1)
        s[step + 1].y2 = yy - 1;
}

int better(int y1, int x1, int y2, int x2)
{
    int dis1 = (y1 - y2) * (y1 - y2);
    int dis2 = (x1 - x2) * (x1 - x2);
    return dis1 + dis2;
}

void dfs(int step, int n)
{
    if(step > 100)
        return;
    if(s[step].x1 == s[step].x2 && s[step].y1 == s[step].y2)
    {
        ans1 = step;
        return;
    }
    if(loop(step))
    {
        ans2 = step;
        return;
    }
    Move1(step, n);
    Move2(step, n);
    int dis[5];
    int Min = inf;
    for(int i = 0; i < 4; ++i)
    {
        dis[i] = inf;
        int yy = s[step + 1].y1 + dir[i][0];
        int xx = s[step + 1].x1 + dir[i][1];
        if(yy >= 1 && yy <= n && xx >= 1 && xx <= n)
            dis[i] = better(yy, xx, s[step].y2, s[step].x2);
        if(dis[i] < Min)
            Min = dis[i];
    }
    if(dis[0] == dis[1] && dis[0] == dis[2] && dis[0] == dis[3]);
    else if(dis[0] == Min)
        s[step + 1].x1++;
    else if(dis[1] == Min)
        s[step + 1].x1--;
    else if(dis[2] == Min)
        s[step + 1].y1--;
    else if(dis[3] == Min)
        s[step + 1].y1++;
    dfs(step + 1, n);
}

int main()
{
    int n;
    int c = 0;
    while(~scanf("%d", &n) && n)
    {
        for(int i = 1; i <= n; ++i)
        {
            getchar();
            for(int j = 1; j <= n; ++j)
            {
                char ch;
                scanf("%c", &ch);
                switch(ch)
                {
                    case 'E': maze[i][j] = 0;break;
                    case 'W': maze[i][j] = 1;break;
                    case 'S': maze[i][j] = 2;break;
                    case 'N': maze[i][j] = 3;break;
                }
            }
        }
        ans1 = ans2 = -1;
        s[0].x1 = s[0].y1 = 1;
        s[0].x2 = s[0].y2 = n;
        dfs(0, n);
        printf("Case %d:\n", ++c);
        if(ans1 == -1 && ans2 == -1)
            printf("Not sure.\n");
        else if(ans1 != -1)
            printf("Get the treasure! At step %d.\n", ans1);
        else
            printf("Impossible. At step %d.\n", ans2);
        printf("\n");
    }
    return 0;
}

 

代码转载自:https://pan.quark.cn/s/8ce4326d996e 对于在 CentOS 7 系统中修改网卡配置文件后无法使设置生效的情况,经过实践验证,可以通过使用 nmcli 命令来进行调整。完成修改之后,需要重新启动虚拟机以使更改生效,这样操作流程即告完成。如果设置仍然无法生效,则表明虚拟机在启动过程中所获取的 IP 地址配置并非针对 eth0,此时可以对其它网卡的配置文件进行修改或将其移除。在 CentOS 7 系统中,网络配置的管理机制与早期版本存在差异,主要体现为采用了 Network Manager 服务来负责网络接口的管理。在某些情形下,尽管修改了 `/etc/sysconfig/network-scripts` 目录下的 `ifcfg-eth0` 文件,但网络配置却未能即时生效。此类问题的发生通常源于 CentOS 7 采用了不同于以往的配置读取方法。接下来将具体阐述如何借助 nmcli 命令来处理这一挑战。 以 root 用户身份登录系统并打开终端界面。nmcli 是 Network Manager 提供的命令行界面工具,它支持在命令行环境下执行网络连接的建立、编辑、查询及管理任务。针对修改 eth0 网卡配置的需求,可以遵循以下步骤进行操作: 1. 导航至 `/etc/sysconfig/network-scripts` 目录: ``` cd /etc/sysconfig/network-scripts ``` 2. 检查该目录内是否存在 `ifcfg-eth0.bak` 文件,该备份文件可能是先前调整配置时遗留下来的,若存在可能造成冲突。若发现该文件,可以选择将其删除: ``` [root@localhost netw...
代码转载自:https://pan.quark.cn/s/46fd08fb879c 网管教程 从入门到精通软件篇 ★一。★详尽的xp修复控制台指令及其应用!!! 放入xp(2000)的光盘,安装时选择R,执行修复! Windows XP(涵盖 Windows 2000)的控制台指令是在系统遭遇某些意外状况时的一种极具效用的诊断、检测以及恢复系统功能的工具。笔者确实一直期望能够将这方面的指令进行归纳,此次由老范辛苦整理了这份极具价值的秘籍。 Bootcfg bootcfg 命令用于启动配置与故障恢复(对大多数计算机而言,即 boot.ini 文件)。 带有特定参数的 bootcfg 命令仅在运用故障恢复控制台时方可使用。能够在命令行界面下运用带有不同参数的 bootcfg 命令。 用法: bootcfg /default 设定默认引导选项。 bootcfg /add 向引导清单中增添 Windows 安装。 bootcfg /rebuild 重复整个 Windows 安装流程并让用户选择需添加的项目。 注意:运用 bootcfg /rebuild 之前,应先借助 bootcfg /copy 命令备份 boot.ini 文件。 bootcfg /scan 探查用于 Windows 安装的全部磁盘并展示结果。 注意:这些结果被静态存储,并用于当前会话。若在当前会话期间磁盘配置发生变动,为获取更新的探查结果,必须先重启计算机,然后再次探查磁盘。 bootcfg /list 列示引导清单中已有的项目。 bootcfg /disableredirect 在启动引导程序中禁用重定向。 bootcfg /redirect [ PortBaudRrate] |[ useBio...
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值