题目描述:Fire Net
 
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.  Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

输入

  1. The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.

输出

  1. For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.

样例输入

  1. 4
  2. .X..
  3. ....
  4. XX..
  5. ....
  6. 2
  7. XX
  8. .X
  9. 3
  10. .X.
  11. X.X
  12. .X.
  13. 3
  14. ...
  15. .XX
  16. .XX
  17. 4
  18. ....
  19. ....
  20. ....
  21. ....
  22. 0

样例输出

  1. 5
  2. 1
  3. 5
  4. 2
  5. 4
 

这道题看着麻烦,实际就是讲了棋盘放位置的策略。总体和n皇后问题很类似。

  1. // Fire Net.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. //解题思路:
  5. //1.类似n皇后问题,上下左右不能有重复,只检测上方和左方即可,下面的没有放到,不需要检查。
  6. //2.走斜角坐标,x = pos/n,y = pos % n
  7. //3.每个坐标存在能放和不能放两种情况
  8.  
  9. #include "stdafx.h"
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define M 10
  15. int n,ans;
  16. char map[M][M];
  17.  
  18. int check(int x, int y)
  19. {
  20. if (map[x][y] == 'X') return ;
  21.  
  22. //左方
  23. for (int col = y - ; col >= ; col--)
  24. {
  25. if (map[x][col] == 'X')
  26. break;
  27. if (map[x][col] == '#')
  28. return ;
  29. }
  30.  
  31. //上方
  32. for (int row = x - ; row >= ; row--)
  33. {
  34. if (map[row][y] == 'X')
  35. break;
  36. if (map[row][y] == '#')
  37. return ;
  38. }
  39.  
  40. return ;
  41.  
  42. }
  43.  
  44. void DFS(int pos, int sum)
  45. {
  46. int x = pos / n, y = pos % n;//x,y 的位置
  47. if (pos == n * n)
  48. {
  49. ans = ans > sum? ans:sum;
  50. return;
  51. }
  52. if (check(x, y))
  53. {
  54. map[x][y] = '#';
  55. DFS(pos + , sum + );
  56. map[x][y] = '.';
  57. }
  58.  
  59. DFS(pos + , sum);
  60. }
  61.  
  62. int main()
  63. {
  64. while (~scanf("%d", &n) && n)
  65. {
  66. memset(map, '\0', sizeof(map));
  67.  
  68. for (int i = ; i < n; i++)
  69. scanf("%s", &map[i]);
  70.  
  71. ans = ;
  72. DFS(,);
  73. printf("%d\n", ans);
  74.  
  75. }
  76. return ;
  77. }

ACM-Fire Net的更多相关文章

  1. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. 【Acm】算法之美—Fire Net

    题目概述:Fire Net Suppose  that we have a square city with straight streets. A map of a city is a square ...

  3. [acm 1002] 浙大 Fire Net

    已转战浙大 题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 浙大acm 1002 #include <iostre ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. HDU1045 Fire Net(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  6. ACM -二分图题目小结

    暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063  过 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  8. zoj 3820 Building Fire Stations 树的中心

    Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  9. HDU-1045 Fire Net

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Me ...

  10. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

随机推荐

  1. SqlSession为什么可以提交事务

    本应在开始读MyBatis源码时首先应该了解下MyBatis的SqlSession的四大对象:Executor.StatemenHandler.ParameterHandler.ResultHandl ...

  2. 【剑指Offer面试编程题】题目1390:矩形覆盖--九度OJ

    题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入 ...

  3. IDEA 服务器热部署详解(On Update action/On frame deactivation)

    https://blog.csdn.net/w15321271041/article/details/80597962 场景:一般服务器(比如tomcat,jboss等)启动以后,我们还需要进一步修改 ...

  4. windows下用libevent 开发一个echo服务

    #include <stdio.h> #include <string.h> #include <errno.h> #include <iostream> ...

  5. python学习笔记(三)---高级特性

    一.切片 取无数多个list元素 不用一个个取得笨方法就用切片 对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python提供了切片(Slice)操作符,能大大简化这种操作. 对应上面的问题, ...

  6. 「JSOI2007」建筑抢修

    传送门 Luogu 解题思路 显然先把所有楼按照报废时间递增排序. 然后考虑 \(1\cdots i-1\) 都能修完, \(i\) 修不完的情况. 显然我们在这 \(i\) 个里面至多只能修 \(i ...

  7. IOS pin约束问题 存在间隙

    今天在为自己的view添加约束 对比以前添加的约束时,发现有有两层淡红色线框一条实线和一条虚线,而以前一个demo中添加的则只有一个蓝色实线框. 今天添加的约束如图1所示: 图1 而以前添加约束如图2 ...

  8. List循环添加数据覆盖问题

    问题:java开发时,当我们使用List.add();循环添加数据,有时会出现前面添加的数据会被后面覆盖的现象.这是怎么回事尼? 会覆盖数据的代码 package com.boot.test; imp ...

  9. 2.13 阶段实战 使用layui重构选课系统

    一.说在前面   昨天  学习表单校验插件validate,并使用ajax 自定义校验规则   今天 使用layui重构选课系统 二.题目要求 1.项目需求: 本项目所开发的学生选课系统完成学校对学生 ...

  10. POJ 3268:Silver Cow Party 求单点的来回最短路径

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15989   Accepted: 7303 ...