Illusive Chase

 

Time Limit: 2000 msMemory 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

 

 

虚幻的追逐

 

机器人猫汤姆在一个机器人展上展出,吸引了一群热情的年轻人,它被放置在一块m * n场地周围。一开始被关掉的Tom被观众中的一个志愿者放置在现场的某个任意点上。在节目开始的零点,汤姆被一个遥控器打开。可怜的汤姆看到了杰瑞在短距离内的全息幻象,他们之间的直线要么是垂直的,要么是水平的。田野中可能有障碍,但幻觉总是被放置在汤姆和幻觉之间的直接路径上,不会有障碍。汤姆试图接近杰瑞,但当他到达那里时,幻觉改变了位置,追逐继续。让我们把每个方向(上、下、左、右)的追逐称为追逐之旅。每次旅行从最后一个幻觉被发现的地方开始,到下一个幻觉被发现的地方结束。在几次追逐之后,全息幻象不再出现,可怜的汤姆不知道下一步该做什么。在这个时候,他得到的信号是,如果他回到他开始追逐的地方,一个真正的杰瑞正在睡觉,他可以抓住它。

 

为了简化这个问题,我们可以把这个场看作一个正方形的网格。有些方格布满了障碍物。在任何时刻,Tom和Jerry都处于网格的某个空方格中,因此他们之间的直接路径要么是水平的,要么是垂直的。假定每次汤姆看到的都是一个幻觉;他只要朝这四个方向中的一个方向移动,就能够到它,而且不会撞到障碍物。汤姆只走一步,就进入了一个相邻的方格。

 

问题是Tom的记录机制有点模糊,因此他在每次追逐过程中所走的步数被记录为整数间隔,例如向左2到5步。现在轮到你发送一个程序到汤姆的记忆中帮助他回到过去。但是为了减轻您在这个比赛中的任务,您的程序应该只计算他可能开始追逐的所有可能位置。

 

输入

输入的第一行包含一个单一的整数t (1 <= t <= 10),测试用例的数量,然后是每个测试用例的输入数据。每个测试用例的第一行包含两个整数m和n,分别是网格的行数和列数(1 <= m, n <= 100)。接下来,有m线,每个包含n个整数,要么是0或1,显示相应的网格单元是否空(0)或(1)被一个障碍。字段的描述后,有一个序列行,每个对应一个追逐旅行汤姆(秩序)。每一行包含两个正整数,一起指定汤姆已经采取一系列措施(包容),紧随其后的是一个大写字母的字符指示的方向追逐旅行,这是一个4 R(右)的情况下,L(左),U(长达)和D(下)。(注意,这些方向是相对于字段的,而不是Tom转向的方向)。测试用例的这一部分以一行恰好包含两个0结束。

 

输出

对于每个测试用例,应该有单行,包含一个整数,指示Tom可能开始追逐的单元格的数量。

 

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

ZOJ 1019 Illusive Chase_C语言

 

代码:

#include<iostream>
#include<cstdio>
using namespace std;
int m,n,p;
int mp[105][105];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct node
{
int a;
int b;
int val;
}nod[10005];
int dfs(int x,int y,int step)
{
if(step==p)
return 1;
int a=nod[step].a;
int b=nod[step].b;
int dd=nod[step].val;
int cx,cy;
for(int i=1;i<a;i++)
{
cx=x+dir[dd][0]*i,cy=y+dir[dd][1]*i;
if(mp[cx][cy])
return 0;
if(cx<0||cx>=m||cy<0||cy>=n)
return 0;
}

for(int i=a;i<=b;i++)
{
cx=x+dir[dd][0]*i;
cy=y+dir[dd][1]*i;
if(cx<0||cx>=m||cy<0||cy>=n)
break;
if(mp[cx][cy])
break;
if(dfs(cx,cy,step+1))
return 1;
}
return 0;
}
int main()
{
int tes,i,j;
int a,b,val;
char c;
cin>>tes;
while(tes--)
{
cin>>m>>n;
p=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&mp[i][j]);
while(scanf("%d%d",&a,&b))
{
if(a==0&&b==0)
break;
scanf(" %c",&c);
if(c=='U')
val=0;
else if(c=='R')
val=1;
else if(c=='D')
val=2;
else
val=3;
nod[p].a=a;
nod[p].b=b;
nod[p++].val=val;
}
int res=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(mp[i][j]==0)
res+=dfs(i,j,0);
cout<<res<<endl;
}
return 0;
}