"DFS" HDU 1175 repeatedly see the topic Link: hdu 1175 repeatedly see the topic
See again, ask whether success?
Test instructions is very simple, we usually play the game rules, seemingly DFS and BFS can do, the author did a Dfs (good think), timed out several times, because the DFS (int d) and the end of the D overload contradiction, so still have to be careful.
Talk about the idea.
- Artifact Pruning: if (t==2&&x!=c&&y!=d) return; This pruning magic pen rejuvenation, 9000+ms optimization to 100+ms AH: If you turn 2 times, but the target is not the same row or the same column is not satisfied with the current position
- There is one more thing to think about: If the G "x" "Y" you want to access is not the end point and there is a number fill, then exit, not G "x" "Y"!=0 on the exit, the former is a necessary and sufficient condition, otherwise can never find the end!
- The direction of the record is also very critical: the author uses the i=0,1,2,3 represents the next, upper, right and left four directions, judging the direction to be traversed and before the direction is consistent, inconsistent on the turn!
The key to "in addition" search is pruning! Optimization!
Reference Code
/*author:hacker_vision*/#include <bits/stdc++.h>#define CLR (k,v) memset (k,v,sizeof (k) )using namespace STD;Const int_max =1e3+Ten;intN,m,a,b,c,d;intg[_max][_max],dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};BOOLOk,vis[_max][_max];voidDfsintXintYintPintT) {//horizontal x, ordinate y, previous direction p, number of transitions T if(x>n| | y>m| | x<1|| y<1|| T>2|| Okreturn;if(x==c&&y==d&&t<=2) {ok=true;return;}if(t==2&&x!=c&&y!=d)return;//Perfect pruning, if you turn 2 times, but the target is not in the same row or column as the current positionvis[x][y]=1; for(inti =0; I <4&&!ok; + + i) {//i=0,1,2,3 means lower, upper, right, and left four directions int_x=x+dir[i][0];int_y=y+dir[i][1];if(_x==c&&_y==d);Else if(g[_x][_y]| | Vis[_x][_y])Continue;//vis[_x][_y]=1; if(i==p| | p==-1) Dfs (_X,_Y,I,T);ElseDFS (_x,_y,i,t+1); vis[_x][_y]=0; }}intMain () {//Freopen ("Input.txt", "R", stdin); while(scanf("%d%d", &n,&m) = =2&&n| | m) { for(inti =1; I <= N; + + i) for(intj =1; J <= M; + + j)scanf("%d", &g[i][j]);intTscanf("%d", &t); while(t--) {scanf("%d%d%d%d", &a,&b,&c,&d);if(g[a][b]!=g[c][d]| |! G[a][b]) {puts("NO");Continue;}//pruning to make an optimizationCLR (Vis,0); ok=false; DFS (a,b,-1,0);//0,1,2,3 all represent One direction, the initial direction takes-1 the best puts(OK?)"YES":"NO"); } }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"DFS" HDU 1175 watch