在杭电OJ平台看到连连看的题目,http://acm.hdu.edu.cn/showproblem.php?pid=1175
连连看
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
思路:两个点最多通过两次折点相连,可通过两个点各自出发划十字线,再增加划线(0或1条)判断是否可以相连:
图1
1、两点相邻,如图1标红的点,可消除。
图2 (2,1 和3,3)
图3 (2,1 和2,4)
2、两个点各自出发划十字线相交图2(或重合图3),有一个(0个)折点。
图4 (1,1和4,3)
3、两个点各自出发划十字线不相交,添加一条线图4,也两点十字线都相交,则有两个折点。
对比网上DFS算法,此方法理解及实现简单。
代码如下:
#include <iostream>
#include <vector>
#include <string.h>
#include <math.h>
using namespace std;
#define M 1010
int map[M][M];
int m, n;
struct position
{
int x;
int y;
};
void get_cross(int x, int y, vector<position> &v_pos)
{
int i = 0;
position tmp;
tmp.y = y;
for (i = x - 1; i > 0; i--)
{
if (0 == map[i][y])
{
tmp.x = i;
v_pos.push_back(tmp);
}
else
{
break;
}
}
for (i = x + 1; i <= m; i++)
{
if (0 == map[i][y])
{
tmp.x = i;
v_pos.push_back(tmp);
}
else
{
break;
}
}
tmp.x = x;
for (i = y - 1; i > 0; i--)
{
if (0 == map[x][i])
{
tmp.y = i;
v_pos.push_back(tmp);
}
else
{
break;
}
}
for (i = y + 1; i <= n; i++)
{
if (0 == map[x][i])
{
tmp.y = i;
v_pos.push_back(tmp);
}
else
{
break;
}
}
}
bool check_have_same_position(vector<position> v_pos_from,
vector<position> v_pos_to)
{
for (vector<position>::iterator itf = v_pos_from.begin();
itf != v_pos_from.end(); itf++)
{
for (vector<position>::iterator itt = v_pos_to.begin();
itt != v_pos_to.end(); itt++)
{
if (itf->x == itt->x && itf->y == itt->y)
return true;
}
}
return false;
}
int main()
{
while (cin >> m >> n)
{
memset(map, 0, sizeof(map));
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
cin >> map[i][j];
int num = 0;
cin >> num;
while (num--)
{
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
vector<position> v_pos_from;
vector<position> v_pos_to;
cin >> x1 >> y1 >> x2 >> y2;
// 起点终点相同,不能消除
if (x1 == x2 && y1 == y2)
{
cout << "NO" << endl;
continue;
}
// 地图之外的,不能消除
if (x1 < 1 || x1 > m || y1 < 1 || y1 > n
|| x2 < 1 || x2 > m || y2 < 1 || y2 > n)
{
cout << "NO" << endl;
continue;
}
// 起点终点相同且不等于0
if (map[x1][y1] == map[x2][y2] && map[x1][y1])
{
// 相邻
bool bflag = ((1 == abs(x1 - x2) && (y1 == y2))
|| (1 == abs(y1 - y2) && (x1 == x2)));
if (bflag)
{
cout << "YES" << endl;
continue;
}
// 取两点各自的十字
cout<<x1<<" "<<y1<<"aaaaaaa"<<endl;
get_cross(x1, y1, v_pos_from);
get_cross(x2, y2, v_pos_to);
// 两个点各自出发划十字线相交(或重合)
bool ret = check_have_same_position(v_pos_from, v_pos_to);
if (ret)
{
cout << "YES" << endl;
continue;
}
// 加一条线的情况,从一个十字中取点划十字,判断是否与另一个十字相交
for (vector<position>::iterator itf = v_pos_from.begin();
itf != v_pos_from.end(); itf++)
{
vector<position> v_pos_new;
get_cross(itf->x, itf->y, v_pos_new);
ret = check_have_same_position(v_pos_new, v_pos_to);
if (ret)
{
cout << "YES" << endl;
continue;
}
}
cout << "NO" << endl;
}
else
{
cout << "NO" << endl;
}
}
}
return 0;
}
运行效果: