题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1411

题目大意:连连看,给出每次连线的两个坐标,求能消去多少方块,拐弯最多2次

Sample Input

3 4
1 1 2 2
3 3 4 4
2 2 1 1
6
1 1 1 2
1 3 1 4
2 1 2 2
2 3 2 4
3 1 3 2
3 3 3 4
0 0

Sample Output

12

分析:连线可以从外围绕过去,用BFS求出两个坐标能够到达的最少拐弯次数。注意是最少拐弯次数,而不是最短距离

这道题目坑死我了,倒不是因为它的算法难,是一些小知识点

代码如下:

 # include<iostream>
# include<cstdio>
# include<cstring>
# include<queue>
using namespace std; int n,m;
int map[][];
bool vis[][];
int dx[]= {-,,,};
int dy[]= {,-,,};
struct node
{
int x,y,turn;
} st;
queue<node>q; bool BFS(int x1,int y1,int x2,int y2)
{
if(map[x1][y1]== || map[x2][y2]== ||map[x1][y1]!=map[x2][y2] ||x1==x2&&y1==y2)
return false;
st.x = x1;
st.y = y1;
while(!q.empty()) q.pop(); //坑死,while写成了if,半天没看出来
memset(vis,,sizeof(vis));
st.turn = -;
q.push(st);
while(!q.empty())
{
st = q.front();
q.pop(); node tmp;
for(int i=; i<; i++)
{
for(int j=;; j++)
{
tmp.x = st.x + j*dx[i];
tmp.y = st.y + j*dy[i];
tmp.turn = st.turn+;
if(tmp.x< || tmp.y< ||tmp.x>n+ ||tmp.y>m+) break;
if(map[tmp.x][tmp.y])
{
if(tmp.x==x2 && tmp.y==y2) return true;
break;
}
if(vis[tmp.x][tmp.y]) continue;
vis[tmp.x][tmp.y] = ;
if(tmp.turn<) //等于2的没必要加入队列了,他已经是叶子了
q.push(tmp);
}
}
}
return false;
} int main()
{
//freopen("in.txt","r",stdin);
int t,x1,y1,x2,y2;
while(scanf("%d%d",&n,&m) &&n &&m) //因为给出n、m为0结束,如果写不等于EOF会超时,错了好几次才反应过来
{
memset(map,,sizeof(map));
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&map[i][j]);
scanf("%d",&t);
int ans=;
while(t--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(BFS(x1,y1,x2,y2))
{
ans += ;
map[x1][y1] = ;
map[x2][y2] = ;
}
}
printf("%d\n",ans);
}
return ;
}

还有另外一种思路,具体看代码:

 #include "stdio.h"
