2009年2月17日 星期二

Q825: Walking on the Safe Side

// Run time: 0.000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 101
int arr[N][N];
char str[1000];
void reset(int row, int col);
void assign(int row, int len);
void check(int row, int col);
int calculate(int row, int col);

int main()
{
int n, row, col;
int count, len;
int i;

scanf("%d", &n);
while (n--)
{
scanf("%d%d\n", &row, &col);

reset(row, col);

for ( i = 1; i <= row; i++)
{
gets(str);
len = strlen(str);
assign(i, len);
}

check(row, col);

count = calculate(row, col);
printf("%d\n", count);

if (n) printf("\n");
}

}

void reset(int row, int col)
{
int i, j;
for ( i = 1; i <= row; i++)
for ( j = 1; j <= col; j++)
arr[i][j] = 1;
}

void assign(int row, int len)
{
int i, tmp;
bool state = false;

for ( i = 0; i < len; i++)
{
if (str[i] == ' ') continue;
tmp = atoi(str+i);

if (state)
arr[row][tmp] = 0;
else
state = true;
}
}

void check(int row, int col)
{
// 因為只能往右或下不能往左或上
// 所以要做確認是否有誤
int i, index;

for ( i = 2; i < row; i++)
if (!arr[i][1])
break;
index = i;

if ( i != row)
for ( i = index; i <= row; i++)
arr[i][1] = 0;

for ( i = 2; i < col; i++)
if (!arr[1][i])
break;
index = i;

if ( i != col)
for ( i = index; i <= col; i++)
arr[1][i] = 0;
}
int calculate(int row, int col)
{
int i, j;

for ( i = 2; i <= row; i++)
for ( j = 2; j <= col; j++)
arr[i][j] = arr[i][j] ? arr[i][j-1] + arr[i-1][j] : 0;

return arr[row][col];
}

沒有留言: