B - 连连看

B - 连连看
Time Limit:10000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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
// File Name: lianliankanadfasdf.cpp
// Author: rudolf
// Created Time: 2013年03月19日 星期二 15时52分08秒

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<queue>
using namespace std;
int n,m,sx,sy,ex,ey;
int mapp[1005][1005];
int mark[1005][1005];
int dist[4][2]={0,1,1,0,0,-1,-1,0};
struct node
{
	int x,y,d,num;
};


bool judge(int x,int y)
{
	if(x>0&&x<=n&&y>0&&y<=m&&(x==ex&&y==ey||!mapp[x][y]))
		return true;
	else
		return false;
}


bool dfs()
{
	queue<node>Q;
	node cur,next;
	cur.x=sx;
	cur.y=sy;
	cur.d=-1;
	cur.num=0;
	Q.push(cur);
	while(!Q.empty())
	{
		cur=Q.front();
		Q.pop();
		if(cur.x==ex&&cur.y==ey)
			return true;
		for(int i=0;i<4;i++)
		{
			next=cur;
			next.x+=dist[i][0];
			next.y+=dist[i][1];
			if(next.d==-1)
			{
				next.d=i;
				next.num=0;
			}
			else
				if(next.d!=i)
				{
					next.num++;
					next.d=i;
				}
				if(next.num>2)
					continue;
				if(judge(next.x,next.y))
				{
					if(mark[next.x][next.y]>next.num)
					{	
						Q.push(next);
						mark[next.x][next.y]=next.num;
					//	Q.push(next);
					}
				}
		}
	}
	return false;
}

int main()
{

	int i,j;
	int k;
	while(cin>>n>>m)
	{
		if(n==0&&m==0)
			break;
		memset(mapp,0,sizeof(mapp));
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				scanf("%d",&mapp[i][j]);
		cin>>k;
		while(k--)
		{
			cin>>sx>>sy>>ex>>ey;
			if(mapp[sx][sy]!=mapp[ex][ey]||!mapp[sx][sy]||(sx==ex&&sy==ey))
			{
				cout<<"NO"<<endl;
				continue;
			}
			for(i=1;i<=n;i++)
				for(j=1;j<=m;j++)
					mark[i][j]=1000000;
		//	st.x=sx;
		//	st.y=sy;
		//	st.d=-1;
		//	st.num=0;
			if(dfs())
				cout<<"YES"<<endl;
			else
				cout<<"NO"<<endl;
		}
	}
return 0;
}








// File Name: lianliankan.cpp
// Author: rudolf
// Created Time: 2013年03月17日 星期日 20时55分21秒

#include<iostream>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
const int MAX=1005;
using namespace std;
int mapp[MAX][MAX];
int mark[MAX][MAX];
int n,m,k,sx,sy,ex,ey;
int dist[4][2]={0,1,1,0,0,-1,-1,0};
struct node
{
    int x,y,d,num;
};

bool judge(int x,int y)
{
    if(x>0&&x<=n&&y>0&&y<=m&&(!mapp[x][y]||x==ex&&y==ey))
            return true;
    else
        return false;
}

bool  bfs()
{
    queue<node> Q;
    node cur,next,p,s;
    cur.x=sx;
    cur.y=sy;
    cur.num=0;
    cur.d=-1;
//    Q.push(st);
//    mark[sx][sy]=0;
//    int k=0;
    Q.push(cur);
    while(!Q.empty())
    {
        cur=Q.front();
        Q.pop();
        if(cur.x==ex&&cur.y==ey)
            return true;
        for(int i=0;i<4;i++)
        {    
            next=cur;
            next.x+=dist[i][0];
            next.y+=dist[i][1];
            if(next.d==-1)
            {
                next.d=i;
                next.num=0;
            }
            else
                if(next.d!=i)
                {
                    next.num++;
                    next.d=i;
                }
                if(next.num>2)
                    continue;
            if(judge(next.x,next.y))
            {
                if(mark[next.x][next.y]>next.num)
                {
                    Q.push(next);
                    mark[next.x][next.y]=next.num;
                //Q.push(next);
                }

            }    
        }
    }
    return false;
}
    
int main()
{

    int i,j;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        memset(mapp,0,sizeof(mapp));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&mapp[i][j]);
        cin>>k;
        while(k--)
        {
            cin>>sx>>sy>>ex>>ey;
            if(mapp[sx][sy]!=mapp[ex][ey]||!mapp[sx][sy]||(sx==ex&&sy==ey))
            {
                cout<<"NO"<<endl;
                continue;
            }
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                    mark[i][j]=1000000;
        //    st.x=sx;
        //    st.y=sy;
        //    st.d=-1;
        //    st.num=0;
            if(bfs())
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
return 0;
}
posted @ 2013-04-12 17:09  bo_jwolf  阅读(254)  评论(0)    收藏  举报