NYOJ 499
法一:
#include<stdio.h>
#include<string.h>
int a[10][10], count, n, m;
int b[10][10];
void DFS(int x, int y)
{
if(x > n || x < 1 || y > m || y < 1 || a[x][y] == 1)
{
return ;
}
if(x == n && y == m)
{
count++;
return ;
}
printf("%d,%d\n",x,y);
a[x][y] = 1;
DFS(x + 1, y);
DFS(x - 1, y);
DFS(x, y + 1);
DFS(x, y - 1);
a[x][y] = 0;
}
int main()
{
int t, i, j;
scanf("%d", &t);
while(t--)
{
count = 0;
memset(b, 0, sizeof(b));
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
scanf("%d", &a[i][j]);
}
}
DFS(1, 1);
printf("%d\n", count);
}
return 0;
}
/*0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1*/
法二:
#include <stdio.h>
int count;
int dt[8][8];
int r,c;
void init()
{
int i;
for(i = 0 ; i <= r+1; i++)
{
dt[i][0] = 1;
dt[i][c+1] = 1;
}
for(i = 0; i <= c+1; i++)
{
dt[0][i] = 1;
dt[r+1][i] = 1;
}
}
void print()
{
int i,j;
for(i = 0; i <= r+1; i++)
{
for(j = 0; j <= c+1; j++)
printf("%d ",dt[i][j]);
printf("\n");
}
}
void dfs(int x,int y)
{
if(x == r && y == c)
{
count++;
return;
}
dt[x][y] = 1;
if(!dt[x+1][y]) dfs(x+1,y);
if(!dt[x][y+1]) dfs(x,y+1);
if(!dt[x-1][y]) dfs(x-1,y);
if(!dt[x][y-1]) dfs(x,y-1);
dt[x][y] = 0;
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&r,&c);
count = 0;
init();
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &dt[i+1][j+1]);
dfs(1,1);
printf("%d\n",count);
}
return 0;
}