HDU1035(DFS)

题目链接

 

AC代码:

#include<iostream>
#include<string.h>
#include<string>
using namespace std;
char M[15][15];
bool vis[15][15]={0};
int dir[150][2]={0};   //对应x,y 
int step[15][15]={0};
int ans=0;
int loop_before=0,loop=0;
void dfs(int x,int y)
{
	if(M[x][y]=='e')  //exit 
	{ 
		cout<<step[x][y]<<" step(s) to exit\n";
		return ;
	}
	vis[x][y]=1;  //标志走过
	int x_next=x+dir[M[x][y]][0]; //计算下一步坐标 
	int y_next=y+dir[M[x][y]][1];
	if(!vis[x_next][y_next])  //未走过 
	{
		step[x_next][y_next]=step[x][y]+1;
		dfs(x_next,y_next);
	}
	else
	{
		loop=step[x][y]-step[x_next][y_next]+1;
		loop_before=step[x_next][y_next];
		cout<<loop_before <<" step(s) before a loop of "<<loop<<" step(s)\n";
		return ;
	}
}
int main()
{
	int r,l,s;
	dir['N'][0]=-1,dir['N'][1]=0;
	dir['S'][0]=1,dir['S'][1]=0;
	dir['E'][0]=0,dir['E'][1]=1;
	dir['W'][0]=0,dir['W'][1]=-1;
	while((cin>>r>>l)&&r&&l)
	{
		int s;
		cin>>s;
		ans =loop_before=loop=0;
		memset(vis,0,sizeof(vis));
		memset(step,0,sizeof(step));
		for(int i=0;i<15;i++)
		{
			for(int j=0;j<15;j++)
			{
				M[i][j]='e';
			}
		}
		
		
		for(int i=1;i<=r;i++)
		{
			for(int j=1;j<=l;j++)
			{
				cin>>M[i][j];
			}
		}
			
		
		dfs(1,s);
		
		
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值