ZOJ 2411 Link Link Look(BFS)
题目链接: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)的更多相关文章
- ZOJ 3675 Trim the Nails(bfs)
Trim the Nails Time Limit: 2 Seconds Memory Limit: 65536 KB Robert is clipping his fingernails. ...
- Symbolic link and hard link的区别(linux)
--Symbolic link and hard link的区别(linux) --------------------------------------------------2014/06/10 ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
随机推荐
- JavaScript要点 (四)JSON
JSON 是用于存储和传输数据的格式. JSON 通常用于服务端向网页传递数据 . 什么是 JSON? JSON 英文全称 JavaScript Object Notation JSON 是一种轻量级 ...
- svn 如何解决冲突
项目中,往往不止你一人开发,多人开发,难免会有代码的冲突.彼此间谁也不能保证不会修改同个文件.如果修改了同个方法的内容.这时提交到svn是会提示代码冲突的. 当然,冲突是可控的,但不能避免.每次写代码 ...
- <转>linux 下stm32开发环境安装
传送门: http://www.eefocus.com/marianna/blog/13-10/298454_7e04f.html http://blog.sina.com.cn/s/blog_643 ...
- [React Fundamentals] Accessing Child Properties
When you're building your React components, you'll probably want to access child properties of the m ...
- UITableViewCell 高度自适应
UITableViewCell 高度自适应一直是我们做动态Cell高度时遇到的最烦躁的问题,Cell动态高度计算可以去看看sunny的这篇文章介绍,今天主要和大家分享下我在使用systemLayout ...
- [020]转--C++ swap函数
原文来自:http://www.cnblogs.com/xloogson/p/3360847.html 1.C++最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符 template < ...
- [Effective C++ --014]在资源管理类中小心copying行为
第一节 <背景> 条款13中讲到“资源取得的时机便是初始化时机”并由此引出“以对象管理资源”的概念.通常情况下使用std中的auto_ptr(智能指针)和tr1::shared_ptr(引 ...
- 【转】int const A::func()和int A::func() const
int const A::func() { return 0; }int A::func() const { return 0; } 上面的代码是合法的,其中A::func成员函数[只能在成员函数后面 ...
- Debug 之 VS2010网站生成成功,但是发布失败
用vs做好了网站.清理解决方案和重新生成解决方案都可以.但是发布不能成功.发布不能成功,有错误还好,郁闷的是竟然没有错误提示. 解决方法: 1.发布文件夹权限问题.重新找个地方建立一个发布文件夹即可. ...
- 自定义手势_GestureOverlayVIew
xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...