题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175

越学越不会,BFS还是很高级的。

连连看

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30994    Accepted Submission(s): 7694

Problem Description
“连
连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来
(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,
连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
 
Input

入数据有多组。每组数据的第一行有两个正整数n,m(0<n<=1000,0<m<1000),分别表示棋盘的行数与列数。在接
下来的n行中,每行有m个非负整数描述棋盘的方格分布。0表示这个位置没有棋子,正整数表示棋子的类型。接下来的一行是一个正整数
q(0<q<50),表示下面有q次询问。在接下来的q行里,每行有四个正整数x1,y1,x2,y2,表示询问第x1行y1列的棋子与第
x2行y2列的棋子能不能消去。n=0,m=0时,输入结束。
注意:询问之间无先后关系,都是针对当前状态的!
 
Output
每一组输入数据对应一行输出。如果能消去则输出"YES",不能则输出"NO"。
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
 
Author
lwg
 
结题报告:
每个点可以多次走,那么我的vis[][]就要加一维vis[][][4],这个点,是否从某一个方向走过。结构体不仅要x,y方位,还要有dire方向,turn 转过几次。一般这种要在某一定限制条件下的BFS,可以有个优化。
好题,好好揣摩。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; const int MAXN = ;
int maps[MAXN][MAXN];
bool visited[MAXN][MAXN][];
int M, N;
const int to[][] = { { , }, { , }, { -, }, { , - } };
struct Node
{
int x, y;
int dire, turn;
}; void init()
{
for (int i = ; i <= N; i++)
{
for (int j = ; j <= M; j++)
{
scanf("%d", &maps[i][j]);
}
}
} bool BFS(int x1, int y1, int x2, int y2)
{
memset(visited, false, sizeof(visited));
queue<Node> que;
Node s;
s.x = x1;
s.y = y1;
s.dire = -;
s.turn = -;
que.push(s); for (int i = ; i < ; i++)
visited[x1][y1][i] = true; while (!que.empty())
{
Node u = que.front();
que.pop();
if (u.turn > )
continue;
for (int j = ; j < ; j++)
{
Node next;
next.turn = u.turn;
if (u.dire != j)
{
next.turn++;
}
for (int k = ;; k++)
{
next.x = u.x + to[j][] * k;
next.y = u.y + to[j][] * k;
if (next.x <= || next.y <= || next.x > N || next.y > M)
break;
if (next.x == x2 && next.y == y2)
{
if (next.turn <= )
{
return true;
}
}
if (maps[next.x][next.y] > || visited[next.x][next.y][j])
{
break;
}
next.dire = j;
que.push(next);
visited[next.x][next.y][j] = true;
}
}
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
int Q, x1, y1, x2, y2;
while (scanf("%d%d", &N, &M) == )
{
if (N == && M == )
{
break;
}
init();
scanf("%d", &Q);
for (int i = ; i < Q; i++)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (!maps[x1][y1] || !maps[x2][y2] || maps[x1][y1] != maps[x2][y2])
{
puts("NO");
continue;
}
if (BFS(x1, y1, x2, y2))
{
puts("YES");
}
else
{
puts("NO");
}
}
}
return ;
}

HDU(1175),连连看,BFS的更多相关文章

  1. HDU 1175 连连看(超级经典的bfs之一)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others)     ...

  2. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

  3. HDU - 1175 连连看 【DFS】【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1175 思路 这种题一想到就用搜索, 但是内存是32m 用 bfs 会不会MLE 没错 第一次 BFS的 ...

  4. HDU 1175 连连看(BFS)

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. HDU 1175 连连看【BFS】

    题意:给出起点和终点的棋子,不能经过别的棋子,而且转弯的次数不能超过2次,问能否消除 和逃离迷宫一样,每个节点记录下来它的当前的方向和转弯的次数,再搜 注意特判起点的棋子和终点的棋子为0或者不一样的情 ...

  6. HDU 1175 连连看

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. hdu 1175 连连看 DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...

  8. Hdu 1175 连连看(DFS)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...

  9. HDU 1175 连连看 (深搜+剪枝)

    题目链接 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以 ...

  10. hdu 1175 连连看 (深搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以 ...

随机推荐

  1. 最大权闭合图(Road constructions)hdu3917

    题意:给出n个城市,k条道路,每条道路都有其负责的公司和花费,m个公司来投标修路,给出m个公司承包需要交纳的赋税,如果第i个公司负责修1-->2路,第j个公司负责修2-->3路,如果选择了 ...

  2. poj 1176 Party Lamps

    http://poj.org/problem?id=1176 Party Lamps Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  3. [转]JVM内幕:Java虚拟机详解

    本文由 ImportNew - 挖坑的张师傅 翻译自 jamesdbloom.欢迎加入翻译小组.转载请见文末要求. 这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 ...

  4. 动画--过渡所需时间 transition-duration

    transition-duration属性主要用来设置一个属性过渡到另一个属性所需的时间,也就是从旧属性过渡到新属性花费的时间长度,俗称持续时间. 案例演示: 在鼠标悬停(hover)状态下,让容器从 ...

  5. mysql explain

    我们使用EXPLAIN解析SQL执行计划时,如果有下面几种情况,就需要特别关注下了: 首先看下 type 这列的结果,如果有类型是 ALL 时,表示预计会进行全表扫描(full table scan) ...

  6. (转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates

    Setting Display Formats Using Microsoft Excel: Right-click on any desired cell and select Format Cel ...

  7. R12 供应商SQL

    -- 供应商主表数据: SELECT ass.vendor_id vendor_id, ass.party_id party_id, ass.segment1 vendor_code, ass.ven ...

  8. 夺命雷公狗---DEDECMS----30dedecms数据dede_archives主表进行查询l操作

    在plus目录下编写一个test2.php的文件,取出dede_archives的所有信息 <?php //编写test2.php这个文件,主要是为了实现可以取出dede_archives表的所 ...

  9. archlinux锁屏

    启动管理器用的是 slim 发现锁屏可以用 slimlock

  10. jquery 下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...