DFS ZOJ 1002/HDOJ 1045 Fire Net

题目传送门

 1 /*
 2     题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台
 3     搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1)左下角走,x = cnt / n; y = cnt % n;    更新坐标,
 4     直到所有点走完为止,因为从左边走到右边,只要判断当前点左上方是否满足条件就可以了
 5     注意:当前点不能放炮台的情况也要考虑
 6     g[x][y] == ‘o‘;    的错误半天才检查出来:)
 7 */
 8 #include <cstdio>
 9 #include <iostream>
10 #include <cstring>
11 #include <string>
12 #include <algorithm>
13 #include <map>
14 #include <cmath>
15 using namespace std;
16
17 const int MAXN = 1e4 + 10;
18 const int INF = 0x3f3f3f3f;
19 char g[5][5];
20 int ans;
21 int n;
22
23 bool ok(int x, int y)
24 {
25     for (int i=y-1; i>=0; --i)
26     {
27         if (g[x][i] == ‘o‘)    return false;
28         else if (g[x][i] == ‘X‘)    break;
29     }
30     for (int i=x-1; i>=0; --i)
31     {
32         if (g[i][y] == ‘o‘)    return false;
33         else if (g[i][y] == ‘X‘)    break;
34     }
35
36     return true;
37 }
38
39 void DFS(int cnt, int tot)
40 {
41     if (cnt == n * n)
42     {
43         if (ans < tot)    ans = tot;
44         return ;
45     }
46
47     else
48     {
49         int x = cnt / n;
50         int y = cnt % n;
51
52         if (g[x][y] == ‘.‘ && ok (x, y) == true)
53         {
54             g[x][y] = ‘o‘;
55             DFS (cnt+1, tot+1);
56             g[x][y] = ‘.‘;
57         }
58
59         DFS (cnt+1, tot);
60     }
61 }
62
63 int main(void)        //ZOJ 1002/HDOJ 1045 Fire Net
64 {
65     //freopen ("ZOJ_1002.in", "r", stdin);
66
67     while (scanf ("%d", &n) == 1 && n)
68     {
69         for (int i=0; i<n; ++i)
70             scanf ("%s", &g[i]);
71
72         ans = -1;    DFS (0, 0);
73
74         printf ("%d\n", ans);
75     }
76
77     return 0;
78 }
79
80 /*
81 5
82 1
83 5
84 2
85 4
86 */
时间: 2024-08-10 13:42:30

DFS ZOJ 1002/HDOJ 1045 Fire Net的相关文章

hdoj 1045 Fire Net 【DFS】

题意:如果两个点要放在同一行或者同一列,那么两个点中间要有一个墙,否则的话只能放一个点,最后问你最多能放几个点. 看了一个星期.. 这道题的解法我还是第一次见,就是逐个逐个的来放置每个点,然后每经过一个点都判断一次,详情看代码 代码: #include <stdio.h> #include <string.h> int ans, n; char map[10][10]; int judge(int lin, int row) { int i; for(i = lin-1; i &g

HDOJ 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): 6917    Accepted Submission(s): 3911 Problem Description Suppose that we have a squa

ZOJ 1002 Fire Net

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 thro

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 (最大独立集)

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

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

二分图最大匹配 hdoj 1045

题目:hdoj1045 题意:给出一个图,其中有 . 和 X 两种,. 为通路,X表示墙,在其中放炸弹,然后炸弹不能穿过墙,问你最多在图中可以放多少个炸弹? 分析:这道题目是在上海邀请赛的题目的数据简化版,数据水了,所以有很多方法,这里讲二分图最大匹配,题目难点在于建图 想到用暴力过,但是事实证明我想多了.然后又想到多重二分匹配,后来发现没有办法表示图中的行列中墙的阻隔,后来看了别人的建图,瞬间觉得高大上. 建图,首先把每一行中的可以放一个炸弹的一块区域标记为同一个数字,数字不重复,然后列做相同

[dfs] zoj 3736 Pocket Cube

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3736 Pocket Cube Time Limit: 2 Seconds      Memory Limit: 65536 KB Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For

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