Time Limit: 20000/10000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
输入数据有多组。每组数据的第一行有两个正整数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时,输入结束。
注意:询问之间无先后关系,都是针对当前状态的!
每一组输入数据对应一行输出。如果能消去则输出”YES”,不能则输出”NO”。
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
YES
NO
NO
NO
NO
YES
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1175
题意:中文题我就不多解释了
思路:这一题的关键就是统计转过了几次弯,就是将原本的记步数换成记转弯数,跟HDU-1728逃离迷宫 有些类似,可以看一下我写的关于这一题的博客,博客网址:https://blog.csdn.net/bestfm/article/details/79672593
这两题我写的代码有一处不一样,到达终点的判断语句放的位置有些不同,因为地图设置不一样,这一题的终点位置的表示方法跟可走位置的表示方法不同,所以要先判断是否到达终点。
#include
#include
#include
#include
using namespace std;
struct node
{
int x,y,pre;
};
int dt[1005][1005],h,l;
int xy[4][2]={0,1,0,-1,1,0,-1,0},temp[1005][1005];
void BFS(int xt,int yt,int xl,int yl)
{
queue it;
memset(temp,0,sizeof(temp));
if (dt[xt][yt]==0||dt[xl][yl]==0) // 排除一些不可能的情况
{
printf("NO\n");
return ;
}
else if (xt==xl&&yt==yl)
{
printf("NO\n");
return ;
}
else if (dt[xt][yt]!=dt[xl][yl])
{
printf("NO\n");
return ;
}
node o,e;
e.x=xt;
e.y=yt;
e.pre=0;
it.push(e);
temp[e.x][e.y]=1;
while (!it.empty())
{
e=it.front();
if (e.pre>2)
break;
it.pop();
for (int i=0;i<4;i++)
{
o.x=e.x+xy[i][0];
o.y=e.y+xy[i][1];
o.pre=e.pre+1;
while (1)
{
if (o.x==xl&&o.y==yl) // 终点判断要放在最前面
{
printf("YES\n");
return ;
}
if (dt[o.x][o.y]!=0||o.x<1||o.x>h||o.y<1||o.y>l)
break;
if (!temp[o.x][o.y])
{
it.push(o);
temp[o.x][o.y]=1;
}
o.x+=xy[i][0];
o.y+=xy[i][1];
}
}
}
printf("NO\n");
}
int main()
{
int n,xq,yq,xz,yz;
while (~scanf("%d %d",&h,&l),h!=0||l!=0)
{
for (int i=1;i<=h;i++)
for (int j=1;j<=l;j++)
scanf("%d",&dt[i][j]);
scanf("%d",&n);
while (n--)
{
scanf("%d %d %d %d",&xq,&yq,&xz,&yz);
BFS(xq,yq,xz,yz);
}
}
return 0;
}