Hdu1175(一起来连连看)

本文介绍了一种利用剪枝技术优化的深度优先搜索算法,用于解决迷宫寻路问题。通过限制转弯次数和路径判断,算法能够高效地找到从起点到终点的可能路径,特别适用于具有复杂障碍物的地图。文章详细展示了算法的实现过程,包括状态标记、方向移动和边界条件检查等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

 

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#include<math.h>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define close ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N=1000000+50;
const int INF=0x3f3f3f3f;
const double EPS = 1e-10;
ll mod = 1e9+7;

/*
用到了剪枝,很巧妙 
*/

int maze[1010][1010];
bool vis[1010][1010];
int sx,sy,ex,ey;
bool flag;
int n,m,q;
int dicx[]={1,-1,0,0};
int dicy[]={0,0,1,-1};

void dfs(int x, int y, int dic, int turns){
	if(turns > 2 || flag) return; //转弯次数不能超过2 
	
	if(turns == 2 && (x - ex) != 0 && (y - ey) != 0) return; //次数为2的情况下不在同一直线 排除 
	if(x == ex && y == ey && turns <= 2){
		flag = 1;
		return;
	}
	
	for(int i = 0; i < 4; i++){
		int xx = x + dicx[i];
		int yy = y + dicy[i];
		if(xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue; //边界情况
		if(maze[xx][yy] == 0 || (xx == ex && yy == ey)){
			vis[xx][yy] = 1;
			if(dic == -1 || dic == i) //如果在起点或者同向的情况turns不变及不转向 (同向为dir值) 
				dfs(xx,yy,i,turns);
			else 
				dfs(xx,yy,i,turns+1);//否则turns+1
			vis[xx][yy] = 0; 			
		} 
	}
	return;
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		if(n == 0 && m == 0) break;
		memset(maze,0,sizeof(maze));
		for(int i = 1; i <= n; i++){
			for(int j = 1;j <= m; j++){
				scanf("%d",&maze[i][j]);
			}
		}
		scanf("%d",&q);
		for(int i = 0; i < q; i++){
			scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
			memset(vis,0,sizeof(vis));
			
			flag = 0;
			if(maze[sx][sy] == maze[ex][ey] && maze[sx][sy])
				dfs(sx,sy,-1,0); //初始方向设为-1
				
			if(flag) printf("YES\n");
			else printf("NO\n"); 
		} 
	}
 	return 0;
}

/*
                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值