hdu 1175 连连看 搜索—dfs

来源:互联网 发布:java常用服务器有哪些 编辑:程序博客网 时间:2024/04/28 04:55

题目:

A - 连连看

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,表示询问第x1y1列的棋子与第x2y2列的棋子能不能消去。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<cstdio>#include<string>#include<cstring>#include<cmath>#include<algorithm>#include<cctype>#include <fstream>#include <limits>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <cassert>using namespace std;int m,n;int fang[4][2]= {-1,0,0,1,1,0,0,-1};bool v[1010][1010];int t[1010][1010];struct la{    int x,y,ci, f;} a,b;bool dfs(int first_x,int first_y,int end_x,int end_y){    a.x=first_x,a.y=first_y,a.ci=-1;    stack<la>st;    //v[a.x][a.y]=1;    a.f=5;    st.push(a);    while(!st.empty())    {        a=st.top();        if(v[a.x][a.y])        {            st.pop();            v[a.x][a.y]=0;            continue;        }        v[a.x][a.y]=1;        //cout<<a.x<<a.y<<a.f<<a.ci<<endl;        for(int i=0; i<4; i++)        {            b.x=a.x+fang[i][0];            b.y=a.y+fang[i][1];            b.f=i;            b.ci=a.ci+(a.f==b.f?0:1);            if(b.ci<=2)                if(b.x>0&&b.x<=m&&b.y>0&&b.y<=n)                {                    if(b.x==end_x&&b.y==end_y)                        return 1;                    if(t[b.x][b.y]==0)                        if(!v[b.x][b.y])                        {                            //v[b.x][b.y]=1;                            st.push(b);                        }                }        }    }    return 0;}int main(){    while(~scanf("%d %d",&m,&n))    {        int ci,fx,fy,ex,ey;        if(m==0&&n==0)            break;        for(int i=1; i<=m; i++)            for(int j=1; j<=n; j++)                scanf("%d",&t[i][j]);        scanf("%d",&ci);        while(ci--)        {            memset(v,0,sizeof(v));            scanf("%d%d%d%d",&fx,&fy,&ex,&ey);            if(t[fx][fy]==t[ex][ey]&&dfs(fx,fy,ex,ey)&&t[fx][fy]>0)                printf("YES\n");            else printf("NO\n");        }    }    return 0;}

0 0
原创粉丝点击