转载地址:http://blog.163.com/max_xbw/blog/static/192669039201171415550309/
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
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
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
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
一看题目就知道是用bfs,但是要注意几个问题:bfs是按层次进行搜索,得到的从起点到终点的路径(如果存在的话)是最短的。题目中说这条路径最多只能转向2次,有一些情况可能得到了从起点到终点的路径,但是它的转向次数已经超过的2次,这样这条路径就不符合要求,得重新找一条。一个一般的结论:如果某一点记录的转向次数大于当前路径在该点的转向次数,那么还能从该点再发出一条路径来查找。可以用一个二维数组hash[n][n]来状态判重,这个数组里存的数值就是某条路径在该点的转向次数,if(hash[x][y]>=now.turn) q.push(now);还有个需要注意的就是能连上的点并没有从图中消失,所以每条查询语句都是独立的。(我把它当成真正的连连看来做,结果WA了10次)
#include <stdio.h>
int map[1001][1001];
int b[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int x1,x2,y1,y2;
int n,m;
int visited[1001][1001];
typedef struct
{
int x;
int y;
int count;
}node;
node pass[1000000];
int bfs()
{ int i;
int front;
int rear;
node a;
a.count=0;
front=0;
pass[0].x=x1;
pass[0].y=y1;
pass[0].count=0;
visited[x1][y1]=1;
rear=1;
while(front!=rear)
{
if(pass[front].count>=3)
return 0;
for(i=0;i<4;i++)
{
a.x=pass[front].x+b[i][0];
a.y=pass[front].y+b[i][1];
//printf("a.x%d a.y%d map[a.x][a.y]%d\n\n",a.x,a.y,map[a.x][a.y]);
while(a.x>=1&&a.x<=n&&a.y>=1&&a.y<=m&&map[a.x][a.y]==0)
{
if(visited[a.x][a.y]==0)
{
a.count=pass[front].count+1;
pass[rear]=a;
rear++;
visited[a.x][a.y]=1;
if(a.x==x2&&a.y==y2)
if(a.count<=3)
return 1;
else
return 0;
}
a.x+=b[i][0];// 访问过的点,不进入队列中,但仍可沿此方向访问,且此方向都是不转折的,其余均是转折
a.y+=b[i][1];
}
}
front++;
}
return 0;
}
int main()
{
int i,j;
int p;
int t;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n+m==0)
break;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&map[i][j]);
scanf("%d",&p);
while(p--)
{
for(i=1;i<1001;i++)
for(j=1;j<1001;j++)
visited[i][j]=0;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(map[x1][y1]!=map[x2][y2]||map[x1][y1]==0)
{
printf("NO\n");
continue;
}
else
{
t=map[x2][y2];
map[x2][y2]=0;
if(bfs())
printf("YES\n");
else
printf("NO\n");
}
map[x2][y2]=t;
}
}
return 0;
}
int map[1001][1001];
int b[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int x1,x2,y1,y2;
int n,m;
int visited[1001][1001];
typedef struct
{
int x;
int y;
int count;
}node;
node pass[1000000];
int bfs()
{ int i;
int front;
int rear;
node a;
a.count=0;
front=0;
pass[0].x=x1;
pass[0].y=y1;
pass[0].count=0;
visited[x1][y1]=1;
rear=1;
while(front!=rear)
{
if(pass[front].count>=3)
return 0;
for(i=0;i<4;i++)
{
a.x=pass[front].x+b[i][0];
a.y=pass[front].y+b[i][1];
//printf("a.x%d a.y%d map[a.x][a.y]%d\n\n",a.x,a.y,map[a.x][a.y]);
while(a.x>=1&&a.x<=n&&a.y>=1&&a.y<=m&&map[a.x][a.y]==0)
{
if(visited[a.x][a.y]==0)
{
a.count=pass[front].count+1;
pass[rear]=a;
rear++;
visited[a.x][a.y]=1;
if(a.x==x2&&a.y==y2)
if(a.count<=3)
return 1;
else
return 0;
}
a.x+=b[i][0];// 访问过的点,不进入队列中,但仍可沿此方向访问,且此方向都是不转折的,其余均是转折
a.y+=b[i][1];
}
}
front++;
}
return 0;
}
int main()
{
int i,j;
int p;
int t;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n+m==0)
break;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&map[i][j]);
scanf("%d",&p);
while(p--)
{
for(i=1;i<1001;i++)
for(j=1;j<1001;j++)
visited[i][j]=0;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(map[x1][y1]!=map[x2][y2]||map[x1][y1]==0)
{
printf("NO\n");
continue;
}
else
{
t=map[x2][y2];
map[x2][y2]=0;
if(bfs())
printf("YES\n");
else
printf("NO\n");
}
map[x2][y2]=t;
}
}
return 0;
}
提示:本题是一道bfs题,可是一直WA,只有在他人提示说bfs应该保证入队列的有序性,即保证后入队列的计数值一定大于等于先入队列的(这是bfs的关键)。此题我原先是一直向四个方向扩展到遇到非0或者已经访问过的点为止(即visited[i][j]==1为止)但是有一个问题(举例如下);1 0 3 4 0 0 0 0 4 3 2 1 从(1,1)开始向四周扩散,并假设为左、下、右、上,则点(1,2)的count为1,(2,1)点的count为1,(2,2)点的count为2,然后由(2,1)点扩散,遇到(2,2)点发现已访问,则终止,产生错误。因为其实(2,3)和(2,4)在count为2时应该也能入队列。要解决这个问题,就采用了以下方法。从一个点向四个方向发散,如果该点访问过,则不入队列,但是可以继续向该方向扩散。代码如下: http://acm.hdu.edu.cn/showproblem.php?pid=1175本题是一道bfs题,可是一直WA,只有在他人提示说bfs应该保证入队列的有序性,即保证后入队列的计数值一定大于等于先入队列的(这是bfs的关键)。此题我原先是一直向四个方向扩展到遇到非0或者已经访问过的点为止(即visited[i][j]==1为止)但是有一个问题(举例如下);1 0 3 40 0 0 04 3 2 1从(1,1)开始向四周扩散,并假设为左、下、右、上,则点(1,2)的count为1,(2,1)点的count为1,(2,2)点的count为2,然后由(2,1)点扩散,遇到(2,2)点发现已访问,则终止,产生错误。因为其实(2,3)和(2,4)在count为2时应该也能入队列。要解决这个问题,就采用了以下方法。从一个点向四个方向发散,如果该点访问过,则不入队列,但是可以继续向该方向扩散。
其他: 连连看大家都应该玩过吧,如果写一个连连看游戏,就要用到搜索的算法,下面就是这题的解决方法。注意:记得剪枝,不然会超时,你玩连连看游戏不会出现按两个东西要等很久才能消吧,所以剪枝很重要!
代码:
Cpp代码
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
int n, m, t, k, z;
int x1, y1, x2, y2;
int a[1005][1005];
bool map[1005][1005], flag;
void init() //初始化函数
{
flag = false;
memset(map, false, sizeof(map));
}
void dfs(int x, int y, int z, int k) //坐标(x,y)
{
if(flag) return;
if(x<=0 || y<=0 || x>n || y>m) return; //若超界,返回
if(k>=3) return; //若转弯数已经超过3次,返回
if(x == x2 && y == y2)
{
flag = true;
return;
}
if(k == 2) //超强剪枝:若已经转两次弯,则目标坐标必须要在前进方向的前面,否则直接返回
{
if( !(z==1 && x>x2 && y==y2 || z==2 && x<x2 && y==y2
|| z==3 && y>y2 && x==x2 || z==4 && y<y2 && x==x2) )
return;
}
if(a[x][y] != 0) return; //如果该点不是0,则不能走,返回
if(map[x][y]) return; //如果该点已经走过,返回
map[x][y] = true; //标记该点为走过
if(z == 1) //上
{
dfs(x-1, y, 1, k);
dfs(x+1, y, 2, k+1);
dfs(x, y-1, 3, k+1);
dfs(x, y+1, 4, k+1);
}
else if(z == 2) //下
{
dfs(x-1, y, 1, k+1);
dfs(x+1, y, 2, k);
dfs(x, y-1, 3, k+1);
dfs(x, y+1, 4, k+1);
}
else if(z == 3) //左
{
dfs(x-1, y, 1, k+1);
dfs(x+1, y, 2, k+1);
dfs(x, y-1, 3, k);
dfs(x, y+1, 4, k+1);
}
else if(z == 4) //右
{
dfs(x-1, y, 1, k+1);
dfs(x+1, y, 2, k+1);
dfs(x, y-1, 3, k+1);
dfs(x, y+1, 4, k);
}
map[x][y] = false; //若深搜不成功,标记该点为未走过
}
int main()
{
int i, j;
while(scanf("%d %d", &n, &m))
{
if(!n && !m) break;
for(i = 1; i <= n; i++)
for(j = 1 ; j <= m; j++)
scanf("%d", &a[i][j]);
scanf("%d", &t);
for(i = 0; i < t; i++)
{
init();
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if(x1 == x2 && y1 == y2 && a[x1][y1] != 0) //若两点坐标是相同的,输出NO
printf("NO\n");
else if(a[x1][y1] == a[x2][y2] && a[x1][y1] != 0) //若两点的值相同且不为0
{
dfs(x1-1, y1, 1, 0);
dfs(x1+1, y1, 2, 0);
dfs(x1, y1-1, 3, 0);
dfs(x1, y1+1, 4, 0);
if(flag) printf("YES\n");
else printf("NO\n");
}
else printf("NO\n");
}
}
return 0;
}
其他: 连连看大家都应该玩过吧,如果写一个连连看游戏,就要用到搜索的算法,下面就是这题的解决方法。注意:记得剪枝,不然会超时,你玩连连看游戏不会出现按两个东西要等很久才能消吧,所以剪枝很重要!
代码:
Cpp代码
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
int n, m, t, k, z;
int x1, y1, x2, y2;
int a[1005][1005];
bool map[1005][1005], flag;
void init() //初始化函数
{
flag = false;
memset(map, false, sizeof(map));
}
void dfs(int x, int y, int z, int k) //坐标(x,y)
{
if(flag) return;
if(x<=0 || y<=0 || x>n || y>m) return; //若超界,返回
if(k>=3) return; //若转弯数已经超过3次,返回
if(x == x2 && y == y2)
{
flag = true;
return;
}
if(k == 2) //超强剪枝:若已经转两次弯,则目标坐标必须要在前进方向的前面,否则直接返回
{
if( !(z==1 && x>x2 && y==y2 || z==2 && x<x2 && y==y2
|| z==3 && y>y2 && x==x2 || z==4 && y<y2 && x==x2) )
return;
}
if(a[x][y] != 0) return; //如果该点不是0,则不能走,返回
if(map[x][y]) return; //如果该点已经走过,返回
map[x][y] = true; //标记该点为走过
if(z == 1) //上
{
dfs(x-1, y, 1, k);
dfs(x+1, y, 2, k+1);
dfs(x, y-1, 3, k+1);
dfs(x, y+1, 4, k+1);
}
else if(z == 2) //下
{
dfs(x-1, y, 1, k+1);
dfs(x+1, y, 2, k);
dfs(x, y-1, 3, k+1);
dfs(x, y+1, 4, k+1);
}
else if(z == 3) //左
{
dfs(x-1, y, 1, k+1);
dfs(x+1, y, 2, k+1);
dfs(x, y-1, 3, k);
dfs(x, y+1, 4, k+1);
}
else if(z == 4) //右
{
dfs(x-1, y, 1, k+1);
dfs(x+1, y, 2, k+1);
dfs(x, y-1, 3, k+1);
dfs(x, y+1, 4, k);
}
map[x][y] = false; //若深搜不成功,标记该点为未走过
}
int main()
{
int i, j;
while(scanf("%d %d", &n, &m))
{
if(!n && !m) break;
for(i = 1; i <= n; i++)
for(j = 1 ; j <= m; j++)
scanf("%d", &a[i][j]);
scanf("%d", &t);
for(i = 0; i < t; i++)
{
init();
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if(x1 == x2 && y1 == y2 && a[x1][y1] != 0) //若两点坐标是相同的,输出NO
printf("NO\n");
else if(a[x1][y1] == a[x2][y2] && a[x1][y1] != 0) //若两点的值相同且不为0
{
dfs(x1-1, y1, 1, 0);
dfs(x1+1, y1, 2, 0);
dfs(x1, y1-1, 3, 0);
dfs(x1, y1+1, 4, 0);
if(flag) printf("YES\n");
else printf("NO\n");
}
else printf("NO\n");
}
}
return 0;
}
其他的解释:这个题目 拿到手很明显 是广搜但是这个广搜 就的仔细点要考率拐弯搬来广搜到一个点 就是最近距离了但是这里 我们要的不是最近距离 而是 拐的最少 这时候我们就要标记一下到达这个点曾经拐了多少次弯于是ac了 可是回头一看 不对 我开 标记v的时候 用的是bool 那么就是说步数 就没记录 然后竟然ac了天哪 有没有搞错啊 速度还可以 比 我用 int char 快多了 这就说明 memset 多么费时了最后仔细看看看debug 终于大概明白为什么bool 也能ac了 真搞笑了 原来用memset 将 bool 类型置位成-1他会默认 bool 成了1111111(十进制255)这就置成负了的效果 然后将错就错 初始都不用了 但是大家最好别用这个方法 因为除了可以复制为255以为 其他的就不行了
6947742008-07-09 19:39:55Accepted1175281MS2924K2041 BC++seamild
#include<iostream>
#include<queue>
using namespace std;
int h[1005][1005];bool v[1005][1005];
int n,m,w,win;
struct node{int x,y,flag,step;};
queue <node> que;
node temp,tmp;
int mov[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int run(int a,int b,int c,int d)
{
int i,x,y;
memset(v,-1,sizeof(v));
while(!que.empty())que.pop();
for(i=0;i<4;i++){
temp.x=a;
temp.y=b;
temp.flag=i;
temp.step=0;
que.push(temp);}
while(!que.empty())
{
tmp=que.front();
que.pop(); for(i=0;i<4;i++)
{
x=tmp.x+mov[i][0];
y=tmp.y+mov[i][1];
if(x==c&&y==d)
{
if(i!=tmp.flag&&tmp.step>=2)continue;
win=1;return 0;
}
if(h[x][y]==0&&x<=n&&x>=1&&y<=m&&y>=1)
{
if(i==tmp.flag&&(v[x][y]==-1||v[x][y]>=tmp.step))
{
temp.x=x;
temp.y=y;
temp.step=tmp.step;
temp.flag=i;
v[x][y]=temp.step;
que.push(temp);
}
else if(tmp.step<2&&(v[x][y]==-1||v[x][y]>=tmp.step+1))
{
temp.x=x;
temp.y=y;
temp.step=tmp.step+1;
temp.flag=i;
v[x][y]=temp.step;
que.push(temp);
}
}
}
}
return 0;
}
int main()
{
int i,j,a,b,c,d;
while(scanf("%d%d",&n,&m)&&n)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&h[i][j]);
scanf("%d",&w);
while(w--)
{
win=0;
scanf("%d%d%d%d",&a,&b,&c,&d);
if(h[a][b]!=h[c][d]||h[a][b]==0||(a==c&&b==d)){printf("NO\n");continue;}
run(a,b,c,d);
if(win==1)printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
6947742008-07-09 19:39:55Accepted1175281MS2924K2041 BC++seamild
#include<iostream>
#include<queue>
using namespace std;
int h[1005][1005];bool v[1005][1005];
int n,m,w,win;
struct node{int x,y,flag,step;};
queue <node> que;
node temp,tmp;
int mov[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int run(int a,int b,int c,int d)
{
int i,x,y;
memset(v,-1,sizeof(v));
while(!que.empty())que.pop();
for(i=0;i<4;i++){
temp.x=a;
temp.y=b;
temp.flag=i;
temp.step=0;
que.push(temp);}
while(!que.empty())
{
tmp=que.front();
que.pop(); for(i=0;i<4;i++)
{
x=tmp.x+mov[i][0];
y=tmp.y+mov[i][1];
if(x==c&&y==d)
{
if(i!=tmp.flag&&tmp.step>=2)continue;
win=1;return 0;
}
if(h[x][y]==0&&x<=n&&x>=1&&y<=m&&y>=1)
{
if(i==tmp.flag&&(v[x][y]==-1||v[x][y]>=tmp.step))
{
temp.x=x;
temp.y=y;
temp.step=tmp.step;
temp.flag=i;
v[x][y]=temp.step;
que.push(temp);
}
else if(tmp.step<2&&(v[x][y]==-1||v[x][y]>=tmp.step+1))
{
temp.x=x;
temp.y=y;
temp.step=tmp.step+1;
temp.flag=i;
v[x][y]=temp.step;
que.push(temp);
}
}
}
}
return 0;
}
int main()
{
int i,j,a,b,c,d;
while(scanf("%d%d",&n,&m)&&n)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&h[i][j]);
scanf("%d",&w);
while(w--)
{
win=0;
scanf("%d%d%d%d",&a,&b,&c,&d);
if(h[a][b]!=h[c][d]||h[a][b]==0||(a==c&&b==d)){printf("NO\n");continue;}
run(a,b,c,d);
if(win==1)printf("YES\n");
else printf("NO\n");
}
}
return 0;
}