DFS series POJ (self-described)

Source: Internet
Author: User

C-sum It up POJ1564
Test instructions
Give you a n, and then give you a bunch of numbers the numbers in every list appear in nonincreasing order, and there could be repetitions., so you find some numbers in this logarithm, if they're and sum= =n, the output is based on the number of formats in the sample.
Ideas:
That is Dfs Bai, deep search a wave, when the sum==n output, here is the elimination of repetitive pruning. Why is it?

#include <iostream>#include <cstdio>#include <stdlib.h>#include <vector>#include <string.h>#include <algorithm>using namespace STD;typedef Long LongLL;#define INF 0x3f3f3f3f#define N 1010intC[i];intA[n];intN,m;intFlagvoidDfsintNumintSumintV) {if(sum==n) {flag=1;printf("%d", d[0]); for(intI=1; i<num;i++) {printf("+%d", D[i]); }printf("\ n"); }if(sum>n)return; for(inti=v;i<m;i++) {D[num]=a[i]; DFS (num+1, sum+a[i],i+1); while(a[i]==a[i+1]&&i<m-1)//sentence weighti++; }}intMain () { while(~scanf("%d%d", &n,&m) &&n&&m) { for(intI=0; i<m;i++)scanf("%d", &a[i]);printf("Sums of%d:\n", n); flag=0; Dfs0,0,0);if(!flag)printf("none\n"); }return 0;}

D-Maze problem poj3984

Test instructions: Chinese ...
Ideas:
This topic recommended bfs+ handwriting queue (which is very important) + path output, how to explain the path output?
(Some non-formal definition of the term, is the chicken itself defined)
You can simulate it yourself, in a queue, we use the current element's pre to record the node above that element in the queue. At first we knew it was the upper-left corner, so we initialized his pre value to 1, and finally to the end, recursive output.
The handwriting queue here not only runs fast but also saves time. Can feel a bit.
So why do we use STL's queue? You know, tell me! Thank you! (Seniors say that using STL is simple and convenient)

#include <iostream>#include <cstdio>#include <stdlib.h>#include <vector>#include <string.h>#include <algorithm>Using namespace Std;typedef long long LL;#define INF 0x3f3f3f3f#define N 1010struct asd{int x,y;intPre//Used to record the path};ASDq[25*10];intHead,tail;intbmap[6][6];bool vis[6][6];intdx[4]={0,0,1,-1};intdy[4]={1,-1,0,0};void Shuchu (int x){if(Q[x]. pre==-1)    {printf("(%d, %d) \ n",Q[x].x,Q[x].y); }Else{Shuchu (Q[x]. Pre);printf("(%d, %d) \ n",Q[x].x,Q[x].y); }}void BFs () {head=0; tail=1;Q[head].x=0;Q[head].y=0;Q[head]. pre=-1; memset (Vis,0, sizeof (VIS)); vis[0][0]=1; while(Head<tail) {intBx,by; bx=Q[head].x; by=Q[head].y;if(bx==4&&by==4) {Shuchu (Q[head]. Pre);return; } for(intI=0;i<4; i++) {intAa=bx+dx[i];intBb=by+dy[i];if(aa<0|| bb<0|| Aa>4|| Bb>4|| vis[aa][bb]| | BMAP[AA][BB])Continue; vis[aa][bb]=1;Q[tail]. Pre=head;Q[tail].x=AA;Q[tail].y=BB;        tail++;    } head++; }}intMain () { for(intI=0;i<5; i++) { for(intj=0;j<5; j + +) scanf ("%d", &bmap[i][j]); } BFS ();printf("(4, 4) \ n");return 0;}

E–sticks poj1011

Test instructions: give you a bunch of sticks that you broke from a bunch of sticks, and now you forget the length of the stick from that pile, let you write a program to find the shortest length
Ideas:
The idea of reasoning is very difficult to tell.
DFS is also very confusing, pruning is also.
It is not so much the so-called strong pruning, rather than the lack of comprehensive thinking.
In fact pruning is the way to run as little as possible on the same conditions that are not necessary.
Well, let's talk about the experience of the problem.
We can find that because all parts became at the most units long, and there is at the most sticks, then the original length of the longest is 320, then we have reason to try to enumerate the length, and then to determine whether it is the smallest long Degree.

We can sort them, thus greatly reducing the number of recursion; (reverse)
Why is the reverse, because the longer the stick, the less flexible, so we have the basis for the use of reverse

Then there is a pruning: this satisfies the length of L, must satisfy the sum of the Bonzi sum%l==0 this condition.

And we can also think of, in order to find the condition of the repetition of the pruning, that there is no condition at this moment, there is no need to go to the same stick to DFS.

#include <iostream>#include <cstdio>#include <stdlib.h>#include <vector>#include <string.h>#include <algorithm>using namespace STD;typedef Long LongLL;#define INF 0x3f3f3f3f#define NintLen[n];intNintFlag,k;BOOLVis[n];voidDfsintVintSumintNUM) {if(flag)return;if(sum==k) {if(num==n) {flag=1; }ElseDfs0,0, num);return; }if(sum==0)    {intv=0; while(Vis[v]) v++; vis[v]=1; DFS (v+1, len[v],num+1); vis[v]=0;return; } for(intI=v; i<n; i++) {if(Sum+len[i]<=k&&!vis[i]) {if(len[i]==len[i-1]&&!vis[i-1])Continue; vis[i]=1; DFS (i+1, sum+len[i],num+1); vis[i]=0; }    }}BOOLcmpintAintb) {returnA>b;}intMain () {intSumintTmax while(~scanf("%d", &n) &&n) {sum=0; tmax=-1; for(intI=0; i<n; i++) {scanf("%d", &len[i]);if(Tmax<len[i]) tmax=len[i];        Sum+=len[i]; } sort (len,len+n,cmp); for(K=tmax; k<=sum; k++) {if(sum%k==0) {flag=0;memset(Vis,0,sizeof(VIS)); Dfs0,0,0);if(flag) {printf("%d\n", k); Break; }            }        }    }return 0;}

DFS series POJ (self-described)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.