【HDU-1045,Fire Net-纯暴力简单DFS】

原题链接:点击!

大致题意:白块表示可以放置炮台的位置——每个炮台可以攻击到上下左右的直线上的炮台(也就是说在它的上下左右直线上不可以再放置炮台,避免引起互相攻击),黑块表示隔离墙的位置——不可放置并且可以阻挡炮火;求对于一个最大4*4的格子来说,最大的放置炮台的个数是多少。

简单分析:

简单的dfs();由于本题地图很小,不用考虑太多,尽情暴力即可。先把DFS枝干画出来,然后枝叶就用函数来具体实现。

AC代码:(附注释)

 1 #include <iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<stdlib.h>
 5 #include<stdio.h>
 6 using namespace std;
 7 char mp[5][5];
 8 int n,ans;
 9 int dir[4][2]={ {0,1},{0,-1},{1,0},{-1,0}  };
10
11 int legal(int x,int y){//若点x,y可以放置炮台,返回1
12
13     for(int i=0;i<4;i++){
14         for(int k=0;k<=3;k++){  //四个方位顶多走3步
15             int xx,yy;
16             xx=x+dir[i][0]*k;
17             yy=y+dir[i][1]*k;
18             if(!(xx>=0&&yy>=0&&xx<=n-1&&yy<=n-1)){  //越界,break,搜下一个方位!
19                 break;
20             }
21             if(mp[xx][yy]==‘X‘){  //一个方位上存在墙体
22                 break;
23             }
24             if(mp[xx][yy]==‘D‘){//一个方位上存在炮台,返回0
25                 return 0;
26             }
27         }
28     }
29     return 1;
30
31 }
32 void dfs(int step)   //step表示已经完成的点数
33 {
34     ans=max(ans,step);
35     for(int i=0;i<n;i++){//每次搜索就是遍历一遍图,由于本题数据范围贼小,就不用考虑标记等
36         for(int j=0;j<n;j++){
37             if(mp[i][j]==‘.‘&&legal(i,j))//找到一处合法的‘.’
38             {
39                 mp[i][j]=‘D‘;//替换成D,简单清晰
40                 dfs(step+1);//dfs迭代
41                 mp[i][j]=‘.‘;//取消标记
42             }
43         }
44     }
45
46 }
47 int main()
48 {
49     while(scanf("%d",&n),n!=0){
50         for(int i=0;i<n;i++){//读图
51             scanf("%s",mp[i]);
52         }
53         ans=0;
54
55         dfs(0);  //开始搜索!
56         printf("%d\n",ans);
57     }
58
59     return 0;
60 }

原文地址:https://www.cnblogs.com/zhazhaacmer/p/8504496.html

时间: 2024-10-07 19:54:25

【HDU-1045,Fire Net-纯暴力简单DFS】的相关文章

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

hdu 1015 Safecracker (纯暴力)

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8183    Accepted Submission(s): 4143 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein

HDU 1045 Fire Net 贪心

Problem Description 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 wh

HDU 1045 Fire Net(dfs,跟8皇后问题很相似)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14670    Accepted Submission(s): 8861 Problem Description Suppose that we have a squar

HDU 1045 Fire Net 状压暴力

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8073    Accepted Submission(s): 4626 Problem Description Suppose that we have a squar

HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description 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, e

hdu 1045 Fire Net(最小覆盖点+构图(缩点))

http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description Suppose that we have a square city with straight streets. A map of a city

HDU 1045 Fire Net(DFS)

Fire Net Problem Description 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 t

hdu 1045 Fire Net DFS入门题

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7012    Accepted Submission(s): 3975 Problem Description Suppose that we have a square city with straight streets. A map of a city is a