连连看
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6496 Accepted Submission(s): 1660
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
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 1005;
int map[MAX][MAX];
int visit[MAX][MAX];
typedef struct{
int x, y, d, c;
}Node;
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m, n;
Node s_n, e_n;
bool bfs()
{
int i;
queue<Node> q;
q.push(s_n);
visit[s_n.x][s_n.y] = 0;
while(!q.empty())
{
Node t = q.front();
q.pop();
// cout<<"x="<<t.x<<" y="<<t.y<<" c="<<t.c<<" flag="<<visit[t.x][t.y]<<endl;
if(t.x == e_n.x && t.y == e_n.y && t.c <= 2)
return true;
for(i = 0; i < 4; i++)
{
Node cur;
cur.x = t.x + d[i][0];
cur.y = t.y + d[i][1];
cur.d = i;
cur.c = t.c;
if(cur.x >= 0 && cur.y >=0 && cur.x < n && cur.y < m &&
(cur.x == e_n.x && cur.y == e_n.y || map[cur.x][cur.y] == 0) && !visit[cur.x][cur.y])
{
if(cur.d != t.d)
{
cur.c = t.c + 1;
if(cur.c > 2)
continue;
}
q.push(cur);
}
}
visit[t.x][t.y] = 1;
}
return false;
}
int main()
{
int i, j, q;
while(cin>>n>>m)
{
if(!n && !m)
break;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
cin>>map[i][j];
cin>>q;
while(q--)
{
cin>>s_n.x>>s_n.y>>e_n.x>>e_n.y;
s_n.x--; s_n.y--;
e_n.x--; e_n.y--;
s_n.c = -1; s_n.d = -1;
if(s_n.x == e_n.x && s_n.y == e_n.y || map[s_n.x][s_n.y] != map[e_n.x][e_n.y] || !map[e_n.x][e_n.y])
{
cout<<"NO"<<endl;
continue;
}
memset(visit, 0, sizeof(visit));
if(bfs())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}
dfs过的代码,dfs太慢了
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 1005;
int map[MAX][MAX];
int visit[MAX][MAX];
typedef struct{
int x, y, d, c;
}Node;
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m, n;
Node s_n, e_n;
bool dfs(Node t)
{
if(t.x == e_n.x && t.y == e_n.y && t.c <= 2)
return true;
for(int i = 0; i < 4; i++)
{
Node cur;
cur.x = t.x + d[i][0];
cur.y = t.y + d[i][1];
cur.d = i;
cur.c = t.c;
if(cur.x >= 0 && cur.y >=0 && cur.x < n && cur.y < m &&
(cur.x == e_n.x && cur.y == e_n.y || map[cur.x][cur.y] == 0) && !visit[cur.x][cur.y])
{
if(cur.d != t.d)
{
cur.c = t.c + 1;
if(cur.c > 2)
continue;
}
visit[cur.x][cur.y] = 1; //做标识
if(dfs(cur))
return true;
visit[cur.x][cur.y] = 0; //回溯消除标识
}
}
return false;
}
int main()
{
int i, j, q;
while(cin>>n>>m)
{
if(!n && !m)
break;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
cin>>map[i][j];
cin>>q;
while(q--)
{
cin>>s_n.x>>s_n.y>>e_n.x>>e_n.y;
s_n.x--; s_n.y--;
e_n.x--; e_n.y--;
s_n.c = -1; s_n.d = -1;
if(s_n.x == e_n.x && s_n.y == e_n.y || map[s_n.x][s_n.y] != map[e_n.x][e_n.y] || !map[e_n.x][e_n.y])
{
cout<<"NO"<<endl;
continue;
}
memset(visit, 0, sizeof(visit));
if(dfs(s_n))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}