连连看 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 ...
随机推荐
- GDI 设备环境句柄(2)
WM_PAINT 消息的触发 Windows 程序在以下情况会触发WM_PAINT消息: 窗口被移动导致被遮盖部分暴露出来 用户调整窗口的大小(当窗口类的 style 字段被设置为 CS_HREDRA ...
- 当li设置为line-block时,元素之间出现间隙的原因和解决方法
原因 因为浏览器默认把inline元素之间的空白符(Tab.空格.换行)渲染成一个空格.而如下述代码,两个li元素之间的换行符被渲染成一个空格,则元素之间产生了间隙. 用Chrome浏览器将场景模拟出 ...
- 3.3、Ansible命令参数详解
0.ansible 命令参数详解: [root@localhost ~]# ansible Usage: ansible <host-pattern> [options] Options: ...
- adb简单使用
一.基本命令 1.查看目前连接的设备/虚拟器的状态 adb devices
- NYIST 1083 美丽的校园
美丽的校园时间限制:1000 ms | 内存限制:65535 KB难度:3 描述 美丽的校园需要大家维护,作为南工学子,当然有责任! 在计科系门前有n棵树,按一行排列在计科系楼前面,它们在一条直线上, ...
- C/C++知识要点5——智能指针原理及自己定义实现
智能指针概述: 智能指针用来管理动态对象.其行为类似于常规指针,重要的差别是:它负责自己主动释放所指向的对象. C++ 11标准库提供两种智能指针:shared_ptr.unique_ptr 差别是: ...
- SQL语法精讲(包括建库、建表、建视图、查询、增加、删除、)
SQL语法精讲(包括建库.建表.建视图.查询.增加.删除.修改) SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELE ...
- 关于server和虚拟主机的差别
文章都是先由本人个人博客,孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新.掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/guan-yu-y ...
- 分享:Android系统的经常使用权限整理
1.ACCES_NETWORK_STATE 同意应用程序获取网络状态信息的权限 2.ACCESS_WIFI_STATE 同意应用程序获取Wi-Fi网络状态的权限 3.BAT ...
- Python+Django+SAE系列教程16-----cookie&session
本章我们来解说cookie和session ,这两个东西相信大家一定不陌生,概念就不多讲了,我们直接来看其使用方法,首先是cookie,我们在view中加入三个视图,一个是显示cookie的,一个是设 ...