连连看 HDU - 1175_搜索_剪枝
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_搜索_剪枝的更多相关文章
- [CF293B]Distinct Paths_搜索_剪枝
Distinct Paths 题目链接:http://codeforces.com/problemset/problem/293/B 数据范围:略. 题解: 带搜索的剪枝.... 想不到吧..... ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- 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 ...
- hdu 4848 搜索+剪枝 2014西安邀请赛
http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为 当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
随机推荐
- Windows批量查找文件
for /r 目录名 %i in (匹配模式1,匹配模式2) do @echo %i for /r SATA %i in (*.txt) do @echo %i D:\REY\test>for ...
- Codeforces Round #471 (Div. 2)B. Not simply beatiful strings
Let's call a string adorable if its letters can be realigned in such a way that they form two conseq ...
- webpack学习笔记(3)--webpack.config.js
module 参数 使用下面的实例来说明 module.exports = { module: { rules: [ { test: /\.css$/, use: 'css-loader' }, { ...
- BZOJ 3774 最优选择 (最小割+二分图)
题面传送门 题目大意:给你一个网格图,每个格子都有$a_{ij}$的代价和$b_{ij}$的回报,对于格子$ij$,想获得$b_{ij}$的回报,要么付出$a_{ij}$的代价,要么$ij$周围四联通 ...
- BZOJ 3110 [ZJOI2013]K大数查询 (整体二分+线段树)
和dynamic rankings这道题的思想一样 只不过是把树状数组换成线段树区间修改,求第$K$大的而不是第$K$小的 这道题还有负数,需要离散 #include <vector> # ...
- 使用Requests+正则表达式爬取猫眼TOP100电影并保存到文件或MongoDB,并下载图片
需要着重学习的地方:(1)爬取分页数据时,url链接的构建(2)保存json格式数据到文件,中文显示问题(3)线程池的使用(4)正则表达式的写法(5)根据图片url链接下载图片并保存(6)MongoD ...
- 【hdu 4135】Co-prime
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1 ...
- Mongodb学习总结(1)——常用NoSql数据库比较
虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 但是NoSQL数据库之间的不同,远超过两 SQ ...
- PL/SQL控制语句
本节要点: l 选择结构控制语句 if条件控制语句 Case语句 l 循环结构控制语句 基本loop循环 for循环 while循环 嵌套循环 PL/SQL既然是面向过程的编程语言,那么它就有针对 ...
- 【剑指Offer学习】【面试题63:二叉搜索树的第k个结点】
题目:给定一棵二叉搜索树,请找出当中的第k大的结点. 解题思路 假设依照中序遍历的顺序遍历一棵二叉搜索树,遍历序列的数值是递增排序的. 仅仅须要用中序遍历算法遍历一棵二叉搜索树.就非常easy找出它的 ...