您的位置:首页 > 其它

hdu 1175 连连看

2016-12-04 21:08 218 查看
警告:qdu14级信安同学不要复制此代码,否则会被查重

D - 连连看
Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u
Submit Status

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 Input

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


Sample Output
YES
NO
NO
NO
NO


这道题一开始用dfs做的,限制10S,也超时了……

然后用了bfs。其实就是套模板,只是加了转弯次数的判断

判断两个点能不能消除实质就是,看一个点能不能走到另一个点

前提这两个点必须相同。

每次枚举四个方向,符合条件的就加入队列

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n,m;
int a[1010][1010];
int book[1010][1010]; ///标记数组,标记有没有被走过
typedef struct zuobiao{
int x,y;
int direction; ///来到该点的方向
int zhuanzhe_cishu; ///来到该点转弯的次数
}zb;
queue<zb> q;
int zhuanhuan(int a,int b){ ///将4个方向重新编码,用来判断转没转弯
if(a == 0 && b == 1) return 1;
if(a == 1 && b == 0) return 2;
if(a == 0 && b == -1) return 3;
if(a == -1 && b == 0) return 4;
return 0;
}
void clearq(){
while(!q.empty()){
q.pop();
}
}
int bfs(int x1,int y1,int x2,int y2){
clearq();
book[x1][y1] = 1;
zb tmp;
tmp.x = x1;
tmp.y = y1;
tmp.direction = 0;
tmp.zhuanzhe_cishu = 0;
q.push(tmp);
while(!q.empty()){
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0} };
for(int i = 0; i < 4; ++i){
int tx = q.front().x + next[i][0];
int ty = q.front().y + next[i][1];
int zh = zhuanhuan(next[i][0],next[i][1]); ///找出当前前进的方向
if(tx < 1 || tx > n || ty < 1 || ty > m) continue;
if(tx == x2 && ty == y2){
if(zh == q.front().direction){ ///和上一次一个方向,可以允许转弯2次,
if(q.front().zhuanzhe_cishu <= 2){
printf("YES\n");
return 1;
}
}
else{
if(q.front().zhuanzhe_cishu < 2){///否则只能允许转弯次数小于2次,因为这一次他需要转弯
printf("YES\n");
return 1;
}
}
}
if(a[tx][ty] == 0 && book[tx][ty] == 0 && q.front().zhuanzhe_cishu <= 2){
book[tx][ty] = 1; ///必须走 0 ,转弯次数不超过2次
tmp.x = tx;
tmp.y = ty;
tmp.direction = zh;
if(q.front().direction == 0) tmp.zhuanzhe_cishu = 0;
else{
if(tmp.direction != q.front().direction) tmp.zhuanzhe_cishu = q.front().zhuanzhe_cishu+1;
else tmp.zhuanzhe_cishu = q.front().zhuanzhe_cishu;///方向和前一次不同就在前一次转弯次数上加一,
} ///否则等于前一次的转弯次数
q.push(tmp);
}

}
q.pop();
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m) && (n||m)){
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
scanf("%d",&a[i][j]);
int q;
scanf("%d",&q);
while(q--){
int x1,y1,x2,y2;
memset(book,0,sizeof(book));
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
///两个点必须相同,不同为NO
if(a[x1][y1] != a[x2][y2] || a[x1][y1]+a[x2][y2] == 0) printf("NO\n");
else{
int ans = bfs(x1,y1,x2,y2);
///如果bfs没搜到,就是NO
if(ans == 0) printf("NO\n");
}

}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 搜索