zoj2100-Seeding

/*
解题思路:首先记录下'.'的个数count,接着每遍历成功一个单元格就num++,当count=num说明全部遍历成功。
因为此题从(0,0)开始,但是不知道什么位置停止,所以不能将坐标作为终止条件。 
*/ 
#include<iostream>
using namespace std;
char map[100][100];
int book[100][100];
int m,n,count=0,num=0,flag=0;
int next[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
void dfs(int x,int y)
{
	int tx,ty,k;
	if(num==count)
	{
		flag=1;
		return ;
	}
	for(k=0;k<4;k++)
	{
		tx=x+next[k][0];
		ty=y+next[k][1];
		if(tx<0||tx>=n||ty<0||ty>=m)
			continue;
		if(map[tx][ty]!='S'&&book[tx][ty]==0)
		{
			num++;//访问一次就加一次
			book[tx][ty]=1;//标记为走过
			dfs(tx,ty);//进入下一层
			book[tx][ty]=0;//取消标记,不可缺少 
			num--;//跳出一次就减一次,不可缺少 
		}
	 } 
}
int main()
{
	int i,j;
	while(cin>>n>>m&&n&&m)
	{
		count=0;//对于循环输入的程序一定注意这些初始化放置在全局还是main中,重要!!之前失败的原因就是这个初始化 
		num=0;
		flag=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='.')
					count++;
			}
		}
		book[0][0]=1;
		num++;
		dfs(0,0);
		if(flag)
			cout<<"yes"<<endl;
		else
			cout<<"no"<<endl;
	
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值