hdu1175 Lianliankan dfs + pruning

Lianliankan

Problem Description
"Lianliankan" I believe many people have played. 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
#include <the iostream> 
#include <cstdio> 
#include <CString> 
the using namespace STD; 
 
int Maze [1010] [1010]; 
BOOL VIS [1010] [1010]; 
int SX, SY, EX, EY; 
BOOL In Flag; 
int n-, m, Q; 
int dicx [] = {. 1, -1,0,0}; 
int DICY [] = {0,0,1, -1}; 
 
void DFS (int X, Y int, int DIC, turns int) { 
    IF (turns> 2 || In Flag) return; // 2 or greater than the number of turns has been found to terminate 
    if (turns == 2 && (x -ex)!! = 0 && (y-ey) = 0) return ; // pruning: determining whether two target after turning on the same line 
    if (x == ex && y == ey && turns <= 2) { Search end // 
        in Flag =. 1; 
        return; 
    } 
    for (int I = 0; i <4; ++ i) { // search four directions 
        int dicx XX = X + [I]; 
        int DICY YY = Y + [I]; 
        IF (XX <XX. 1 ||> n-YY || <|| YY. 1>m || vis [xx] [yy]) continue; // boundary where
        IF (Maze [XX] [YY] == 0 || (XX YY == == && EX EY)) { 
            VIS [XX] [YY] =. 1; 
            IF (DIC == - == DIC. 1 || I) / / If not the case and at the beginning of or turns in the same direction without turning, and the current direction is denoted I 
                DFS (XX, YY, I, turns); 
            the else 
                DFS (XX, YY, I, turns +. 1); // otherwise it turns +. 1 
            VIS [XX] [YY] = 0; 
        } 
    } 
    return; 
} 
 
int main () { 
    the while (~ Scanf ( "% D% D", & n-, & m)) { 
        IF (n-== 0 && m == 0) 
            BREAK; 
        Memset (Maze, 0, the sizeof (Maze)); 
        
        for (int I =. 1; I <= n-; ++ I) 
        for (int I = 0; I <Q; I ++) { 
            Scanf ( "% D%% D D D%", & SX, & SY, EX &, &Oh); 
            for (int. 1 = J; J <= m;++j)
                scanf("%d",&maze[i][j]);
        
        Scanf ( "% D", & Q);
            Memset (VIS, 0, the sizeof (VIS)); 
            
            In Flag = 0; // initialize 
            IF (Maze [SX] [SY] == Maze [EX] [EY] && Maze [SX] [SY]) 
                DFS (SX, SY , -1, 0); // initial direction is -1 
                
            IF (In Flag) the printf ( "YES \ n-"); 
            the else the printf ( "NO \ n-"); 
        } 
    } 
    return 0; 
}

  

 

 

Guess you like

Origin www.cnblogs.com/lyj1/p/11516975.html