ZOJ1019解题报告

 
Illusive Chase

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audience of youngsters, placed around an m * n field. Tom which is turned off initially is placed in some arbitrary point in the field by a volunteer from the audience. At time zero of the show, Tom is turned on by a remote control. Poor Tom is shown a holographic illusion of Jerry in a short distance such that a direct path between them is either vertical or horizontal. There may be obstacles in the field, but the illusion is always placed such that in the direct path between Tom and the illusion, there would be no obstacles. Tom tries to reach Jerry, but as soon as he gets there, the illusion changes its place and the chase goes on. Let's call each chase in one direction (up, down, left, and right), a chase trip. Each trip starts from where the last illusion was deemed and ends where the next illusion is deemed out. After a number of chase trips, the holographic illusion no more shows up, and poor Tom wonders what to do next. At this time, he is signaled that for sure, if he returns to where he started the chase, a real Jerry is sleeping and he can catch it.

To simplify the problem, we can consider the field as a grid of squares. Some of the squares are occupied with obstacles. At any instant, Tom is in some unoccupied square of the grid and so is Jerry, such that the direct path between them is either horizontal or vertical. It's assumed that each time Tom is shown an illusion; he can reach it by moving only in one of the four directions, without bumping into an obstacle. Tom moves into an adjacent square of the grid by taking one and only one step.

The problem is that Tom's logging mechanism is a bit fuzzy, thus the number of steps he has taken in each chase trip is logged as an interval of integers, e.g. 2 to 5 steps to the left. Now is your turn to send a program to Tom's memory to help him go back. But to ease your task in this contest, your program should only count all possible places that he might have started the chase from.


Input


The first line of the input 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 contains two integers m and n, which are the number of rows and columns of the grid respectively (1 <= m, n <= 100). Next, there are m lines, each containing n integers which are either 0 or 1, indicating whether the corresponding cell of the grid is empty (0) or occupied by an obstacle (1). After description of the field, there is a sequence of lines, each corresponding to a chase trip of Tom (in order). Each line contains two positive integers which together specify the range of steps Tom has taken (inclusive), followed by a single upper-case character indicating the direction of the chase trip, which is one of the four cases of R (for right), L (for left), U (for up), and D (for down). (Note that these directions are relative to the field and are not directions to which Tom turns). This part of the test case is terminated by a line containing exactly two zeros.


Output

For each test case, there should be a single line, containing an integer indicating the number of cells that Tom might have started the chase from.


Sample Input

2
6 6
0 0 0 0 0 0
0 0 0 1 1 0
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 1 0 1
0 0 0 0 0 1
1 2 R
1 2 D
1 1 R
0 0
3 4
0 0 0 0
0 0 0 0
0 0 0 0
1 2 R
3 7 U
0 0


Sample Output

10
0

题目首先给出场地的地图,然后给出机器猫走的路线。计算机器猫可能的出发点有多少个。

场地为0的地方为空地,而为1的地方即是有障碍物。既然机器猫可以按照给出的操作走,那么出发点起码是在按操作走后不会遇到障碍的点。

这也是一个搜索算法的题目,从每个可能的出发点,按照操作的指令和范围走一下,看可不可行,如果可行,那么可能出发点的个数加1。

代码如下:

#include<stdio.h>
#define trip 10000
int index1,found,m,n,ans;
//地图 
int grid[101][101];
//显示上方有哪些阻碍
int top[101][101];
//显示左方有哪些阻碍
int left[101][101]; 
struct mystep
{
  int x,y;
  char ope;
}step[trip];
//初始化
void initial()
{
   int i,j;
   for(i=1;i<=100;i++)
   {
       for(j=0;j<=100;j++)
       {
           grid[i][j]=0;
           left[i][j]=0;
           top[i][j]=0; 
       } 
   } 
   found=0;
   ans=0;
   index1=0;
} 
int minFun(int temp1,int temp2)
{
   return temp1<temp2?temp1:temp2; 
}
int maxFun(int temp1,int temp2)
{
   return temp1>temp2?temp1:temp2; 
}
void search(int mynum,int i,int j)
{
    int i0,j0,p,q;
    if(found)
    {
        return; 
    }
    if(mynum==index1)
    {
       found=1;
       ans++;
       return; 
    }
    i0=i;
    j0=j;
    //沿某个方向行进此范围的距离 
    for(p=step[mynum].x;p<=step[mynum].y;p++)
    {
        i0=i;
        j0=j;
        switch(step[mynum].ope)
        {
              case 'L':j0-=p;break;
              case 'R':j0+=p;break;
              case 'U':i0-=p;break;
              case 'D':i0+=p;break;
        }
        
        //如果仍在范围内 
        if(i0>=1&&i0<=m&&j0>=1&&j0<=n)
        {
             if(i0==i)
             {
                 //即为水平方向
                 if(left[i][minFun(j0,j)-1]==left[i][maxFun(j0,j)])
                 {
                       search(mynum+1,i0,j0);
                 } 
             }
             if(j0==j)
             {
                 //即为竖直方向
                 if(top[minFun(i,i0)-1][j]==top[maxFun(i,i0)][j])
                 {
                       search(mynum+1,i0,j0);
                 } 
             }
        }
    }
}
int main()
{
   int testnum,i,x,y,j,k;
   char ope;
   scanf("%d",&testnum);
   for(i=0;i<testnum;i++)
   {
        initial();
        scanf("%d%d",&m,&n); 
        for(j=1;j<=m;j++)
        {
              for(k=1;k<=n;k++)
              {
                  scanf("%d",&grid[j][k]);
              } 
        }
        for(j=2;j<=m;j++)
        {
           for(k=1;k<=n;k++)
           {
               top[j][k]=grid[j][k]+top[j-1][k];
           } 
        }     
        for(j=1;j<=m;j++)
        {
           for(k=2;k<=n;k++)
           {
               left[j][k]=grid[j][k]+left[j][k-1]; 
           } 
        }
        while(1)
        {
              scanf("%d%d",&x,&y);
              if(x==0||y==0)
              {
                  break; 
              } 
              step[index1].x=x;
              step[index1].y=y;
              scanf(" %c",&ope);
              step[index1].ope=ope;
              
              index1++;
        }
        for(j=1;j<=m;j++)
        {
           for(k=1;k<=n;k++)
           {
               if(grid[j][k]==0)//如果此地无障碍 
               {
                     found=0;
                     search(0,j,k);
               } 
           } 
        }
        printf("%d\n",ans);
   }
 
   system("pause");
   return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值