Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1045
Title Description: Put the car in the matrix, the car can be four-sided attack, the Matrix has a wall, can prevent attack, give the location of the wall, the output can put up how many cars;
Topic points:dfs& binary graph max match (Hungarian algorithm) (here is Dfs)
The subject at the beginning of the time into a greedy vortex, written out of the thought is DFS, but actually talk, and greedy here is wrong; (greedy algorithm is narrow in application, most of the cases is wrong)
Then found the DFS and greedy, the greedy algorithm is fixed every time the method of placing a car, in fact, does not enumerate all the situation; DFS algorithm or build tree, for a point, when can not be placed here on Dfs, his next point, if you can put here, Not necessarily put here (this is the greedy algorithm and the biggest difference between the place, talk algorithm is if you can put that on the back of the prevention situation, so did not enumerate all the situation), you can choose to put, or not put, if put, change the state of the point, and then Dfs next point, and restore the site at the end of Dfs, the state will be changed back; If you choose not to put the DFS next point directly (the DFS order is from top to bottom from left to right, if run to the bottom right corner of the end);
The code is as follows:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 Chara[6][6];5 intmaxn=0, N;6 BOOLCheckintXintY//Check whether the point can be put;7 {8 for(inti=x-1; i>=0; i--)9 {Ten if(a[i][y]=='X') One Break; A if(a[i][y]=='*') - return false; - } the for(inti=y-1; i>=0; i--) - { - if(a[x][i]=='X') - Break; + if(a[x][i]=='*') - return false; + } A return true; at } - voidDfsintXintYintcount) - { - if(x>=N) - { - if(maxn<count) inmaxn=count; - return ; to } + if(y>=N) - { theDFS (x+1,0, count); * } $ ElsePanax Notoginseng { - if(a[x][y]=='.'&&check (x, y)) the { +a[x][y]='*';//modify the scene; ADFS (x,y+1, count+1); thea[x][y]='.';//restore the scene; + } -DFS (x,y+1, count);//This place is the situation when the point is not put, (in time can be put also to consider the situation:); $ } $ } - intMain () - { the while(cin>>n&&N) - {Wuyi for(intI=0; i<n;i++){ the GetChar (); -scanf"%s", A[i]); Wu } - /*printf ("\n%d\n", n); About for (int i=0;i<n;i++) { $ for (int j=0;j<n;j++) { - printf ("%c", A[i][j]); - } - printf ("\ n"); A }*/ +maxn=0; theDfs0,0,0); -cout<<maxn<<Endl; $ } the the return 0; the}
View Code
hdoj--1045<dfs& binary Best match > (this is the DFS solution)