hdoj 1175 连连看(dfs+剪枝)

【题目大意】:题目给出一副连连看的图形,其中0代表没有物品,其余数字代表该位置的物品,规定,每次消除所经过的路径不能超过两次专向。另外,不能在图外进行转向。问,对于q个询问,是否能够消除。每一个询问都是对于初始图而言的,


【解题思路】:dfs+剪枝。其实没什么好说的,有几个要注意的地方,第一个是判重,第二个是记住最多仅能够进行两次转向。切记,在判断到达目标的时候,需要判断其转向次数是否超过两次,表示个人在此处wa了两次。
纯暴力版过了之后。有一个剪枝是:在转向两次之后,因为不可转向,所以接下来必须与之前的方式保持一致,这个优化减少了5s左右的时间,原先是7000ms,现在是2000ms。


【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

int stx[4]={-1,0,1,0};
int sty[4]={0,1,0,-1};
int a[1100][1100];
int vis[1100][1100];
int n,m,x,y,xx,yy,q,tmp;
bool flag;


bool solve(int x,int y,int xx,int yy,int cnt,int dir){
    int tx,ty;
    if (x==xx && y==yy && cnt<=2) {return true;}
    if (a[x][y]!=0 && cnt!=-1) return false;
    if (cnt>2) return false;
    else {
        if (cnt==2){   //优化部分
            vis[x][y]=tmp;
            tx=x+stx[dir];
            ty=y+sty[dir];
            if (tx>=0 && tx<n && ty>=0 && ty<m && vis[tx][ty]!=tmp){
                if (solve(tx,ty,xx,yy,cnt,dir)) return true;
                }
            vis[x][y]=0;
        }   //end
        else{
        vis[x][y]=tmp;
        for (int i=0; i<4; i++){
            tx=x+stx[i];
            ty=y+sty[i];
            if (tx>=0 && tx<n && ty>=0 && ty<m && vis[tx][ty]!=tmp){
                if (cnt==-1) {if (solve(tx,ty,xx,yy,cnt+1,i)) return true;}
                else {if (i!=dir) 
                    {if (solve(tx,ty,xx,yy,cnt+1,i)) return true;}
                        else {if (solve(tx,ty,xx,yy,cnt,dir)) return true;}
                }                     
            }
        }
        vis[x][y]=0;
        }      
    }
    return false; 
}

int main() {
    while (~scanf("%d%d",&n,&m)){
        if (n==0 && m==0) break;
        for (int i=0; i<n; i++){
            for (int j=0; j<m; j++)
               scanf("%d",&a[i][j]);
        }
        memset(vis,0,sizeof(vis));
        scanf("%d",&q);
        for (int i=1; i<=q; i++){
            scanf("%d%d%d%d",&x,&y,&xx,&yy);
            x--;
            y--;
            xx--;
            yy--;
            tmp=i;
            vis[x][y]=tmp;
            if (a[xx][yy]!=a[x][y] || (a[x][y]==a[xx][yy] && a[x][y]==0)) {printf("NO\n"); continue;}
            if (xx==x && yy==y) {printf("NO\n"); continue;}
            flag=false;
            if (solve(x,y,xx,yy,-1,-1)==true) {  
                printf("YES\n");
            }
            else printf("NO\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值