题目来源:http://poj.org/problem?id=1024

题目大意:

  有一个迷宫,迷宫的起点在(0,0)处。给定一条路径,和该迷宫墙的设置,要求验证该路径是否为唯一的最短路径,该种墙的设置中是否存在多于的墙,可结合图进行理解。

 

输入:第一行制定测试用例数。每个测试用例的第一行为两个正整数,指明迷宫的长和宽。接下来是给定的最短路径,用由L(左)R(右)U(上)D(下)组成的字符串表示。然后输入一个正整数m表示墙的个数。后接m行,每行四个整数,前两个整数和后两个整数分别组成两个坐标,表示该面墙把这两个点之间的路径隔断。

输出:若符合条件输出CORRECT否则输出INCORRECT。


Sample Input

2
8 5
RRRUULLURRRRDDRRUUU
19
0 0 0 1
1 0 1 1
2 0 2 1
2 1 3 1
3 0 4 0
3 1 4 1
3 2 4 2
3 2 3 3
2 2 2 3
4 2 4 3
0 3 0 4
1 3 1 4
2 3 2 4
3 3 3 4
4 3 4 4
5 3 5 4
5 3 6 3
5 2 6 2
6 1 6 2
4 3
RRRUU
2
2 2 3 2
2 2 2 1

Sample Output

CORRECT
INCORRECT

一开始没有什么想法,看了Discuss里的讨论和一些解题报告,思路很高明。

1. bfs求各点到源点的最小步数

2. bfs求各点到终点的最小步数

3. 遍历所有网格点,如果某不在路径上的点,到源点的步数+到终点的步数<=给定的路径,则给定的路径不是唯一最短

4. 检查每堵墙,如果把墙两侧点 到起点和到终点的步长加起来+1 > 给定路径的长度,则该墙多余。(如果拆掉这堵墙,唯一最短路径不变就说明多余)

 //////////////////////////////////////////////////////////////////////////
// POJ1024 Tester Program
// Memory: 368K Time: 16MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <string>
#include <queue>
using namespace std; struct Grid {
bool inpath; // 是否是路径方格
bool uwal; // 是否有上墙
bool rwal; // 是否有右墙
int scnt; // 到源点步数
int dcnt; // 到终点步数
}; int main(void) {
bool ok;
int w, h, cnt, steps; // 1 <= w, h <= 100
string path;
Grid grid[][];
queue<pair<int, int> > q; int t, x, y, desx, desy, x2, y2, i;
for (cin >> t; t > ; --t) {
//初始化数据
cin >> w >> h;
for (y = ; y < h; ++y) {
for (x = ; x < w; ++x) {
grid[y][x].inpath = false;
grid[y][x].uwal = false;
grid[y][x].rwal = false;
grid[y][x].scnt = -;
grid[y][x].dcnt = -;
}
}
cin >> path;
x = , y = ;
grid[][].inpath = true;
steps = path.size();
for (i = ; i < steps; ++i) {
switch (path[i]) {
case 'U': ++y; break;
case 'D': --y; break;
case 'L': --x; break;
case 'R': ++x; break;
}
grid[y][x].inpath = true;
}
desx = x, desy = y;
cin >> cnt;
for (i = ; i < cnt; ++i) {
cin >> x >> y >> x2 >> y2;
if (x == x2)
if (y + == y2) grid[y][x].uwal = true;
else grid[y2][x].uwal = true;
else
if (x + == x2) grid[y][x].rwal = true;
else grid[y][x2].rwal = true;
} //求各点到源点的最小步数(BFS)
q.push(make_pair(, ));
grid[][].scnt = ;
while (!q.empty()) {
y = q.front().first, x = q.front().second;
if (y < h - && grid[y][x].uwal == false && grid[y + ][x].scnt == -) {
grid[y + ][x].scnt = grid[y][x].scnt + ;
q.push(make_pair(y + , x));
}
if ( < y && grid[y - ][x].uwal == false && grid[y - ][x].scnt == -) {
grid[y - ][x].scnt = grid[y][x].scnt + ;
q.push(make_pair(y - , x));
}
if ( < x && grid[y][x - ].rwal == false && grid[y][x - ].scnt == -) {
grid[y][x - ].scnt = grid[y][x].scnt + ;
q.push(make_pair(y, x - ));
}
if (x < w - && grid[y][x].rwal == false && grid[y][x + ].scnt == -) {
grid[y][x + ].scnt = grid[y][x].scnt + ;
q.push(make_pair(y, x + ));
}
q.pop();
} //求各点到终点的最小步数(BFS)
q.push(make_pair(desy, desx));
grid[desy][desx].dcnt = ;
while (!q.empty()) {
y = q.front().first, x = q.front().second;
if (y < h - && grid[y][x].uwal == false && grid[y + ][x].dcnt == -) {
grid[y + ][x].dcnt = grid[y][x].dcnt + ;
q.push(make_pair(y + , x));
}
if ( < y && grid[y - ][x].uwal == false && grid[y - ][x].dcnt == -) {
grid[y - ][x].dcnt = grid[y][x].dcnt + ;
q.push(make_pair(y - , x));
}
if ( < x && grid[y][x - ].rwal == false && grid[y][x - ].dcnt == -) {
grid[y][x - ].dcnt = grid[y][x].dcnt + ;
q.push(make_pair(y, x - ));
}
if (x < w - && grid[y][x].rwal == false && grid[y][x + ].dcnt == -) {
grid[y][x + ].dcnt = grid[y][x].dcnt + ;
q.push(make_pair(y, x + ));
}
q.pop();
} //判断路径是否唯一最短,以及墙是否多余
ok = true;
for (y = ; y < h && ok; ++y) {
for (x = ; x < w && ok; ++x) {
if (grid[y][x].scnt == - || grid[y][x].dcnt == -)
ok = false; // 是否有封闭区域
if (y < h - && grid[y][x].uwal
&& grid[y][x].scnt + grid[y + ][x].dcnt + > steps
&& grid[y][x].dcnt + grid[y + ][x].scnt + > steps)
ok = false; // 是否上墙多余
if (x < w - && grid[y][x].rwal
&& grid[y][x].scnt + grid[y][x + ].dcnt + > steps
&& grid[y][x].dcnt + grid[y][x + ].scnt + > steps)
ok = false; // 是否右墙多余
if (!grid[y][x].inpath && grid[y][x].scnt + grid[y][x].dcnt <= steps)
ok = false; // 是否存在更短路径或另一最短路径
}
}
if(ok) cout << "CORRECT" << endl;
else cout << "INCORRECT" << endl;
}
return ;
}

