POJ 1024--Tester Program

北京大学ACM Problems 1024: Tester Program Tester Program Time Limit:1000MS Memory Limit:10000K Total Submissions:3317 Accepted:1217 Description Tester Program For this contest, we first designed the following problem (note that you do not have to solve it!): Another Wall... 阅读详情

题意

题目的大概意思是讲有一个迷宫游戏,给出其布局,和一个最佳行走路线,要求判断这个最佳路线是否是唯一的最短路线。同时,如果有多余(不影响结果)的墙也不行。

分析

我们来考虑每一面墙的作用,假如这面墙是必要的(拆了影响结果)。那么从这面墙到起点的距离加上到终点的距离必然小于给出的最佳路线长度。

所以,可以分别从起点和终点出发,假定给出的最佳路线长度为n,那么从起点和终点分别走n步(广度搜索),得到距离起点和终点n步的范围。然后再计算一下每一面墙分别到起点和终点的距离之和,如果一面墙的任意一面不在n步范围之内,显然这面墙是没有必要的。当然,如果距离之和大于最佳路线,也是没有必要的。

至于是不是唯一路线,从起点到终点广度搜索一遍即可确定。

代码如下:
Memory: 252K Time: 16MS Length: 178LINES

#include<iostream>
#include<string>
using namespace std;
struct PointPair { int x1, y1, x2, y2; };
struct PointandPath 
{ 
    int x, y; 
    string curpath; 
    PointandPath* next;  
    PointandPath(int a, int b, string s) 
    { x = a; y = b; curpath = s; next = NULL; }
};
struct DirectionSet { bool up, right, down, left; };
int width, height;
string path;
int walls;
int* stepmatrix;
int* stepmatrix2;
DirectionSet* map_direction;
int endx = 0, endy = 0;
PointPair* wallcoor;
string bestpath;
bool* visit;
void BuildMap()
{
    for (int i = 0; i < width; ++i) 
        map_direction[i * height].down = map_direction[i * height + height - 1].up = false;
    for (int i = 0; i < height; ++i)    
        map_direction[i].left = map_direction[(width - 1) * height + i].right = false;
    int count = 0;
    while (count < walls)
    {
        if (wallcoor[count].x1 < wallcoor[count].x2)    
            map_direction[wallcoor[count].x1 * height + wallcoor[count].y1].right = map_direction[wallcoor[count].x2 * height + wallcoor[count].y2].left = false;
        else if (wallcoor[count].x2 < wallcoor[count].x1) 
            map_direction[wallcoor[count].x2 * height + wallcoor[count].y2].right = map_direction[wallcoor[count].x1 * height + wallcoor[count].y1].left = false;
        else if (wallcoor[count].y1 < wallcoor[count].y2) 
            map_direction[wallcoor[count].x1 * height + wallcoor[count].y1].up = map_direction[wallcoor[count].x2 * height + wallcoor[count].y2].down = false;
        else if (wallcoor[count].y2 < wallcoor[count].y1) 
            map_direction[wallcoor[count].x1 * height + wallcoor[count].y1].down = map_direction[wallcoor[count].x2 * height + wallcoor[count].y2].up = false;
        ++count;
    }
}
void Traverse(int x, int y, const int dx, const int dy, int& count, int* matrix)
{
    memset(visit, true, sizeof(bool) * width * height);
    PointandPath* head = new PointandPath(x, y, "");
    PointandPath* rear = head;
    visit[x * height + y] = false;
    while (head)
    {
        if (head->x == dx && head->y == dy)
        {
            if (bestpath.length() == 0 || bestpath.length() > head->curpath.length())
            {
                bestpath = head->curpath;
                count = 1;
            }
            else if (head->curpath.length() == bestpath.length() && head->curpath != bestpath)      
                ++count;
        }
        matrix[head->x * height + head->y] = head->curpath.length();
        if (map_direction[head->x * height + head->y].right && visit[(head->x + 1) * height + head->y] && 
            (head->curpath.length() + 1 <= matrix[(head->x + 1) * height + head->y] ||  matrix[(head->x + 1) * height + head->y] == 0) && 
            head->curpath.length() + 1 <= path.length())
        {
            rear = rear->next = new PointandPath(head->x + 1, head->y, head->curpath + 'R');    
            visit[(head->x + 1) * height + head->y] = false;
        }
        if (map_direction[head->x * height + head->y].left && visit[(head->x - 1) * height + head->y] && 
            (head->curpath.length() + 1 <= matrix[(head->x - 1) * height + head->y] || matrix[(head->x - 1) * height + head->y] == 0) && 
            head->curpath.length() + 1 <= path.length())
        {
            rear = rear->next = new PointandPath(head->x - 1, head->y, head->curpath + 'L'); 
            visit[(head->x - 1) * height + head->y] = false;
        }
        if (map_direction[head->x * height + head->y].up && visit[head->x * height + head->y + 1] && 
            (head->curpath.length() + 1 <= matrix[head->x * height + head->y + 1] || matrix[head->x * height + head->y + 1] == 0) &&
            head->curpath.length() + 1 <= path.length())
        {
            rear = rear->next = new PointandPath(head->x, head->y + 1, head->curpath + 'U'); 
            visit[head->x * height + head->y + 1] = false;
        }
        if (map_direction[head->x * height + head->y].down && visit[head->x * height + head->y - 1] && 
            (head->curpath.length() + 1 <= matrix[head->x * height + head->y - 1] || matrix[head->x * height + head->y - 1] == 0) && 
            head->curpath.length() + 1 <= path.length())
        {
            rear = rear->next = new PointandPath(head->x, head->y - 1, head->curpath + 'D');    
            visit[head->x * height + head->y - 1] = false;
        }
        PointandPath* p = head;
        head = head->next;
        delete p;
    }
}
inline bool AtStartorEnd(int x, int y)
{
    if ((x == 0 && y == 0) || (x == endx && y == endy))
        return true;
    else
        return false;
}
int shorter(int pos)
{
    int tmp1 = 0, tmp2 = 0;
    if ((stepmatrix[wallcoor[pos].x1 * height + wallcoor[pos].y1] != 0 || AtStartorEnd(wallcoor[pos].x1, wallcoor[pos].y1)) && 
        (stepmatrix2[wallcoor[pos].x2 * height + wallcoor[pos].y2] != 0 || AtStartorEnd(wallcoor[pos].x2, wallcoor[pos].y2)))
        tmp1 = stepmatrix[wallcoor[pos].x1 * height + wallcoor[pos].y1] + stepmatrix2[wallcoor[pos].x2 * height + wallcoor[pos].y2];
    if ((stepmatrix[wallcoor[pos].x2 * height + wallcoor[pos].y2] != 0 || AtStartorEnd(wallcoor[pos].x2, wallcoor[pos].y2)) && 
        (stepmatrix2[wallcoor[pos].x1 * height + wallcoor[pos].y1] != 0 || AtStartorEnd(wallcoor[pos].x1, wallcoor[pos].y1)))
        tmp2 = stepmatrix[wallcoor[pos].x2 * height + wallcoor[pos].y2] + stepmatrix2[wallcoor[pos].x1 * height + wallcoor[pos].y1];
    if (tmp1 == 0)      return tmp2 == 0 ? 0 : tmp2 + 1;
    if (tmp2 == 0)  return tmp1 == 0 ? 0 : tmp1 + 1;
    else        return tmp1 < tmp2 ? tmp1 + 1 : tmp2 + 1;
}
int main()
{
    int cases;
    cin >> cases;
    while (cases-- > 0)
    {
        cin >> width >> height >> path >> walls;
        endx = 0;
        endy = 0;
        wallcoor = new PointPair[walls];
        stepmatrix = new int[height * width];
        stepmatrix2 = new int[height * width];
        visit = new bool[height * width];
        map_direction = new DirectionSet[height * width];
        memset(stepmatrix, 0, sizeof(int) * height * width);
        memset(stepmatrix2, 0, sizeof(int) * height * width);
        memset(wallcoor, 0, sizeof(PointPair) * walls);
        memset(map_direction, true, sizeof(DirectionSet) * height * width);
        for (int i = 0; i < walls; ++i)
            cin >> wallcoor[i].x1 >> wallcoor[i].y1 >> wallcoor[i].x2 >> wallcoor[i].y2;
        BuildMap();
        int key = 0;
        stepmatrix2[0] = path.length();
        while (key < path.length())
        {
            if (path[key] == 'R') { if (map_direction[endx * height + endy].right) ++endx;  else break; }
            else if (path[key] == 'U') { if (map_direction[endx * height + endy].up) ++endy; else break; }
            else if (path[key] == 'L') { if (map_direction[endx * height + endy].left) --endx;  else break; }
            else if (path[key] == 'D') { if (map_direction[endx * height + endy].down) --endy;  else break; }
            stepmatrix[endx * height + endy] = ++key;
            stepmatrix2[endx * height + endy] = path.length() - key;
        }
        if (key == path.length())
        {
            int count = 0;
            bestpath = "";
            Traverse(0, 0, endx, endy, count, stepmatrix);
            if (count == 1 && bestpath == path)
            {
                Traverse(endx, endy, 0, 0, count, stepmatrix2);
                int i = 0;
                while (i < walls)
                {
                    int length = shorter(i++);
                    if (length > path.length() || length == 0)
                    {
                        cout << "INCORRECT" << endl;
                        break;
                    }
                }
                if (i == walls)     cout << "CORRECT" << endl;
            }
            else        cout << "INCORRECT" << endl;
        }
        else    cout << "INCORRECT" << endl;
        delete[] wallcoor;
        delete[] stepmatrix;
        delete[] stepmatrix2;
        delete[] map_direction;
        delete[] visit;
    }
    return 0;
}
保姆级教程:用Arduino+MPU6050自制寻北仪(附MEMS陀螺精度测试报告) 本文提供了一份详细的Arduino与MPU6050自制寻北仪的保姆级教程,包括硬件选型、卡尔曼滤波算法优化、地磁场补偿及真北计算等关键技术。通过实测数据展示了MEMS陀螺仪在民用领域的高精度性能,帮助读者低成本实现1°以内的寻北精度。 阅读详情

