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

Description

Tester Program 
For this contest, we first designed the following problem (note that you do not have to solve it!):

Another Wall in the Maze

In ACM/ICPC contests, you'll often see questions such as "find the shortest path out of this maze." Let's turn this on its head and ask "given a path, find a maze for which the given path is the shortest path." Our paths will run vertically and horizontally between the regularly spaced points of a rectangular grid. The problem is to compute a set of unit-length baffles (walls) separating grid points that forces the given path to be the unique shortest path from its starting point to the end point. To make things more interesting, we will require that there should be no redundant walls constructed in the sense that it should not be possible to remove any wall and still have the given path as the unique shortest path. In the following figure for example, consider the path through the 8 ? 5 grid on the left maze of the top row. The wall placements in the two mazes to its right (top row) make that path unique. The two mazes on the lower row are faulty. 
The path is not unique in the one on the left, and there are some redundant walls on the right. 

Input (of the original problem)

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. The first line of each test case consists of two integers W and H (1 ≤ W, H ≤ 100) giving the width and height of the grid respectively. The second line of the test case contains a path. The path always starts in the lowerleft corner, (0, 0). It is specified as a string of U (up), D (down), L (left), and R (right) characters (with no embedded white space). You may assume that the path remains within the bounds of the maze and does not intersect itself. It may end anywhere in the maze (i.e., not necessarily in a corner or against a wall).

Output (of the original problem)

First line of the output for the i-th test case (starting from one) should contain an integer M, the number of walls used in the solution. Following the first line, there are M lines each containing a wall specification in the form of four consecutive integers corresponding to two pairs of (x, y) coordinates specifying adjacent grid points separated by the wall (0 ≤ x < W and 0 ≤ y < H). Note that the possible output is not unique. There should no blank line in the output.

Sample Input (of the original problem)


8 5 
RRRUULLURRRRDDRRUUU 
4 3 
RRRUU

Sample Output (of the original problem)

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 

2 2 3 2 
2 2 2 1 
This is the end of the original problem statement! Being lazy, we did not want to spend time to write a tester program for this problem, and decided to have you write this for us! 
Write a program that receives both input and output as one input test case, and write as output CORRECT or INCORRECT to indicate whether or not the output is correct.

Input

You read both input and output of the original problem from the standard input;it has each output just after each case's input of the original problem. 
Note that the output of original does not have formatting problems, i.e., 
The number of lines in the output file is correct and is as supposed to be. 
There are no leading or trailing white space characters in output lines. 
Wall specifications are correct, meaning that the four numbers correctly specify a possible wall within the boundary of the maze.

Output

Your program should write a single line for each test case of the input containing a single word CORRECT or INCORRECT, indicating the original problem has correctly produced the output for that test case or not.

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
题目大意:给你一个地图,上面有墙,其中x1 y1 x2 y2代表一面墙分割了x1,y1与x2,y2
                   给你一个序列(终点不固定),请问这是否是唯一的最短的路径,在地图中是否有墙多余?  (如果不满足其一输出“INCORRECT“)
 
试题分析:很好的一道搜索题,将每个点到终点、起点的距离全算出来,然后枚举不在这路径上的点,如果到终点距离+到起点距离<=字符串长度,不是唯一/最优
                    那么判断多余墙的只需要将这个墙砸掉,如果被墙隔开的两个方块的起点距离+终点距离>字符串长度 终点距离+起点距离>字符串长度那么这个墙是废的。
 
