hdu 1175 深搜

简单深搜,主要记录转弯次数。

题目http://acm.hdu.edu.cn/showproblem.php?pid=1175

#include<stdio.h>
#include<string.h>
int mov[4][2] = {1,0,0,1,-1,0,0,-1};
int map[1001][1001];
int book[1001][1001];
int n,m,ex,ey;
int dfs(int sx,int sy,int turn,int dir)
{
	
	if(turn==3)
		return 0;
	for(int i = 0;i<4;i++)
	{
		if(turn==2 && dir!=i)
			continue;
		int next_x = sx+mov[i][0];
		int next_y = sy+mov[i][1];
		if(next_x==ex && next_y==ey)
			return 1;
		if(next_x<0 || next_x>=n || next_y<0 || next_y>=m || book[next_x][next_y])
			continue;
		if(map[next_x][next_y]==0)
		{
			int t;
			t = (dir==i?turn:turn+1);
			book[next_x][next_y] = 1;
			if(dfs(next_x,next_y,t,i)==1)
			{
				return 1;
			}
			book[next_x][next_y] = 0;
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d%d",&n,&m) && n+m)
	{
		for(int i = 0;i<n;i++)
			for(int j = 0;j<m;j++)
				scanf("%d",&map[i][j]);
		int num;
		scanf("%d",&num);
		int sx,sy;
		while(num--)
		{
			memset(book,0,sizeof(book));
			scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
			sx = sx - 1;
			sy = sy - 1;
			ex = ex - 1;
			ey = ey - 1;
			if(map[sx][sy]!=map[ex][ey] || map[sx][sy]==0)
			{
				printf("NO\n");
			}
			else
			{
				printf("%s\n",dfs(sx,sy,-1,-1)==0?"NO":"YES");
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值