POJ1024 Tester Program的更多相关文章

  1. 【BFS】Tester Program

    [poj1024]Tester Program Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2760   Accepted ...

  2. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  3. 数据结构( Pyhon 语言描述 ) — — 第7章:栈

    栈概览 栈是线性集合,遵从后进先出原则( Last - in first - out , LIFO )原则 栈常用的操作包括压入( push ) 和弹出( pop ) 栈的应用 将中缀表达式转换为后缀 ...

  4. 关于Rational Functional Tester (RFT)的简单介绍

    前段时间给客户做了个RFT的简单培训,以下.因为涉及到公司的框架,所以中间省去了很多框架里的细节,只留了一个框架的总体结构的概览. RFT IBM Rational Functional Tester ...

  5. How to using Piwis Tester II code Porsche rear end electronics

    V18.100 Piwis Tester II Diagnostic Tool For Porsche With CF30 Laptop High Quality Top 7 Reasons to G ...

  6. Porsche Piwis Tester II V14.000 with CF30 Laptop at autonumen.com

    Porsche piwis tester ii is the latest professional tester for Porshe,the most poweful diagnose and o ...

  7. Piwis Tester II V18.100 with CF30 Laptop for Porsche

    Porsche Piwis Tester II is the latest professional tester for Porshe,the most poweful diagnose and o ...

  8. Porsche Piwis II V14. three hundred and fifty computer software Tester II

    Porsche piwis tester 2 Help Devices: SERP automatio tranny, air-conditioner, SRS, ABDOMINAL MUSCLES, ...

  9. Porsche Piwis Tester II Diagnostic Tool -Next Generation of PIWIS Tester KTS520

    Porsche Piwis Tester II is the latest inspect equipment of Porsche Company. This is the latest profe ...

随机推荐

  1. FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析

    相关博客列表 : FFMPEG内存操作(一) avio_reading.c 回调读取数据到内存解析 FFMPEG内存操作(二)从内存中读取数及数据格式的转换 FFmpeg内存操作(三)内存转码器 在F ...

  2. bzoj 4261: 建设游乐场 费用流

    题目 现在有一大块土地,可以看成N*M的方格.在这块土地上,有些格子内是崎岖的山地,无法建造任何东西:其他格子都是平原.现在打算在这块土地上建设一个游乐园.游乐园由若干条闭合的过山车轨道组成,每个平原 ...

  3. BZOJ4003:[JLOI2015]城池攻占

    浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  4. bzoj 3083 遥远的国度 —— 树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3083 换根后路径还是不变,子树分类讨论一下,树剖后线段树维护即可. 代码如下: #inclu ...

  5. 找工作--volatile

    在Java编写的程序中,有时为了提高程序的运行效率,编译器会自动对其进行优化,把经常访问的变量缓存起来,程序在读取这个变量时有可能会直接从缓存(例如寄存器)中来读取这个值,而不会从内存中读取.这样做的 ...

  6. HDOJ1671(字符串前缀匹配)

    #include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...

  7. Python:内置函数makestrans()、translate()

    转于:https://blog.csdn.net/u014351782/article/details/46740297 博主:夜-feng 一.makestrans() 格式: str.maketr ...

  8. java代码Math.sqrt

    总结:这个判断小数的题目,当时全只2有一个人想出了结果.老师很开心.我很桑心~~~~ 我没想到要取膜,我只想到了除以等于0就够了.至于中间的“取膜”,我没凑齐来,还是不够灵活 package com. ...

  9. keepalived+redis 高可用redis主从解决方案

    背景介绍: 目前,Redis还没有一个类似于MySQL Proxy或Oracle RAC的官方HA方案.#Redis 2.8版开始正式提供名为Sentinel的主从切换方案(后面附上,未测试) 因此, ...

  10. 二 lambda表达式

    1:lambda写的好可以极大的减少代码冗余,同时可读性也好过冗长的内部类,匿名类. 2: lambda表达式配合Java8新特性Stream API可以将业务功能通过函数式编程简洁的实现. 3: l ...