相关推荐

npu-smi info 华为昇腾NPU 状态监控工具解读

用于查询所有设备的基本信息。

Lixora's DB Home 2486

poj-1024(唯一最短路径)(转)

题意: /*** (1)求各点到源点的最小步数(DFS)* (2)求各点到终点的最小步数(DFS)* (3)如果点不是给定路径上的点,那么:该点到源点的最小步数+该点到终点的最小步数<=给定路径的步数,否则给定路径不是唯一最短的* (4)如果两相邻点a、b之间存在墙,那么:a到源点的最小步数+1+b到终点的最小步数<=给定路径的步数*...

dmt26374的博客 336

【Python实战解析】从数据爬取到房价预测:一个完整的数据科学项目流程拆解

本文详细拆解了一个完整的Python数据科学项目流程,从爬取长沙二手房数据到房价预测模型构建。通过实战案例,介绍了数据爬取、清洗、可视化及模型优化的关键技巧,帮助开发者掌握Python在数据分析中的应用,提升程序开发能力。

weixin_30379531的博客 411

POJ-1024取石子游戏(威佐夫博弈)——求解与数学证明

今天在网上看到这道算法题,很有意思,看了网上的解题思路,很受启发,但感觉没有看到严谨的证明,就结合自己的思路,写一下我的证明过程。题目是这样的:Problem Description有两堆石子,数量任意,可以不同。游戏开始由两个人轮流取石子。游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子;二是可以在两堆中同时取走相同数量的石子。最后把石子全部取完者为胜者。现在给出初始的两堆...

qq_37664133的博客 819

node-red 可视化探索

配置socket选择配置流程产生url。

weixin_44133581的博客 3412

物体检测之非极大值抑制:NMS、softNMS、softerNMS、IoU-Net

当前的物体检测算法为了保证召回率,对于同一个真实物体往往会有多个的候选框输出。由于多余的候选框会影响检测精度,因此需要利用NMS过滤掉重叠的候选框,得到最佳的预测输出。当前有如下几种较为常见的NMS方法, 1.基本NMS 非极大值抑制, 顾名思义就是抑制不是极大值的边框,这里的抑制通常是直接去掉冗余的边框。 这个过程涉及以下两个量化指标: 1.预测得分:NMS假设一个边框的预测得分越高,这个框...

LeoLee 1941

POJ 1024 Tester Program(DFS:求单源最短路径)

POJ 1024 Tester Program(DFS求单源最短路径) http://poj.org/problem?id=1024 题意:现在有一个迷宫,我们给出一条最短路径,然后继续给出这个迷宫的墙的构造方式,要你检查最短路径是否唯一 且 这些墙是不是有多余? 分析:仔细体会如何用DFS一次求出单源最短距离.        首先我们根据上面给的最短路径和墙的信息构图(具体看代码,仔细

code 2015

CMU的local_planner学习笔记

ros::Subscriber subOdometry = nh.subscribe//当接收到/state_estimation话题消息更新时启动odometryHandler回调函数 (消息格式:nav_msgs::Odometry消息内容:关于机器人或车辆的位置和方向(姿态)信息)pcl::PointCloud::Ptr freePaths(new pcl::PointCloud());

qq_35971623的博客 3876

POJ答案解析1000——1024,全部AC

1000(A+B问题) #include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf("%d\n",a+b); return 0; } 1004(财务管理) #include <stdio.h> void main() { int i; d...

qq_38769551的博客 1231

Claude Code 完全指南:从入门到精通

《AI编程助手ClaudeCode深度指南》摘要 ClaudeCode作为新一代AI编程助手,凭借200K tokens的超长上下文窗口和深度代码理解能力,正在重塑开发工作流程。本文系统介绍了从安装配置到高级应用的完整使用指南: 核心优势包括: 项目级代码理解能力 Agent自主规划复杂任务 原生终端集成支持 多文件协同编辑功能 实战应用场景: 快速理解陌生项目架构 诊断性能瓶颈并提供优化方案 自动生成符合规范的API代码 开发可复用技术组件 高级技巧: 通过CLAUDE.md定义项目规范 合理使用/cle

分享AI、区块链技术及应用,未来已来 603

【并发编程五】c++进程通信——共享内存(shared memmory)

现代操作系统,对于内存管理,采⽤的是虚拟内存技术,也就是每个进程都有⾃⼰独⽴的虚拟内存空间,不同进程的虚拟内存映射到不同的物理内存中。所以,即使进程 A中 和 进程 B中 的虚拟地址是⼀样的,其实访问的是不同的物理内存地址,对于数据的增删查改互不影响。这样这个进程写⼊的东⻄,另外⼀个进程⻢上就能看到了,都不需要拷⻉来拷⻉去,传来传去,⼤⼤提⾼了进程间通信的速度。⽤了共享内存通信⽅式,带来新的问题,那就是如果多个进程同时修改同⼀个共享内存,很有可能就冲突了。那共享内存的⽅式,就很好的解决了这⼀问题。

junxuezheng的博客 1万+

POJ 1024 Tester Program

/* WA 主要思路: 首先DFS建立层次图。 然后判断,路径是否唯一,墙壁是否重复。 */ #include //#define TEST using namespace std; const int nMax = 105; const int mMax = 10100; int dis_s[nMax][nMax], dis_e[nMax][nMax]; char s[mMax]; stru

