HDU - 1175 - BFS

"Lianliankan" I believe many people have played. I never played it does not matter, here I tell you about the rules of the game: In a chessboard, put a lot of pieces. If a two identical pieces can be connected together (this line can not pass through the other pieces), and the number of times of no more than two turning line, then the two pieces can be eliminated by a line on the board. I am sorry, because I had not played Lianliankan, consulted students, the connection can not go around from the outside, but in fact this is wrong. Now lead to disaster, it is only the wrong, and can not bypass connections from the periphery.
Players click on the mouse has two pieces, trying to eliminate them, and then the game's background to determine the two squares can not be eliminated. Now your task is to write the daemon.

Input

Multiple sets of input data. The first line of each data set has two positive integers n, m (0 <n < = 1000,0 <m <1000), respectively, the number of rows and columns of the board. In the next n lines, each line has a non-negative integer m chessboard checkered distribution described. 0 indicates that this position is not a pawn, a positive integer representing the type of piece. The next line is a positive integer q (0 <q <50) , represents the q-th interrogation below. Q in the next row, each row has four positive integers x1, y1, x2, y2, x1 represents interrogation of row and first column pieces y1 x2 y2 row piece can eliminate columns. n = 0, m = 0, the end of the input.
Note: Has no relationship between the inquiry, are directed at the current state!

Output

Each set of input data corresponding to one line of output. If we can eliminate the output "YES", then the output can not be "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

Thinking: bfs turn number statistics + <to 2; vis steering problems with the array is determined;

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int maze[1010][1010];
bool vis[1010][1010];
int sx,sy,ex,ey;
bool flag;
int n,m,q;
int XX[]={1,-1,0,0};
int YY[]={0,0,1,-1};
void dfs(int x,int y,int dic,int turns){
    if(turns>2||flag) return;//转弯次数大于2或者已经找到就终止 
    if(turns==2&&(x-ex)!=0&&(y-ey)!=0) return;//剪枝:判断两次转弯后是否与目标在同一直线上 
    if(x==ex&&y==ey&&turns<=2){//搜索终点 
        flag=1;
        return;
    }
    for(int i=0;i<4;++i){//搜索四个方向 
        int xx=x+XX[i];
        int yy=y+YY[i];
        if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]) continue;//边界情况 
        if(maze[xx][yy]==0||(xx==ex&&yy==ey)){
            vis[xx][yy]=1;
            if(dic==-1||dic==i)//如果在起点或者同向的情况turns不变及不转向,并将当前方向记为i 
                dfs(xx,yy,i,turns);
            else
                dfs(xx,yy,i,turns+1);//否则turns+1 
            vis[xx][yy]=0;
        }
    }
    return;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0)
            break;
        memset(maze,0,sizeof(maze));
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
                scanf("%d",&maze[i][j]);
        scanf("%d",&q);
        for(int i=0;i<q;++i){
            scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
            memset(vis,0,sizeof(vis));//用vis数组判断转向问题 
            flag=0;//初始化 
            if(maze[sx][sy]==maze[ex][ey]&&maze[sx][sy]) //显然不能实现的情况 
                dfs(sx,sy,-1,0);//将初始方向设为-1 
            if(flag) 	printf("YES\n");
            else	 printf("NO\n");
        }
    }
    return 0;
}

 

Published 150 original articles · won praise 73 · views 6567

Guess you like

Origin blog.csdn.net/queque_heiya/article/details/104353651