int a[][];
int x1,y1,x2,y2;
int m,n;
int process()
{
int b[][];
int i,j;
int temp,pos;
for(i=;i<=n+;i++)
for(j=;j<=m+;j++)
b[i][j]=a[i][j];
if(b[x1][y1]!=b[x2][y2] || b[x1][y1]==) return ;
temp=;
while(x1+temp<=n+)
{
if(b[x1+temp][y1]==)
{
b[x1+temp][y1]=-;
temp++;
}
else if(x1+temp==x2 && y1==y2) return ;
else break;
}
temp=;
while(x1-temp>=)
{
if(b[x1-temp][y1]==)
{
b[x1-temp][y1]=-;
temp++;
}
else if(x1-temp==x2 && y1==y2) return ;
else break;
}
temp=;
while(y1+temp<=m+)
{
if(b[x1][y1+temp]==)
{
b[x1][y1+temp]=-;
temp++;
}
else if(x1==x2 && y1+temp==y2) return ;
else break;
}
temp=;
while(y1-temp>=)
{
if(b[x1][y1-temp]==)
{
b[x1][y1-temp]=-;
temp++;
}
else if(x1==x2 && y1-temp==y2) return ;
else break;
}
temp=;
while(x2+temp<=n+)
{
if(b[x2+temp][y2]==-) return ;
else if(b[x2+temp][y2]==)
{
b[x2+temp][y2]=-;
temp++;
}
else break;
}
temp=;
while(x2-temp>=)
{
if(b[x2-temp][y2]==-) return ;
else if(b[x2-temp][y2]==)
{
b[x2-temp][y2]=-;
temp++;
}
else break;
}
temp=;
while(y2+temp<=m+)
{
if(b[x2][y2+temp]==-) return ;
else if(b[x2][y2+temp]==)
{
b[x2][y2+temp]=-;
temp++;
}
else break;
}
temp=;
while(y2-temp>=)
{
if(b[x2][y2-temp]==-) return ;
else if(b[x2][y2-temp]==)
{
b[x2][y2-temp]=-;
temp++;
}
else break;
}
if(y2<y1)
{
temp=;
while(x2+temp<=n+ && b[x2+temp][y2]==-)
{
pos=;
while(y2+pos<=m+ && b[x2+temp][y2+pos]==)
pos++;
if(y2+pos<=m+ && b[x2+temp][y2+pos]==-) return ;
temp++;
}
temp=;
while(x2-temp>= && b[x2-temp][y2]==-)
{
pos=;
while(y2+pos<=m+ && b[x2-temp][y2+pos]==)
pos++;
if(y2+pos<=m+ && b[x2-temp][y2+pos]==-) return ;
temp++;
}
}
else if(y2>y1)
{
temp=;
while(x2+temp<=n+ && b[x2+temp][y2]==-)
{
pos=;
while(y2-pos>= && b[x2+temp][y2-pos]==)
pos++;
if(y2-pos>= && b[x2+temp][y2-pos]==-) return ;
temp++;
}
temp=;
while(x2-temp>= && b[x2-temp][y2]==-)
{
pos=;
while(y2-pos>= && b[x2-temp][y2-pos]==)
pos++;
if(y2-pos>= && b[x2-temp][y2-pos]==-) return ;
temp++;
}
}
if(x2<x1)
{
temp=;
while(y2+temp<=m+ && b[x2][y2+temp]==-)
{
pos=;
while(x2+pos<=n+ && b[x2+pos][y2+temp]==)
pos++;
if(x2+pos<=n+ && b[x2+pos][y2+temp]==-) return ;
temp++;
}
temp=;
while(y2-temp>= && b[x2][y2-temp]==-)
{
pos=;
while(x2+pos<=n+ && b[x2+pos][y2-temp]==)
pos++;
if(x2+pos<=n+ && b[x2+pos][y2-temp]==-) return ;
temp++;
}
}
else if(x2>x1)
{
temp=;
while(y2+temp<=m+ && b[x2][y2+temp]==-)
{
pos=;
while(x2-pos>= && b[x2-pos][y2+temp]==)
pos++;
if(x2-pos>= && b[x2-pos][y2+temp]==-) return ;
temp++;
}
temp=;
while(y2-temp>= && b[x2][y2-temp]==-)
{
pos=;
while(x2-pos>= && b[x2-pos][y2-temp]==)
pos++;
if(x2-pos>= && b[x2-pos][y2-temp]==-) return ;
temp++;
}
}
return ;
}
int main()
{
int step;
int i,j;
int num;
scanf("%d%d",&n,&m);
while(n!= || m!=)
{
num=;
for(i=;i<=n+;i++)
for(j=;j<=m+;j++)
a[i][j]=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&a[i][j]);
scanf("%d",&step);
for(i=;i<step;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(process()==)
{
a[x1][y1]=;a[x2][y2]=;
num=num+;
}
}
printf("%d\n",num);
scanf("%d%d",&n,&m);
}
return ;
}

ZOJ 2411 Link Link Look(BFS)的更多相关文章

  1. ZOJ 3675 Trim the Nails(bfs)

    Trim the Nails Time Limit: 2 Seconds      Memory Limit: 65536 KB Robert is clipping his fingernails. ...

  2. Symbolic link and hard link的区别(linux)

    --Symbolic link and hard link的区别(linux) --------------------------------------------------2014/06/10 ...

  3. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  4. ZOJ 1093 Monkey and Banana (LIS)解题报告

    ZOJ  1093   Monkey and Banana  (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  5. ZOJ Problem Set - 3829Known Notation(贪心)

    ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...

  6. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  10. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

随机推荐

  1. Info.plist和pch文件的作用

  2. Cocos2d-x 3.x学习笔记(一):开始Cocos2d之旅

    首先,进入官网下载cocos2d-x:http://www.cocos.com/download/,当然你需要注册一个账号才可以下载. 接下来需要跟着官网的配置文档配置一下开发环境,不得不说,Coco ...

  3. Oracle- 日期加减

    加法 select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate,add_months(sysdate,1 ...

  4. 注入限制绕过<转>

    突然想我们是否可以用什么方法绕过SQL注入的限制呢?到网上考察了一下,提到的方法大多都是针对AND与“'”号和“=”号过滤的突破,虽然有点进步的地方,但还是有一些关键字没有绕过,由于我不常入侵网站所以 ...

  5. php empty()和isset()的区别<转载>

    在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...

  6. iOS开发——网络编程OC篇&GCDAsyncSocket编程

    GCDAsyncSocket编程 同上一篇文章一样,这里也是使用Socket实现一个聊天室,但是这里使用的是一个常用的框架实现的:GCDAsyncSocket 一:导入这个框架 二:声明这个Socke ...

  7. firefly 问题

    1.G:\servers\Python27\Lib\mimetypes.py reload(sys) sys.setdefaultencoding('gb18030') 2.G:\servers\Py ...

  8. php输出错误信息

    error_reporting(E_ALL);  ini_set('display_errors','on'); header("Content-Type:text/html;charset ...

  9. 显示/去掉CONSOLE窗口

    众所周知,控制台应用程序一般都会显示一个控制台窗口(虚拟DOS窗口),但很多时候控制台程序的执行逻辑根本不需要与用户进行交互,所以显示这个难看的窗口纯属多余,那么如何将它屏蔽掉呢?下面我向大家介绍一种 ...

  10. MyBatis6:MyBatis集成Spring事务管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事务管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事务的做法,本文的目的是在这个的基 ...