NYOJ 499

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值