首页 技术 正文
技术 2022年11月12日
0 收藏 867 点赞 4,321 浏览 2093 个字

连连看

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

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 Input3 41 2 3 40 0 0 04 3 2 141 1 3 41 1 2 41 1 3 32 1 2 43 40 1 4 30 2 4 10 0 0 021 1 2 4 1 3 2 30 0 Sample OutputYESNONONONOYES 分析:可以用BFS搜索。首先要查找的两个位置中的数字如果不等 或者 有一个是0的话,那么它们不能连起来。剩下的就要判断少于2次转弯能否连接起来。定义结构体中加上方向和转弯次数,运用BFS,100MS就能AC。 代码如下:

 # include<stdio.h>
# include<string.h>
# include<queue>
using namespace std;
int map[][];
int vis[][];
int n,m;
struct Node{
int x,y,dir,times; //dir表示方向 ,times表示转弯的次数
}st,en; queue<Node>q;
int dx[]={-,,,};
int dy[]={,-,,};
bool ismap(int x,int y){
if(x< || y< || x>n ||y>m)
return false;
return true;
}
void BFS(){
memset(vis,,sizeof(vis));
while(!q.empty()) q.pop();
vis[st.x][st.y] = ;
q.push(st);
bool tmp = false;
int i;
while(!q.empty()){
Node a = q.front(); q.pop();
if(a.x == en.x && a.y ==en.y){
tmp = true;
break;
}
for(i=;i<;i++){
Node b;
b.x = a.x + dx[i];
b.y = a.y + dy[i];
b.dir = i;
b.times = a.times;
if(!(a.dir==-||a.dir+b.dir==||a.dir==b.dir)) //a.dir+b.dir==3表示转180度的弯
b.times = a.times + ;
if(!ismap(b.x,b.y)) continue;
if(vis[b.x][b.y]) continue;
if(map[b.x][b.y] && !(b.x==en.x&&b.y==en.y)) continue; //很重要,如果不是空格子并且不是终点
if(b.times > ) continue;
vis[b.x][b.y] = ;
q.push(b);
}
}
if(tmp)
puts("YES");
else
puts("NO");
}
int main()
{
while(scanf("%d%d",&n,&m)&&n&&m){
int i,j,T;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&map[i][j]);
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&st.x,&st.y,&en.x,&en.y);
st.times = ; st.dir = -;
if(map[st.x][st.y] != map[en.x][en.y])
puts("NO");
else if(map[st.x][st.y] == || map[en.x][en.y] == )
puts("NO");
else
BFS();
}
}
return ;
}

BFS中,用到queue,如果头文件里边没有using namespace std;这句话,在VC里边会编译出错,郁闷。。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902