884

摇骰子、抽奖转盘酒桌游戏 人生重启模拟器小程序源码分享-开通流量主躺着赚钱

摇骰子、抽奖转盘酒桌游戏 人生重启模拟器小程序源码分享-开通流量主躺着赚钱

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1650

POJ1024 Tester Program

题目来源:http://poj.org/problem?id=1024 题目大意:   有一个迷宫,迷宫的起点在(0,0)处。给定一条路径,和该迷宫墙的设置,要求验证该路径是否为唯一的最短路径,该种墙的设置中是否存在多于的墙,可结合图进行理解。   输入:第一行制定测试用例数。每个测试用例的第一行为两个正整数,指明迷宫的长和宽。接下来是给定的最短路径,用由L(左)R(右)U(上)D(下)组...

weixin_30256901的博客 195

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

POJ 1024 Tester Program 笔记

给出走出迷宫的最短路径和迷宫。判断迷宫是否能使最短路径唯一,并且迷宫墙壁没有冗余。

woniupengpeng的专栏 412

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

poj 1024+BFS+Tester Program

题目大意:给一个rows*column的迷宫,并给出一条路径,和一些迷宫路径之间的墙,让判断这些墙是否能使的从起点到终点的唯一最短路径为给定的路径,并且这些墙中没有多余的墙,其中起点为(0,0),终点不定。 题目解法:暴力+BFS,然后竟然也过了- -| 从起点BFS搜索到终点的最短路径,如果最短路径长度小于给定的路径长度,则不正确;依次将路径中的路径用墙隔离,如果隔离后仍存在从起点到

