题目来源: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. python实现无序列表:链表

    介绍链表前我们先了解下什么是列表. 在对基本数据结构的讨论中,我们使用 Python 列表来实现所呈现的抽象数据类型.列表是一个强大但简单的收集机制,为程序员提供了各种各样的操作.然而,不是所有的编程 ...

  2. informix 把数据从一个表倒到另外一个表中

    drop table zrjReinUnClaimTmpT; create table zrjReinUnClaimTmpT ( mainid SERIAL not null, RepayNo var ...

  3. 1068 Find More Coins (30)(30 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  4. 让Django支持数据库长连接(可以提高不少性能哦)

    书接上回 上回我们说到:<在生产系统使用Tornado WebServer来代替FastCGI加速你的Django应用> 那么现在很流行用一些高性能的nonblock的app server ...

  5. HDU1114(完全背包装满问题)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. LoadRunner 参数模拟——快速得到并发用户的进场规则

    LoadRunner 并发时,一般需要进入参数化 用户名或者相关的字段数据,用以满足在并发时无限的接近真实的使用场景: 但当你在设置好参数化的读取规则后,在执行并发场景时,用户并发加载顺序是否确实如你 ...

  7. 07_ddms透视图介绍

    通过ADB(Android Debug Bridge)安卓调试桥把你的Eclipse(集成开发环境)和你的设备连接在一起.有时候ADB可能会被其他的东西占用.例如WPS会跟你抢ADB(抢端口).如果你 ...

  8. SpringSecurity01 SpringSecurity环境搭建

    版本说明: JDK -> java version "1.8.0_101" MAVEN -> Apache Maven 3.5.0 IDEA -> 2017.2. ...

  9. R: data.frame 生成、操作数组。重命名、增、删、改

    ################################################### 问题:生成.操作数据框   18.4.27 怎么生成数据框 data.frame.,,及其相关操 ...

  10. 22、IDP-ASE

    IDPASE https://github.com/bdeonovic/IDPASE.jl Prepare necessary input files (1)FASTQ file of your hy ...