UVA 825 Walking on the Safe Side

UVA_825

    一个比较容易的计数问题,如果(i,j)这个点可以通过的话f[i][j]=f[i-1][j]+f[i][j-1],否则f[i][j]=0。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAXD 1010
char b[10010];
int N, M, G[MAXD][MAXD], f[MAXD][MAXD];
void solve()
{
int i, j, k;
gets(b);
sscanf(b, "%d%d", &N, &M);
memset(G, 0, sizeof(G));
for(i = 1; i <= N; i ++)
{
gets(b);
for(j = 0; isdigit(b[j]); j ++);
while(b[j])
{
if(isdigit(b[j]))
{
sscanf(b + j, "%d", &k);
G[i][k] = 1;
while(isdigit(b[j]))
j ++;
}
else
j ++;
}
}
for(i = 0; i <= N; i ++)
f[i][0] = 0;
for(i = 0; i <= M; i ++)
f[0][i] = 0;
f[1][0] = 1;
for(i = 1; i <= N; i ++)
for(j = 1; j <= M; j ++)
{
f[i][j] = 0;
if(!G[i][j])
{
f[i][j] += f[i - 1][j];
f[i][j] += f[i][j - 1];
}
}
printf("%d\n", f[N][M]);
}
int main()
{
int t;
gets(b);
sscanf(b, "%d", &t);
while(t)
{
gets(b);
solve();
if(-- t)
printf("\n");
}
return 0;
}


你可能感兴趣的:(ide)