凌语小辉 632

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

poj1024:Tester Program给出最短路径求对应迷宫

题意大概如下:給出最短路径,要求一个迷宫,使得该迷宫的唯一最短路径即为此路径,且迷宫中的每一堵墙都不能多余。然后给出一种方案,要你判断此方案是否正确。 我的算法是:先bfs得出一条最短路径和最短路径的总数,若大于1或者跟输入的最短路径不同均不可以。然后计算对于每一个墙,如果可以通过该墙的话最短路径是多少。 如果有算出大于输入的最短路径的(也即是该墙多余),也不可以。算法完毕; AC代码如下:

To lose to pursue. 1657

poj1024

题目描述:给定走出一个迷宫的最短路径,再给出一个由这个最短路径算出来的解(一个墙的结合),现在要有设计程序判断该解是否正确。 正确的标准:最短路径为给出的最短路径且唯一,如果给出的解(墙的集合)组成的迷宫的最短路径比给定的最短路径长或短都不行,一定要一样长且只有给定的那一条路是最短路。 输入格式:多组数据,第一行为一个整数t,表示数据的组数 每组数据包括一个原始问题输入和一个原始问题输出。

SparkRat的家 1587

poj1024 bfs

如题:http://poj.org/problem?id=1024 Tester Program Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2360   Accepted: 830 Description Tester Program For this

