UVA 825 - Walking on the Safe Side(重做)

这道题改天重新做一下:

高人的代码:

#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("i=%d j=%d f[i][j]=%d\n",i,j,f[i][j]);
}
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;
}

我的代码:(wrong answer)

#include<stdio.h>
#include<string.h>
#define MAXN 10110
int cros[MAXN][MAXN], f[MAXN][MAXN];
char scros[MAXN][2*MAXN];
int n, W, N, num;
void dp(int i, int j)
{
printf("i=%d j=%d num=%d\n",i, j, num);
if(i==W&&j==N) {num ++; return;}
if((cros[i][j+1] != -1) && j+1<=N)
dp(i,j+1);
if(cros[i+1][j] != -1 && i+1<=W)
dp(i+1,j);
}
void input()
{
scanf("%d",&n);
while(n --)
{
scanf("%d%d", &W, &N);
getchar();
for(int i = 1; i <= W; i ++)
{
for(int j = 1; j <= N; j ++)
{cros[i][j] = 0;}
}
for(int i = 1; i <= W; i ++)
{
gets(scros[i]);
int len = strlen(scros[i]);
for(int j = 0; j < len; j ++)
{
int t = 0;
if(scros[i][j] == ' '&& scros[i][j+1] <= '9'&&scros[i][j+1] >= '0')
{
sscanf(scros[i]+j+1,"%d",&t);
cros[i][t] = -1;
}
}
}
num = 0;
//mset(f,0,sizeof(f));
dp(1,1);
printf("%d\n",num);
puts("");
}
}
int main()
{
input();
return 0;
}





你可能感兴趣的:(ide)