题目来源: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. freeMarker(十二)——模板语言补充知识

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.特殊变量参考 特殊变量是由FreeMarker引擎自己定义的变量. ...

  2. 关于对H264码流的TS的封装的相关代码实现

    1 写在开始之前 在前段时间有分享一个H264封装ps流到相关文章的,这次和大家分享下将H264封装成TS流到相关实现,其实也是工作工作需要.依照上篇一样,分段说明每个数据头的封装情况,当然,一样也会 ...

  3. Statement

    题目大意 给定一棵基环外向树,和若干组询问,对于每次独立的询问都指定一些起点和一些终点,你删去一些边,使得从任意起点出发都无法到达终点,并让删去的边的编号的最小值最大,求这个最大的最小值. 题解 不难 ...

  4. ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)

    Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...

  5. linux绑定多个ip(转载)

    在Linux下有时候需要给一个网卡绑定多个IP,本文介绍在Redhat系列(redhat,Fedora Core,Centos)中的实现方法和一种在Gentoo等其他Linux中普遍适用的方法. 1. ...

  6. sql中in和exist语句的区别?(补充了left join和right join)

    in和exists(摘录自百度)in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 如果两个表中一个较小,一个是大表,则子查询表大的用exi ...

  7. 【转】深刻理解render 和 redirect_to

    由于最近老是在表单提交后出现没有反应的现象,发现是在action中的使用render 和 redirect_to的原因,于是就想搞清楚他两真正的区别在哪里,上一遍的blog也谈到了这二者的区别,但是有 ...

  8. ES6学习之Set和Map

    一.Set 1.Set 定义:Set类似于数组,但成员的值都是唯一的,没有重复的值 let s = new Set([1,2,3,4,5,2,4]); //Set { 1, 2, 3, 4, 5 } ...

  9. T-SQL 高级编程

    在Sql Server 中访问数据库一般有2种方式: 1.一种是使用应用程序编程接口API 2.数据库语句 变量:局部变量:以@为前缀,如@Age:全局变量以@@为前缀:(Ps:全局变量以系统定义和维 ...

  10. Angular面试题

    1. ng-show/ng-hide 与 ng-if的区别? 我们都知道ng-show/ng-hide实际上是通过display来进行隐藏和显示的.而ng-if实际上控制dom节点的增删除来实现的.因 ...