连连看 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 ...
随机推荐
- bzoj 1207 [HNOI2004]打鼹鼠 小技巧
Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探 ...
- elasticsearch的mapping
PUT /website/article/1 { "post_date": "2017-01-01", "title": "my ...
- jmeter图片的下载
1.jmeter下载文件 首先添加一个线程组,然后在线程组里面添加一个http请求,因为是获取数据,所有是get请求,写好下载的地址 1.添加线程组 :右键测试计划,添加-Threads(Users) ...
- IOS - Ask for Application Badge permission ios8
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUs ...
- Jenkins学习总结(6)——了解DevOps的前世今生
DevOps是什么?从哪里来? DevOps的概念 DevOps一词的来自于Development和Operations的组合,突出重视软件开发人员和运维人员的沟通合作,通过自动化流程来使得软件构建. ...
- Makefile错误总结
自己在做嵌入式驱动时,编写makefile文件是犯的错及解决办法 问题1:makefile 3 missing separator.stop: 问题2:Nothing to be done for ' ...
- gem update --system
gem update --system 修改完gem sources之后,进行gem update: gem update --system 之后的输出: C:\Sites\test01>gem ...
- [Tailwind] Extending Tailwind with Responsive Custom Utility Classes
You are able to extend the custom css with hover, focus, group-hover, responsive variants class in t ...
- iOS_21团购_地图功能
终于效果图: 右下角的回到用户位置button: MapController控制器, 是主控制器左側dock上面的[地图]button相应的控制器, 继承自ShowDealDetailControll ...
- Github博客私人订制(一)
这是博客订制的第一步,简单博客的建立. (这里不做截图说明了,贴图实在是太累,有问题大家QQ私聊我) (一)创建Github账号 URL:https://github.com/ (二)登录Github ...