zht的专栏 919

Poj 1024

传送门 判断给定的路径是否是迷宫的唯一最短路径,判断迷宫是否有墙多余 bfs计算出每个点分别到起点和终点的最短路径 路径是否是迷宫的唯一最短路径:将路径上的点和墙两侧的点标记,查看是否有非标记的点到起点和终点之和小于等于给定路径长度 是否有墙多余:设墙两侧的点分别为a、b,若a到起点的距离与b到终点的距离大于路径长度并且a到终点的距离与b到起点的距离大于路径长度,则墙多余   ...

weixin_30627341的博客 203

POJ 1024 Gone Fishing(贪心)

Description John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He

蒟蒻本蒻的博客 248

Chapter5-标准I/O库(中)-「APUE读书笔记」

读和写流 当流打开后,有3种非格式化I/O可对其进行读写操作:(1)每次一个字符,(2)每次一行的I/O,(3)直接I/O。 1、输入函数 #include //三个函数返回取到的字符,出错或到达文件尾返回EOF int    getc(FILE*  fp);     //可被实现为宏。 int    fgetc(FILE*  fp); int    getchar(void

Hering_Linux的专栏 780
上一篇: POJ 1023--The Fun Number System
下一篇: POJ 1027--The Same Game
Hering_Linux
博客等级 码龄12年 0粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值