您的位置:首页 > 其它

hdu 1175 连连看

2010-04-09 12:01 288 查看
Description

“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。

玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。

Input

输入数据有多组。每组数据的第一行有两个正整数n,m(0<n≤100,0<m≤100),分别表示棋盘的行数与列数。

在接下来的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




Source

hdu 1175





#include<iostream>

using namespace std;

int b[1020][1020];

int n, m, q; 

bool standard(int c1, int c2, int r)
 
{ 
    int t;
    if(c1>c2)
    { 
        t=c1;
		c1=c2;
		c2=t;
    } 
    for(int i=c1+1;i<=c2-1;i++) 
    { 
        if(b[r][i]!=0) 
            return false; 
    } 
    return true; 
} 

bool vertical(int r1,int r2, int c) 

{ 
    int t;
    if(r1>r2) 
    { 
        t=r1;r1=r2;r2=t; 
    } 
    for(int i=r1+1;i<=r2-1;i++) 
    { 
        if(b[i][c]!=0) 
            return false; 
    } 
    return true; 
} 

bool check(int r1,int c1,int r2,int c2) 
{
    if(b[r1][c1]==0||b[r2][c2]==0||b[r1][c1]!=b[r2][c2]||r1==r2&&c1==c2) 
        return false;    
  
    for(int i=1;i<=n;i++) //walk 一|__
    { 
        if((i!=c1&&b[r1][i]!=0)||(i!=c2&&b[r2][i]!=0)) 
            continue; 
        if(standard(i,c1,r1)&&vertical(r1,r2,i)&&standard(i,c2,r2)) 
            return true;
    } 

    for(int i=1;i<=m;i++) //walk |_|
    { 
        if((i!=r1&&b[i][c1]!=0)||(i!=r2&&b[i][c2]!=0)) 
            continue; 
        if(vertical(i,r1,c1)&&standard(c1,c2,i)&&vertical(i,r2,c2)) 
            return true; 
    } 
    return false; 
} 

int main() 
{ 
    
    int x1, y1, x2, y2, i, j; 
    while( cin>>n>>m&&(n||m) )
    {
        for(i=1;i<=n;++i)
        {
            for(j=1;j<=m;++j) 
                scanf("%d ", &b[i][j]);
        }
        scanf("%d", &q);
        while( q-- )
        { 
            cin>>x1>>y1>>x2>>y2; 
            if( check(x1,y1,x2,y2) ) 
                cout<<"YES/n";
            else 
                cout<<"NO/n"; 
        } 
    }
    return 0; 
}




#include <iostream>
#include <queue>
using namespace std;

const int N = 1001;
bool flag;
int n,m,sx,sy,ex,ey;
int hash

,map

;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node{
    int x,y,turn,d;
}start;
queue<node> q;

inline bool in(const node &p){
    if(p.x<0 || p.y<0 || p.x>=n || p.y>=m)
        return false;
    return true;
}
void bfs(){
    node now,t;
    while(!q.empty()){
        now=q.front(),q.pop();
        if(now.x==ex && now.y==ey && now.turn<=2){
            flag=true;
            return;
        }
        for(int i=0;i<4;i++){
            t.x=now.x+dir[i][0],t.y=now.y+dir[i][1];
            if(now.d==i)
                t.turn=now.turn,t.d=now.d;
            else
                t.turn=now.turn+1,t.d=i;
            if(in(t) && (map[t.x][t.y]==0||t.x==ex&&t.y==ey) && hash[t.x][t.y]>=t.turn)
                hash[t.x][t.y]=t.turn,q.push(t);
        }
    }
} int main(){
    int i,j,t;
    while(scanf("%d %d",&n,&m),n||m){
        for(i=0;i<n;i++)
            for(j=0;j<m;j++) scanf("%d",&map[i][j]);
            scanf("%d",&t);
            while(t--){
                scanf("%d %d %d %d",&sx,&sy,&ex,&ey);
                sx--,sy--,ex--,ey--;
                if((map[sx][sy]!=map[ex][ey]) || map[sx][sy]==0 || map[ex][ey]==0 || (sx==ex&&sy==ey)){
                    puts("NO");
                    continue;
                }
                for(i=0;i<n;i++)
                    for(j=0;j<m;j++) hash[i][j]=11;
                    while(!q.empty()) q.pop();           
                    for(i=0;i<4;i++){
                        start.x=sx,start.y=sy,start.turn=0,start.d=i;
                        q.push(start);
                    }
                    flag=false,hash[sx][sy]=0;
                    bfs();
                    puts(flag ? "YES":"NO");
            }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: