HDU1175(dfs)
连连看
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
Input
注意:询问之间无先后关系,都是针对当前状态的!
Output
Sample Input
3 4
1 2 3 4
0 0 0 0
4 3 2 1
4
1 1 3 4
1 1 2 4
1 1 3 3
2 1 2 4
3 4
0 1 4 3
0 2 4 1
0 0 0 0
2
1 1 2 4
1 3 2 3
0 0
Sample Output
YES
NO
NO
NO
NO
YES 搜索,也可以用枚举
//2016.8.20
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int mp[][], book[][];
int dx[] = {, , , -};
int dy[] = {, , -, };
int n, m, x2, y2;
bool ok, fg; void dfs(int x, int y, int pd, int tt)//pd表示上一次的方向,tt表示转折的次数
{
if(tt>)return ;
if(x == x2 && y == y2)
{
cout << "YES" << endl;
ok = true;
return ;
}
if(x<||x>n||y<||y>m||mp[x][y]!=)return;
if(ok)return ;
book[x][y] = ;
for(int i= ; i < ; i++)
{
int nx = x+dx[i];
int ny = y+dy[i];
if(book[nx][ny]==)continue;
if(i != pd)
dfs(nx, ny, i, tt+);
else
dfs(nx, ny, i, tt);
book[nx][ny] = ;
}
}
int main()
{
int q, x1, y1;
while(cin>>n>>m)
{
if(n==&&m==)break;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
scanf("%d", &mp[i][j]); cin >> q;
while(q--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
memset(book, , sizeof(book));
if(mp[x1][y1]==mp[x2][y2]&&mp[x1][y1]&&mp[x2][y2]&&x1>=&&y1>=&&x1<=n&&y1<=m&&x2>=&&y2>=&&x2<=n&&y2<=m)//剪枝
{
ok = false;
int tmp = mp[x1][y1];
mp[x1][y1] = ;
dfs(x1, y1, -, );
mp[x1][y1] = tmp;
if(!ok)cout << "NO" << endl;
}else
{
cout << "NO" <<endl;
}
}
} return ;
}
HDU1175(dfs)的更多相关文章
- HDU1175 连连看(DFS)
Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经 ...
- hdu1175连连看(dfs+细节)
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu1175 连连看(bfs疯狂MLE和T,遂考虑dfs+剪枝)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1175/ 题目大意就是给出地图,上面有若干的数,相当于连连看,给了q个查询,问给出的两个位置的数能否在两次转弯以内 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
随机推荐
- IMapControl3 Interface(1) Properties属性
IMapControl3 Interface Provides access to members that control the MapControl. Note: the IMapControl ...
- PAT (Advanced Level) 1017. Queueing at Bank (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- SVG页面loading动态图
https://github.com/SamHerbert/SVG-Loaders demo http://samherbert.net/svg-loaders/
- 0115percona-toolkit安装教程
一.percona-toolkit简介percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: 检查master和slav ...
- c语言 inline函数
大学在教科书上学习过inline函数,定义为inline函数之后,会省去函数调用的开销,直接嵌套汇编代码,取代函数调用,提高效率. google的google c++ style guide 1.in ...
- CentOS 7.0 安装配置 kafka 消息队列
查询下载最新版本 kafka http://kafka.apache.org/downloads.html wget http://mirror.bit.edu.cn/apache/kafka/0.8 ...
- EnablePrefetcher注册表项的修改与电脑提速
前些天在图书馆找教材,偶然发现一本windows dos命令应用技巧的书,发现了几个有用的注册表项 EnablePrefetcher这个注册表项是windows用来控制系统预读取数据开放程度的参数,其 ...
- 转:css中overflow:hidden 不起作用了吗?
css中overflow:hidden 不起作用了吗? 有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分并没有被隐藏,难道是设置的hidden失效了吗?其实看似 ...
- Memcached源码分析之assoc.c
#include "memcached.h" #include <sys/stat.h> #include <sys/socket.h> #include ...
- iOS调用相机,相册,上传头像 分类: ios技术 2015-04-14 11:23 256人阅读 评论(0) 收藏
一.新建工程 二.拖控件,创建映射 三.在.h中加入delegate @interface ViewController : UIViewController 复制代码 四.实现按钮事件 -(IBAc ...