连连看 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 ...
随机推荐
- selenium的显示等待和隐式等待区别
1.selenium的显示等待 原理:显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.(简而言之:就 ...
- 【BZOJ3065】带插入区间k小值
题意: 带插入,修改的区间k小值在线查询 原序列长度<=35000,插入个数<=35000,修改个数<=70000,0<=权值<=70000 题解: Orz vfleak ...
- C++ STL - queue使用详解
c++队列模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型. 下面详细介绍 ...
- OSI层次介绍
1.应用层:为应用软件提供接口,使应用程序能够使用网络服务. 2.表示层:①数据的解码和编码,②数据的加密和解密,③数据的压缩和解压缩. 3.会话层:建立.维护.管理应用程序之间的会话. 功能:对话控 ...
- 理解 Javascript 执行上下文和执行栈
如果你是一名 JavaScript 开发者,或者想要成为一名 JavaScript 开发者,那么你必须知道 JavaScript 程序内部的执行机制.理解执行上下文和执行栈同样有助于理解其他的 Jav ...
- centos solr 集群搭建
一.什么是 SolrCloud SolrCloud(solr 云)是 Solr 提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时 ...
- 最大团&稳定婚姻系列
[HDU] 1530 Maximum Clique 1435 Stable Match 3585 maximum shortest distance 二分+最大团 1522 Marriage is ...
- [SharePoint2010开发入门经典]10、使用SPS2010构建面向服务的应用程序
本章概要: 1.使用SPS自带的web service 2.构建自定义web service 3.使用不同的客户端解决方案部署自定义站点
- rails Installer之后的调整rails.bat等文件
rails Installer之后的调整rails.bat文件 出现系统找不到指定路径 学习了:http://www.jianshu.com/p/065355a731ee 修改rails.bat为 @ ...
- Android測试APP工具(一)
近期面试APP开发者的时候,遇到了技术总监问 APP測试的概念性问题.后面感觉主要的项目流程.项目逻辑.屏幕适配. 測试是全然没有问题的.可是对于APP的性能測试.压力測试等高端的測试.还是存在着美中 ...