Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1045
Theme: Place o in a place other than X, and all o cannot view each other without a partition (horizontal or serial number). Ask how many O can be placed at most.
Solutions:
The problem scale is relatively small (4*4) and can be solved by DFS.
For a point, either put or not put.
The condition must be that the upper, lower, left, and left directions are scanned to the boundary without first hitting X.
You can only mark the point to be placed, but not the point to be left. In this way, return in time when Dep> N * n.
Note that each DFS only needs to consider one point in order, instead of simultaneously running DFS on all non-X points of the entire board; otherwise, it will burst.
The reason is that only one vertex is associated at a time, and other vertices are not associated. If DFS is equal to a large number of repeated computations.
#include "cstdio"#include "string"#include "cstring"#include "iostream"using namespace std;int n,vis[5][5],ans;char map[5][5];struct status{ int x,y; char type; status(int x,int y,char type):x(x),y(y),type(type) {} status() {}}P[20];bool judge(int X,int Y){ if(map[X][Y]==‘X‘) return false; for(int i=X-1; i>=1&&map[i][Y]!=‘X‘; i--) if(vis[i][Y]) return false; for(int i=X+1; i<=n&&map[i][Y]!=‘X‘; i++) if(vis[i][Y]) return false; for(int i=Y-1; i>=1&&map[X][i]!=‘X‘; i--) if(vis[X][i]) return false; for(int i=Y+1; i<=n&&map[X][i]!=‘X‘; i++) if(vis[X][i]) return false; return true;}void dfs(int p,int s){ if(p>n*n) {ans=max(ans,s);return;} dfs(p+1,s); vis[P[p].x][P[p].y]=true; if(judge(P[p].x,P[p].y)) dfs(p+1,s+1); vis[P[p].x][P[p].y]=false;}int main(){ //freopen("in.txt","r",stdin); ios::sync_with_stdio(false); string tt; while(cin>>n&&n) { memset(vis,0,sizeof(vis)); int cnt=0;ans=0; for(int i=1;i<=n;i++) { cin>>tt; for(int j=0;j<tt.size();j++) { P[++cnt]=status(i,j+1,tt[j]); map[i][j+1]=tt[j]; } } dfs(1,0); cout<<ans<<endl; }}
| 11909497 |
2014-10-19 11:59:35 |
Accepted |
1045 |
0 ms |
292 K |
1335 B |
C ++ |
Physcal |
HDU 1045 (DFS search)