POJ1024 Tester Program
题目来源: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的更多相关文章
- 【BFS】Tester Program
[poj1024]Tester Program Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2760 Accepted ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- 数据结构( Pyhon 语言描述 ) — — 第7章:栈
栈概览 栈是线性集合,遵从后进先出原则( Last - in first - out , LIFO )原则 栈常用的操作包括压入( push ) 和弹出( pop ) 栈的应用 将中缀表达式转换为后缀 ...
- 关于Rational Functional Tester (RFT)的简单介绍
前段时间给客户做了个RFT的简单培训,以下.因为涉及到公司的框架,所以中间省去了很多框架里的细节,只留了一个框架的总体结构的概览. RFT IBM Rational Functional Tester ...
- 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 ...
- 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 ...
- 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 ...
- 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, ...
- 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 ...
随机推荐
- 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- Codechef Union on Tree
Codechef Union on Tree https://www.codechef.com/problems/BTREE 简要题意: 给你一棵树,\(Q\)次询问,每次给出一个点集和每个点的\(r ...
- [转]HTTP头的Expires与Cache-control
1.概念 Cache-control用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache) 数据包中的格式: Cache-Control: cache- ...
- 系统原生文件MD5值获取
windows: certutil -hashfile filePath MD5 certutil -hashfile filePath SHA1 Linux md5sum filePath s ...
- 洛谷【P1177】【模板】归并排序
题目传送门:https://www.luogu.org/problemnew/show/P1177 归并排序: 1.先将\(a\)数组的区间\([l,mid],[mid+1,r]\)排成有序的. 2. ...
- POJ1379:Run Away
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...
- GrayCode for state machine
How & Why use Gray Code A gray counter is a binary counter where only one bit changes at a time. ...
- centos6.5升级默认的Mysql到5.5方法
0.用lsb_release -a 查看linux系统的版本 1.官网下载bundle或rpm版2.解压 tar -xvf MySQL-xxx.tar或 MySQL-server-xxx.rpm和My ...
- HDOJ1073(gets 应用)
练习操作字符串的好题. #include<cstdio> #include<algorithm> #include<cstring> using namespace ...
- requests 的使用
1.1.实例引入 # 引入Requests库 import requests # 发起GET请求 response = requests.get('https://www.baidu.com/') ...