hdu 1045 Fire Net(搜索)
2012-04-22 10:08
405 查看
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3036 Accepted Submission(s): 1751
[align=left]Problem Description[/align]
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
View Code
/* dfs依次搜索每个点 */ #include<iostream> #define N 5 using namespace std; int map ,ans,m,n; bool visited ,flag; void dfs(int s) { if(s==n*n) { if(ans<m) ans=m; return ; } int i,j,k; i=s/n;j=s%n; if(!visited[i][j]) { flag=false; if(map[i][j]) flag=true; for(k=i-1;k>=0&&!flag;k--) { if(map[k][j]==1) break; if(visited[k][j]==true) { flag=true; break; } } for(k=j-1;k>=0&&!flag;k--) { if(map[i][k]==1) break; if(visited[i][k]==true) { flag=true; break; } } if(!flag) { m++; visited[i][j]=true; dfs(s+1); m--; visited[i][j]=false; dfs(s+1); } else { dfs(s+1); } } } int main() { int i,j; char t; while(cin>>n&&n) { ans=m=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>t; map[i][j]=(t=='X'); visited[i][j]=false; } } dfs(0); cout<<ans<<endl; } return 0; }
相关文章推荐
- HDU1045 Fire Net 暴力搜索DFS
- hdu 1045 Fire Net(回溯搜索)
- hdu 1045 || zoj 1002 Fire Net(搜索:DFS+水题)
- HDU 1045 Fire Net(搜索/贪心/二分图最大匹配)
- hdu-1045-Fire Net(搜索)
- hdu 1045 Fire Net (与 上一题 zoj 1654 如出一辙)
- 【HDU 1045】Fire Net(DFS,check函数)
- HDU 1045 Fire Net(行列匹配变形+缩点建图)
- HDU 1045 Fire Net
- hdu 1045 Fire Net
- HDU 1045(Fire Net)题解
- HDU 1045 Fire Net 最大二分图匹配
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
- HDU-1045-fire net
- A - Fire Net - hdu 1045(二分图匹配)
- zoj 1002 && hdu 1045 Fire Net
- hdu1045——Fire Net(二分图+行列匹配)
- HDU 1045 Fire Net (二分图的最大匹配)
- hdu 1045 Fire Net (最爱 DFS)
- HDU - 1045 - Fire Net (DFS)