2016 November last week
This week, I reviewed the only algorithm that I know about today--depth-first search algorithm (DFS).
The handling of various details is still extremely unskilled, and it is not easy to draw conclusions based on test instructions to determine whether to restore the mark.
I have to say, distance from a quasi-acmer I have a considerable gap, whether this road can not be learned.
Tomorrow is the freshman race, GG is coming ... But no matter how successful I am, I'm not holding on to anything,
To prove yourself or something, and to fill in every volunteer thing. The results slipped into two (and not very regretful).
Or that sentence, he is strong enough to let him strong, take his own road good.
Rookie a think, the big guy laughed, but if as rookie still don't think, that estimate is always a rookie.
Well, at the beginning of this week, I finally took out the mat laptop's "Aha!" Algorithm starts to look.
Book is a good book, very suitable for my pupil level, I now finally understand how quicksort is a principle ...
After reading the bucket, bubbling, fast three sort, I jump directly to the depth first search algorithm This chapter, feel reading or the blind JB learn a little better,
Its templates are also clearer and more beautiful than I have written before.
(about the new template http://www.cnblogs.com/ray-coding-in-rays/p/6127150.html)
After a couple of sample code, I decided to find hdoj the only simple DFS problem I wrote.
Title: http://acm.hdu.edu.cn/showproblem.php?pid=1241
Test instructions is roughly the area of how many @ in a rectangular area, * is a delimited symbol
, write it again with the new template, and then I find myself forgetting everything ...
Anyway, after a struggle, I finally got it out.
For the first time, my AC code is like this:
1#include <stdio.h>2 #defineMAXN 1003 4 CharMAP[MAXN][MAXN];5 6 voidDfsint,int);7 8 intMainvoid)9 {Ten intLength,width; One inti,j; A intcount; - while(~SCANF ("%d%d",&length,&width)) - { theCount=0; - if(length==0) Break; - for(i=0; i<length;i++) - for(j=0; j<=width;j++) + { -scanf"%c",&map[i][j]); + } A for(i=0; i<length;i++) at for(j=0; j<width;j++) - { - if(map[i][j]=='@') - { - DFS (I,J); -count++; in } - } toprintf"%d\n", count); + } - the return 0; * } $ Panax Notoginseng voidDfsintXinty) - { the if(map[x][y]=='@') + { Amap[x][y]='*'; theDFS (x+1, y); +DFS (x,y+1); -DFS (X-1, y); $DFS (x,y-1); $DFS (x+1, y+1); -DFS (x+1, Y1); -DFS (X-1, Y1); theDFS (X-1, y+1); - }Wuyi Else the return; -}
It feels too rough ... , then after the framework is refactored:
1#include <stdio.h>2 3 Charmap[ -][ -];4 intwidth, lenth;5 //An array is defined here to represent the movement of the retrieval point6 intmove[8][2] = {7{1,0},{0,1},{ -1,0},{0,-1 },8{1,1},{1,-1},{ -1,-1},{ -1,1 }9 };Ten voidDFS (intXinty) One { A intK, TX, Ty; - - for(k =0; K <8; k++)//8 ways to move the { -tx = x + move[k][0]; -ty = y + move[k][1]; - //Check whether it is legal + if(tx<0|| ty<0|| Tx >= Lenth | | Ty >= width)Continue; - if(Map[tx][ty] = ='*')Continue; + if(Map[tx][ty] = ='@') A { at //mark it as non-target State -Map[tx][ty] ='*'; - DFS (TX, ty); - //Note that there is no need to restore the markup to implement backtracking - } - } in return; - } to + intMainvoid) - { the intI, J, Count; * while(~SCANF ("%d%d", &lenth, &width)) $ {Panax NotoginsengCount =0; - if(Lenth = =0) Break; the for(i =0; i<lenth; i++) + //here is a small improvement, read the string row by line without worrying about the end of the carriage return problem Ascanf"%s", Map[i]); the for(i =0; i<lenth; i++) + for(j =0; J < width; J + +) - { $ if(Map[i][j] = ='@') $ { - DFS (i, j); -count++; the } - }Wuyiprintf"%d\n", count); the } - return 0; Wu}
Well, it feels much better, that's all.
"Pseudo-weekly summary (yes, I did so little work a week)" HDOJ-1241 oil deposits first AC rough version vs code frame refactoring