数据discuss中有
 
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int T;
int N,M;
char str[100001];
bool path[101][101];
bool to[101][101][4];//0:up 1:do 2:lf 3:rt
int toed[101][101];
int tobg[101][101];
bool vis[101][101];
int stx,sty;
int W;
int a1[1001],b1[1001],c1[1001],d1[1001]; struct data{
int x,y,st;
}Que[100001];
int dis[5][2]={{1,0},{0,1},{-1,0},{0,-1}}; void BFS(bool k){
int l=1,r=1;
Que[l].x=stx,Que[l].y=sty;
vis[stx][sty]=true;
while(l<=r){
int a=Que[l].x,b=Que[l].y;
//cout<<a<<" "<<b<<":"<<Que[l].st<<endl;
for(int i=0;i<4;i++){
//cout<<to[a][b][i]<<endl;
if(to[a][b][i]){continue;}
int xx=a+dis[i][0];
int yy=b+dis[i][1];
if(xx<0||yy<0||xx>N-1||yy>M-1||vis[xx][yy]) continue;
vis[xx][yy]=true;
if(k==0)toed[xx][yy]=Que[l].st+1;
else tobg[xx][yy]=Que[l].st+1;
Que[++r].st=Que[l].st+1;
Que[r].x=xx,Que[r].y=yy;
}
l++;
}
return ;
} int main(){
T=read();
while(T--){
N=read(),M=read();
cin>>str;int len=strlen(str);
memset(to,0,sizeof(to));
memset(path,0,sizeof(path));
memset(toed,0,sizeof(toed));
memset(tobg,0,sizeof(tobg));
for(int i=0;i<N;i++)
to[i][0][3]=to[i][M-1][1]=1;
for(int i=0;i<M;i++)
to[0][i][2]=to[N-1][i][0]=1;
stx=0,sty=0;
path[stx][sty]=true;
for(int i=0;i<len;i++){
if(str[i]=='R') stx++;
if(str[i]=='L') stx--;
if(str[i]=='D') sty--;
if(str[i]=='U') sty++;
path[stx][sty]=true;
}
W=read();
for(int i=1;i<=W;i++){
int a=read(),b=read(),c=read(),d=read();
if(b<d){
to[a][b][1]=true;
to[c][d][3]=true;
}
if(b>d){
to[a][b][3]=true;
to[c][d][1]=true;
}
if(a<c){
to[a][b][0]=true;
to[c][d][2]=true;
}
if(a>c){
to[a][b][2]=true;
to[c][d][0]=true;
}
a1[i]=a,b1[i]=b,c1[i]=c,d1[i]=d;
}
memset(vis,0,sizeof(vis));
BFS(0);
memset(vis,0,sizeof(vis));
stx=0,sty=0;
BFS(1);
bool flag=false;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(!path[i][j]){
if(tobg[i][j]+toed[i][j]<=len){
flag=true;
//break;
}
}
//if(!path[i][j])cout<<i<<" "<<j<<":"<<tobg[i][j]<<" "<<toed[i][j]<<endl;
}
//if(flag) break;
}
if(flag){
puts("INCORRECT");
continue;
}
for(int i=1;i<=W;i++){
if((tobg[a1[i]][b1[i]]+toed[c1[i]][d1[i]]>len)&&(tobg[c1[i]][d1[i]]+toed[a1[i]][b1[i]]>len))
{flag=true;break;}
}
if(flag){
puts("INCORRECT");
continue;
}
puts("CORRECT");
}
}

【BFS】Tester Program的更多相关文章

  1. 【BFS】Help the Princess!

    题目描述 The people of a certain kingdom make a revolution against the bad government of the princess. T ...

  2. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  3. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  4. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  5. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  6. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  7. 【bfs】迷宫问题

    [题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...

  8. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  9. 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz

    最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...

随机推荐

  1. [Unity]游戏Inside中的Chromatic Aberration效果学习

    Chromatic Aberration效果指的是模拟摄像机的拍摄瑕疵导致rgb三个通道的颜色发生了偏移,如 传统的Chromatic Aberration实现往往是基于一个后处理,将rgb采样的坐标 ...

  2. Vue基本指令

    模板对象 vue指令 一:模板对象 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  3. TCP之非阻塞connect和accept

    套接字的默认状态是阻塞的,这就意味着当发出一个不能立即完成的套接字调用时,其进程将被投入睡眠,等待响应操作完成,可能阻塞的套接字调用可分为以下四类: (1) 输入操作,包括read,readv,rec ...

  4. Linux 入门记录:六、Linux 硬件相关概念(硬盘、磁盘、磁道、柱面、磁头、扇区、分区、MBR、GPT)

    一.硬盘 硬盘的功能相当简单但很重要,它负责记录系统所需要的各种数据.硬盘记录数据有两个方面,一个是硬件方面的存储原理和结构,另外一方面则是软件方面的数据和文件系统.硬盘的主要行为就是数据的存放和取出 ...

  5. shell 智能获取历史记录功能

    vim ~/.inputrc 文件内容: "\e[A": history-search-backward"\e[B": history-search-forwa ...

  6. 图论-强连通分量-Tarjan算法

    有关概念: 如果图中两个结点可以相互通达,则称两个结点强连通. 如果有向图G的每两个结点都强连通,称G是一个强连通图. 有向图的极大强连通子图(没有被其他强连通子图包含),称为强连通分量.(这个定义在 ...

  7. 【软件设计】UML类图怎么看

    前言 无论使用哪种语言,都离不开面向过程与面向对象两个流派,而类图是面向对象程序设计中至关重要的一种软件表达形式,如何看懂类图,并设计好的软件架构,是我们作为软件工程师必不可少的技能之一. 今天小黑把 ...

  8. 修改centos地址连接为自动连接

    1.进入目录/etc/sysconfig/network-scripts/ 2.修改ifcfg-etn0 文件   (即你的网卡标识命名的配置文件) 3.将ONBOOT=no改成yes 4.保存后重启 ...

  9. C++——初识C++

    1. C关键字 auto int double long char float short signed unsigned struct union enum static switch case d ...

  10. Python中使用dom模块生成XML文件示例

    在Python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文件,下一篇文章再继续介绍Dom解析XML文件. 在生成XML文件中,我们主 ...