连连看
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29378 Accepted Submission(s): 7330
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 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 YES
Author
lwg
BFS代码,请仔细理解vis数组的设定(用优先队列可以优化二维),折叠次数:从初始点出发不算折叠,之后,依次计算折叠次数,所以初始化折叠次数为-1,原点方向为-1,来消除初始点的影响
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int MAXN = 1e3 + 5;
int n, m;
int O[MAXN][MAXN];
bool vis[MAXN][MAXN][4][4];//第一二维表示x,y坐标,第三维表示从哪个方向来的,第四维表示到达当前位置的拐弯次数
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int a1, a2, b1, b2, q;
bool sc;
struct o {
int x, y, fl, z, d;
o() {}
o(int x, int y,int fl,int z, int d):x(x), y(y), fl(fl), z(z), d(d) {}
};
queue<o>Q;
bool BFS() {
memset(vis, false, sizeof(vis));
while(!Q.empty()) Q.pop();
Q.push(o(a1, a2, 0, -1, -1));
for(int i = 0; i < 4; i ++) {
for(int j = 0; j < 4; j ++) {
vis[a1][a2][i][j] = true;
}
}
while(!Q.empty()) {
o e = Q.front();
Q.pop();
if(e.fl && O[e.x][e.y] != 0) continue;
for(int i = 0; i < 4; i ++) {
int nx = e.x + dx[i];
int ny = e.y + dy[i];
int z = e.z;
if(e.d != i) z ++;
if(nx < 1 || ny < 1 || nx > n || ny > m || vis[nx][ny][i][z] || z > 2) continue;
if(nx == b1 && ny == b2) {
return true;
}
vis[nx][ny][i][z] = true;
Q.push(o(nx, ny, 1, z, i));
}
}
return false;
}
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", &O[i][j]);
}
}
scanf("%d", &q);
while(q --) {
sc = false;
scanf("%d%d%d%d", &a1, &a2, &b1, &b2);
if(O[a1][a2] == O[b1][b2] && O[a1][a2] && O[b1][b2])
sc = BFS();
printf(sc ? "YES\n":"NO\n");
}
}
return 0;
}
DFS代码与BFS大同小异,但是四维一般是免不了的,否则应该需要加各种剪枝较为麻烦
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int MAXN = 1e3 + 5;
int n, m;
int O[MAXN][MAXN];
bool vis[MAXN][MAXN][4][4];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int a1, a2, b1, b2, q;
bool sc;
void DFS(int x, int y, int flag,int d, int z) {
if(x == b1 && y == b2) {
sc = true;
return;
}
if((flag && O[x][y] != 0) || sc) return;
for(int i = 0; i < 4; i ++) {
int nx = x + dx[i];
int ny = y + dy[i];
int nz = z;
if(d != i) nz ++;
if(nx < 1 || ny < 1 || nx > n || ny > m || vis[nx][ny][i][nz] || nz > 2) continue;
vis[nx][ny][i][nz] = true;
DFS(nx, ny, 1, i, nz);
}
}
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", &O[i][j]);
}
}
scanf("%d", &q);
while(q --) {
memset(vis, false, sizeof(vis));
sc = false;
scanf("%d%d%d%d", &a1, &a2, &b1, &b2);
for(int i = 0; i < 4; i ++) {
for(int j = 0; j < 4; j ++) {
vis[a1][a2][i][j] = true;
}
}
if(O[a1][a2] == O[b1][b2] && O[a1][a2] && O[b1][b2])
DFS(a1, a2, 0, -1, -1);
printf(sc ? "YES\n":"NO\n");
}
}
return 0;
}