hdu有毒,考试上 AC 的就是一直 WA…

其实这道题是可以进行初始化来进行优化的,这样的话询问次数是可以达到 10510^5105 的。不过普通的 dfsdfsdfs + 剪枝也是可过的。

Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000 + 2;
int G[maxn][maxn], n, m, q, a, b, c, d;
bool vis[maxn][maxn];
bool dfs(int dir, int times, int x, int y)
{
if(x == c && y == d) return true;
if(vis[x][y] || (G[y][x] && (x != a || y != b))) return false; vis[x][y] = 1;
if(times > 3) return false;
if(y <= 0 || x <= 0 || y > n || x > m) return false;
if(times == 3)
{
if((dir == 1 || dir == 2) && y != d) return false;
if((dir == 3 || dir == 4) && x != c) return false;
if(dir == 1 && x < c) return false;
if(dir == 2 && x > c) return false;
if(dir == 3 && y < d) return false;
if(dir == 4 && y > d) return false; if(dir == 1){ if(dfs(dir, times, x - 1, y)) return true; }
if(dir == 2){ if(dfs(dir, times, x + 1, y)) return true; }
if(dir == 3){ if(dfs(dir, times, x, y - 1)) return true; }
if(dir == 4){ if(dfs(dir, times, x, y + 1)) return true; }
return false;
}
if(dir != 1) { if(dfs(1, times + 1, x - 1, y)) return true; }
else if(dfs(1, times, x - 1, y)) return true; if(dir != 2) { if(dfs(2, times + 1, x + 1, y)) return true; }
else if(dfs(2, times, x + 1, y)) return true; if(dir != 3) { if(dfs(3, times + 1, x, y - 1)) return true; }
else if(dfs(3, times, x, y - 1)) return true; if(dir != 4) { if(dfs(4, times + 1, x, y + 1)) return true; }
else if(dfs(4, times, x, y + 1)) return true; return false;
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
if(n == 0 && m == 0) break;
for(int i = 1;i <= n; ++i)
for(int j = 1;j <= m; ++j) scanf("%d",&G[i][j]);
scanf("%d",&q);
while(q--)
{
memset(vis, 0, sizeof(vis));
int flag = 0;
scanf("%d%d%d%d",&b,&a,&d,&c);
if(G[b][a] != G[d][c] || G[b][a] == 0 || G[d][c] == 0) flag = 1;
else
{
if(!dfs(0, 0, a, b)) flag = 1;
}
if(flag) printf("NO\n");
else printf("YES\n");
}
}
return 0;
}

连连看 HDU - 1175_搜索_剪枝的更多相关文章

  1. [CF293B]Distinct Paths_搜索_剪枝

    Distinct Paths 题目链接:http://codeforces.com/problemset/problem/293/B 数据范围:略. 题解: 带搜索的剪枝.... 想不到吧..... ...

  2. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

  4. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  5. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  6. ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)

    题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...

  7. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

  8. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  9. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

随机推荐

  1. eclipse中的maven项目部署到tomcat中

    http://www.cnblogs.com/guodefu909/p/4874549.html

  2. Scala 技术笔记之 可变长参数

    转自 http://www.cnblogs.com/rollenholt/p/4112833.html Scala 允许你指明函数的最后一个参数可以是重复的.这可以允许客户向函数传入可变长度参数列表. ...

  3. HDU2188 - 悼念512汶川大地震遇难同胞——选拔志愿者【巴什博弈】

    对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队,治疗和防疫的医护人员,以及进行心理疏导的心理学专家.根据要求,我校也有一个奔赴灾区救 ...

  4. 【codeforces 799A】Carrot Cakes

    [题目链接]:http://codeforces.com/contest/799/problem/A [题意] 你有一个烤炉; 每t秒能同时烤出k个蛋糕; 你可以在第一个烤炉在烤的时候;同时花费d秒建 ...

  5. Docker学习总结(14)——从代码到上线, 云端Docker化持续交付实践

    2016云栖大会·北京峰会于8月9号在国家会议中心拉开帷幕,在云栖社区开发者技术专场中,来自阿里云技术专家罗晶(瑶靖)为在场的听众带来<从代码到上线,云端Docker化持续交付实践>精彩分 ...

  6. C#中的public protected internal private

    1.常见的四种方位修饰符关系下图中的protected internal是并集的关系,意思是在命名空间内是internal的,在命名空间外是protected的 2.sealed final seal ...

  7. 升级后开机就提示“android.process.acore”停止执行 --分析 解决方式

    OTA升级的,升级引发的全部问题都是能够解释的,有的能解决,有的不能解决. 一个项目报了这个问题. 升级后开机就提示"android.process.acore"停止执行 抓取 a ...

  8. c/c++中sleep()函数毫秒级的实现

    近期看到好多人在问.c/c++中的sleep函数是秒级的,能不能实现毫秒级的呢?当然非常easy.我的写法例如以下 #include <stdio.h> #include <sys/ ...

  9. 全屏滚动实现:fullPage.js和fullPage

    fullPage.js和fullPage都能实现全屏滚动,二者差别是:fullPage.js需依赖于JQuery库,而fullPage不须要依赖不论什么一个js库.能够单独使用. 一.fullPage ...

  10. 数据库SQL Server2012笔记(三)——表的复杂查询

    1.数据分组--max/min/avg/sum/count select  avg(字段名),sum(字段名)  from  表名 select  count(*)  from  表名 select ...