hdu 1175 Lianliankan DFS_ bytes written original title beating

Reprinted to: https: //www.cnblogs.com/LQBZ/p/4253962.html

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
Title requires relatively simple processing is lianliankan codes, but not around the outer
 
     

1 #include<iostream>
2 #include<cstring>
3 #define M 1010
4 using namespace std;
5 int dix[4]={0,1,0,-1};
6 int diy[4]={1,0,-1,0};//方向
7 int map[M][M];//建立地图
8 bool vid[M][M];//标记是否走过
9 bool flag;
10 int n,m,q,i,j,x1,x2,y1,y2;
11 int main()
12 {
13 void dfs(int x,int y,int turn,int direction);
14 while(cin>>n>>m,n||m)
15 {
16 memset(vid,0,sizeof(vid));
17 for(i=1;i<=n;i++)
18 for(j=1;j<=m;j++)
19 cin>>map[i][j];
20 cin>>q;
21 while(q--){
22 cin>>x1>>y1>>x2>>y2;
23 if(x1==x2&&y1==y2){
24 cout<<"NO"<<endl;
25 continue;
26} // the same time when the start and end points can not be eliminated
27 if (x1 <1 || x1 > n || y1 <1 || y1> m || x2 <1 || x2> n || y2 <1 Y2 ||> m)
28 {
29 COUT << "NO" << endl;
30 Continue;
31 is through} // I tested, this question examples exist outside the map
32 if (map [x1] [ y1] = = map [x2] [y2] && map [x1] [y1]) // when the digital beginning and end positions of the same and not equal to 0 to run
33 is {
34 is in Flag = 0;
35 for (I = 0; I <. 4; I ++) // here you important four directions from a processing point
36 DFS (X1, y1,0, I);
37 [IF (! in Flag)
38 is COUT << "NO" << endl;
39}
40 the else
41 is << COUT "NO" << endl;
42 is}
43 is}
44 is return 0;
45}
46 is void DFS (int X, Y int, int Turn, int direction)
47 {
48 IF (In Flag)
49 return;
50 IF (X >n || x <1 || y < 1 || y> m || vid [x] [y]) // When out of bounds or through the place returned
51 return;
52 if (turn == 2 && x = x2 && y = y2!!) // copied from others better pruning
53 is return;
54 is / ******************* ****************************
55 me explain this pruning, when you can not turn the two directions of rotation direction.
56 So when the turn 2 times when to special sentence, if the current position and the end is not in a
57 straight when he returns, because time is not in a position you do not turn not
58 possible to the finish, this question this particular sentence pruning is good, some people cut
59 and this method will be about the same, but because he had to determine the direction of the so-generation
60 yards on a bit too long drag on a bit of time.
*********************************************** 61/
62 iF (Turn> 2)
63 is return;
64 iF (X == X2 && Y == Y2 && Turn <= 2) {
65 COUT << "YES" << endl;
66 In Flag =. 1;
67 return;
68} // determine if it is end
69 if (map [x] [ y] = 0!) // here are the pit 11 5555
70 {
71 is IF (X == Y == && X1 Y1);
72 the else return;

74 VID [X] [Y] =. 1;
75 int DX, Dy, I;
76 for (I = 0; I <. 4; I ++)
77 {
78 DX = X + Dix [I];
79 Dy = Y + DIY [I];
80 iF (I == direction) /// when steering in the same direction on the same time
81 DFS (DX, Dy, turn, direction);
82 /// not the same when the else to add a steering
83 DFS (DX, Dy, Turn +. 1, I);
84}
85 VID [X] [Y] = 0; /// remember purge marker
86 return;
87}
88 / *
89 bit test data provide
90. 5. 5
91 is . 1 0 2 2 2
92 2 0. 1. 1. 1
93. 1. 1. 1. 1. 1
94. 1. 1. 1. 1. 1
95. 1. 1. 1. 1. 1
96. 5
97. 1. 1. 3. 1
98. 1. 1. 3 2
99. 1. 1 2. 3
100. 1. 1 2 . 4
101. 5. 1. 5. 1
102. 8. 8
103. 7 0 0 2. 3. 1. 5. 6
104. 4 0 0 0 0 0. 5. 6
105 0 0 5 0 4 8 7 0
106 0 0 0 0 0 0 0 0
107 2 0 8 0 0 0 0 7
108 6 5 3 0 2 4 0 9
109 7 0 0 0 0 0 0 7
110 9 7 9 8 6 6 1 5
111 5 5
112 1 1 8 7
113 1 2 5 1
114 3 6 5 3
115 3 6 8 4
116 5 8 7 8
117 */

 

 

 

Guess you like

Origin www.cnblogs.com/yichengming/p/11129368.html