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 ...
随机推荐
- ZBUS = MQ + RPC
http://git.oschina.net/rushmore/zbus http://my.oschina.net/sbz/blog Readme.md 18.02 KB ZBUS = MQ + ...
- [iOS]C语言技术视频-11-指针变量练习一(交换值)
下载地址: 链接: http://pan.baidu.com/s/1pJIcGm3 密码: s83p
- hibernate--多对多双向关联(少用)
老师知道自己教了哪些学生, 学生也知道教自己的有哪些老师. Teacher.java: package com.bjsxt.hibernate; import java.util.HashSet; i ...
- 17.4.3 使用MulticastSocket实现多点广播(4)
17.4.3 使用MulticastSocket实现多点广播(4) 通过UserInfo类的封装,所有客户端只需要维护该UserInfo类的列表,程序就可以实现广播.发送私聊信息等功能.本程序底层通 ...
- Java笔记(二)
10. public protected default private 同一个类中 √ √ √ √ 同一个包中 √ √ √ 子类 √ √ 不同包中 √ 11. 线程: s ...
- (简单) POJ 3279 Fliptile,集合枚举。
Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...
- dsp与dmp的cookie mapping
dsp ad.com 在 meijiu.com上部署广告. 假设dmp叫cm.api.taobao.com 建立gid映射表 (1) ad.com在meiju.com的页面上部署,指向dmp ...
- LPC1788的IIC使用
#ifndef __IIC0_H_ #define __IIC0_H_ #include "common.h" #include "delay.h" //IIC ...
- iOS制作毛玻璃效果
//添加一个图片 UIImageView *imageview = [[UIImageView alloc]init]; imageview.frame = CGRectMake(10, 100, 3 ...
- 苹果App Store开发者帐户从申请,验证,到发布应用(3)
应用上架的流程和操作步骤 下面主要介绍一下,上架应用相关流程和相关的操作步骤: 1.登录itunes,https://itunesconnect.apple.com/WebObjects/iTun ...