poj1024 bfs

23 篇文章 0 订阅

 

 

如题:http://poj.org/problem?id=1024

Tester Program
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2360 Accepted: 830

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)


2
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 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

 

 

思路:注意2点

      1.必须判断给出的路径是否是最短并且唯一

      2.判定图中不存在多余的墙

     

第一点:分别从起点和终点bfs一次,点(x,y)距离起点和终点的距离用d1[x][y]和d[x][y]表示。将给出的路径先记录下来,如果地图中一点(x,y)不再给出的路径中,并且

d[x][y]+d1[x][y]==最短路,那么最短路径不唯一。

第二点:如果某一面墙有用,那么最短路径一点是沿着墙走,遍历所有的墙,设墙的两边的点分别是(x1,y1),(x2,y2).如果d[x1][y1]+d1[x2][y2]>最短路并且d[x2][y2]+d1[x1][y1]>最短路,代表沿着这面墙走路径大于最短路,那么这个墙没用。

还有要注意,如果某一点四周全被围住,也就是某一点起点,终点无法到达,则肯定存在多余的墙,最短路肯定不会饶一个圈。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

typedef pair<int,int>P;
int W,H,M;
char s[10005];
int wall[105][105][5]; 
int ex,ey;
int d[105][105]; //d[i][j]:点i,j到终点ex,ey的最短距离
int d1[105][105]; //d1[i][j]:点i,j到起点0,0的最短距离
int shortpath[105][105];
int dir[][2]={0,1,0,-1,1,0,-1,0};

int find_end()
{
 ex=0,ey=0;
 int sum=0;
 int i;
 shortpath[ex][ey]=1;
 for(i=0;s[i]!='\0';i++)
 {
  if(s[i]=='R')
   ex++;
  else if(s[i]=='U')
   ey++;
  else if(s[i]=='L')
   ex--;
  else
   ey--;
  shortpath[ex][ey]=1;
  sum++;
 }
 return sum;
}
int Judge(int x,int y)
{
 if(x>=0&&y>=0&&x<W&&y<H)
  return 1;
 return 0;
}
int Iswall(int x1,int y1,int x2,int y2)  //(x1,y1)->(x2,y2)是否穿过了墙
{
 int i;
 for(i=1;i<=4;i++)
 if(wall[x1][y1][i]==x2*100+y2)
  return 1;
 return 0;
}
void bfs(int sx,int sy,int ex,int ey,int dis[105][105])
{
 queue<P>que;
 int inq[105][105]={0};
 que.push(P(sx,sy));
 inq[sx][sy]=1;
 while(!que.empty())
 {
  P p=que.front();
  int now_x=p.first;
  int now_y=p.second;
  que.pop();
  int i;
  for(i=0;i<4;i++)
  {
   int next_x=now_x+dir[i][0];
   int next_y=now_y+dir[i][1];
   if(Judge(next_x,next_y)&&!inq[next_x][next_y]&&!Iswall(now_x,now_y,next_x,next_y))
   {
    dis[next_x][next_y]=dis[now_x][now_y]+1;
    que.push(P(next_x,next_y));
    inq[next_x][next_y]=1;
   }
  }
 }
}

int main()
{
// freopen("C:\\1.txt","r",stdin);
 int t;
 cin>>t;
 int i,j;
 while(t--)
 {
  memset(wall,-1,sizeof(wall));
  memset(d,0,sizeof(d));
  memset(d1,0,sizeof(d1));
  cin>>W>>H;
  cin>>s;
  int step=find_end();
  cin>>M;
  for(i=1;i<=M;i++)
  {
   int x1,x2,y1,y2;
   scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
   int c1=1,c2=1;
   while(wall[x1][y1][c1]!=-1)
    c1++;
   wall[x1][y1][c1]=x2*100+y2;
   while(wall[x2][y2][c2]!=-1)
    c2++;
   wall[x2][y2][c2]=x1*100+y1;
  }
  bfs(0,0,ex,ey,d1);  //多有点到起点的最短距离d1
  bfs(ex,ey,0,0,d);
  int flag=0;
  for(i=0;i<W;i++)
  {
   for(j=0;j<H;j++)
   {
    if(d1[i][j]+d[i][j]==step&&shortpath[i][j]==0) //起点到i,j+i,j到终点的距离==最短路并且不再给出的最短路上
     flag=1;
    int cnt=1;
    while(wall[i][j][cnt]!=-1&&cnt<5)
    {
     int x1=i;
     int y1=j;
     int x2=wall[i][j][cnt]/100;
     int y2=wall[i][j][cnt]%100;
     if(d1[x1][y1]+d[x2][y2]>step&&d[x1][y1]+d1[x2][y2]>step)
     {
      flag=1;
      break;
     }
     cnt++;
    }
   }
  }
  for(i=0;i<W;i++)
   for(j=0;j<H;j++)
    if(i!=0&&j!=0&&d1[i][j]==0)
    {
     flag=1;
     break;
    }
  if(flag)
   printf("INCORRECT\n");
  else
   printf("CORRECT\n");
 }
 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值