Running on Safe Path

New Delhi is a place for people to run anywhere easily. The two-way streets run North-South or East-West dividing the city into regular blocks. Most street intersections are safe for pedestrians to cross. In some of them, however, crossing is not safe and pedestrians are forced to use the available underground passages. Such intersections are avoided by walkers. The entry to the New Delhi garden is on the North-West corner of town, whereas the airport is on the South-East corner.

Suppose you want to go from the garden to the airport, and do not want to walk more than the required number of blocks. You also want to make your way avoiding the underground passages, that would introduce extra delay. Your task is to determine the number of different paths that you can follow from the garden to the airport, satisfying both requirements.

The example in the picture illustrates a city with 4 E-W streets and 5 N-S streets. Three intersections are marked as unsafe. The path from the garden to the airport is 3 + 4 = 7 blocks long and there are 4 such paths that avoid the underground passages.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input contains the number of East-West streets W and the number of North-South streets N. Each one of the following W lines starts with the number of an East-West street, followed by zero or more numbers of the North-South crossings which are unsafe. Streets are numbered from 1.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The number of different minimal paths from the garden to the airport avoiding underground passages.

Example

Sample Input

1

4 5
1
2 2
3 3 5
4

Sample Output

4

Time Limit: 2s

Size: 5000 B

Algorithm


Just do a breadth-first search from the upper left to the lower-right. For each new cell you visit, sum together the number of paths from all 4 directions that are of minimal distance.
Initialize a[0][0] to 1, as there\'s one way to get to (0,0).
Note that if (0,0) is blocked with an underground passage, then there are 0 ways to get to the station.
start
if(0th node block)
   print->0
   exit
else
   do bfs
   for i=0 to i=4 do
       total=total+add number of path from all direction with minimum distance
   print ->total
end

C Code

/** Code Written By: Ankit Babbar
    Date: 26 March,2011
    Quesn: Running on safe path
**/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAXN 200

char Road[MAXN][MAXN];
char f[MAXN][MAXN];
char str[MAXN];

struct ss {
    int r,  c;
    int move;
}q[MAXN*MAXN];

int qh, qt, R, C, Count;

int mx[] = {0,0,1,-1};
int my[] = {1,-1,0,0};

void Push(int r, int c, int move) {
    q[qh].r = r;
    q[qh].c = c;
    q[qh].move = move;
    qh ++;
    qh %= MAXN*MAXN;
}

ss Pop() {
    ss p;
    p.move = q[qt].move;
    p.r = q[qt].r;
    p.c = q[qt++].c;
    qt %=  MAXN*MAXN;
    return p;
}

int Bfs() {
    int i,  tr, tc;
    ss temp;
    f[0][0] = 1;
    qh = qt = 0;
    Push(0,0,0);
    while(qh != qt) {
        temp = Pop();
        for(i = 0; i<4; i++) {
            tr = temp.r + mx[i];
            tc = temp.c + my[i];
            if(tr>=R || tr<0 || tc>=C || tc<0) continue;
            if(tr == R-1 && tc == C-1) return temp.move+1;
            if(f[tr][tc] == 1|| Road[tr][tc]) continue;
            f[tr][tc] = 1;
            Push(tr,tc,temp.move+1);
        }
    }
    return 0;
}


void Ids(int r, int c, int min, int level) {
    int i;
    int nr, nc;
    if(r == R-1 && c == C-1) {
        Count ++;
        return;
    }
    if(level >= min) return;
    f[r][c] = 1;
    for(i = 0; i<4; i++) {
        nr = r + mx[i];
        nc = c + my[i];
        if(nr>=R || nr<0 || nc>= C || nc <0) continue;
        //if(nr<r || nc<c) continue;
        if(f[nr][nc] || Road[nr][nc]) continue;
        Ids(nr,nc, min, level+1);
    }
    f[r][c] = 0;
}

void Cal() {
    int min, i, j;
    min = Bfs();
    if(!min) {
        printf("0\n");
        return;
    }
    Count = 0;
    for(i = 0; i<R; i++) {
        for(j = 0; j<C; j++)
            f[i][j] = 0;
    }
    Ids(0,0,min,0);
    printf("%d\n",Count);
}

void Reset() {
    int i, j;
    for(i = 0; i<R; i++) {
        for(j = 0; j<C; j++) {
            f[i][j] = Road[i][j] = 0;
        }
    }
}

void ReadCase() {
    int i, j;
    char *p;
    gets(str);
    sscanf(str,"%d%d",&R,&C);
    Reset();
    for(i = 0; i<R; i++) {
        gets(str);
        p = strtok(str," ");
        p = strtok(NULL, " ");
        while(p) {
            j = atoi(p);
            Road[i][j-1] = 1;
            p = strtok(NULL, " ");
        }
    }
}

int main() {
    char input[100];
    int kase;
    gets(input);
    sscanf(input,"%d",&kase);
    gets(input);
    while(kase--) {
        ReadCase();
        Cal();
        if(kase) {
            gets(input);
            putchar('\n');
        }

    }
}