题目链接: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. ELK beats通用配置说明(12th)

    Beats配置文件是以YAML语法,该文件包含用于所有的beats的通用配置选项,以及其特点的选项.下面说说通用的配置,特定的配置要看各自beat文档. 通用的配置如下几部分: Shipper Out ...

  2. Oracle- 查询误删数据

    使用flashback table能恢复误删数据. flashback table CONTAINER_CONTENT to timestamp to_timestamp('2010-06-30 22 ...

  3. 让Visual Studio 2015 支持ASP.NET MVC4.0.0.1

    近日装上了Visual Studio 2015 ,打开之前vs2013创建的MVC4的项目发现无法编译通过,提示System.Web.MVC,System.Web.WebPages 等找不到,网上搜索 ...

  4. CAS Tomcat实现单点登录

    转贴: http://www.cnblogs.com/ja-net/archive/2012/07/25/2608536.html 最近这两天在搞单点登录,第一次使用老出状况.以下是配置过程: 1.安 ...

  5. oracle查询前10条记录

    select * from table_name where rownum<11;

  6. android学习日记06--SurfaceView视图

    一.API关SurfaceView的介绍 SurfaceView是视图(View)的继承类,这个视图里内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸.Surfacev ...

  7. ant例子

    1.安装ant 下载解压→环境变量配置→cmd输入ant 出现 Buildfile: build.xml does not exist! 代表安装成功 参考文章:http://www.cnblogs. ...

  8. Servlet, Listener 、 Filter.

    Java Web的三大组件:Servlet, Listener . Filter. 使用Listener监听器:八大监听器: 第一组:用于监听Servlet三个域对象的创建与销毁 1. Servlet ...

  9. VIM7.3中文手册

    INDEX *index.txt*     For Vim version 7.3.  最近更新: 2010年7月 VIM 参考手册    by Bram Moolenaar 译者: Willis h ...

  10. Oracle取得中文拼音首字母函数

    CREATE ) ; V_RETURN ) ; FUNCTION F_NLSSORT (P_WORD IN VARCHAR2) RETURN VARCHAR2 AS BEGIN RETURN NLSS ...