博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ(ZJU) 1002 Fire Net(深搜)
阅读量:6618 次
发布时间:2019-06-25

本文共 3826 字,大约阅读时间需要 12 分钟。

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.

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.

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

Sample input:

4

.X..
….
XX..
….
2
XX
.X
3
.X.
X.X
.X.
3
.XX
.XX
4
….
….
….
….
0

Sample output:

5

1
5
2
4

题目大意:

假设我们有一个正方形的城市,其街道都是直的。在方形的地图上,有n行和n列,每个代表一条街道或一堵墙。每个碉堡有4个射击孔,分别正对东西南北方向。在每个射击孔配置一架高射机枪。
我们假设,子弹是如此强大,它的射程可以任意远,并能摧毁它射中的碉堡。另外,墙也是很坚固的,可以挡住子弹!
其目标是,在该城市布置尽可能多的碉堡,而碉堡之间又不好互相摧毁。合理布置碉堡的原则是:没有两个碉堡在一个水平方向或垂直方向,除非它们之间有墙相隔!
在本题中,假设城市很小,(n最大为4) ,而且子弹不能贯穿墙壁。

输入n,代表n行n列。

n为0是输入结束。
‘.’代表空地,’X’代表墙壁。

因为题目规模很小,直接采用深度优先算法就可以解决。

import java.util.Scanner;public class Main{
static int n; static int time; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ n =sc.nextInt(); if(n==0){ return ; } char[][] map = new char[n][n]; for(int i=0;i
time){
//是否有更大的值 time=count; return ; } }else{ //将单元数转换为xy坐标 x =k/n; y =k%n; //如果本单元格可以放置碉堡 if(map[x][y]=='.'&&CanPut(map,x,y)){ map[x][y]='O';//放置下一个碉堡 //令count加一,并递归到下一个单元格 dp(map,k+1,count+1); //递归完毕,恢复该单元格 map[x][y]='.'; } //本单元格不能放置碉堡 dp(map,k+1,count); } } /** * 判断行x和列y处能不能配置碉堡 * @param map * @param x * @param y * @return */ private static boolean CanPut(char[][] map,int x, int y) { for(int i=x-1;i>=0;i--){
//判断y列上的合法性 if(map[i][y]=='O') return false; if(map[i][y]=='X') break; } for(int i=y-1;i>=0;i--){
//判断x行上的合法性 if(map[x][i]=='O') return false; if(map[x][i]=='X') break; } return true; }}
你可能感兴趣的文章
会计的思考(42):会计如何转变为公司的内部财务顾问
查看>>
利用钥匙串,在应用里保存用户密码的方法
查看>>
vuex状态管理详细使用方法
查看>>
不要等有了足够的钱才选择去创业!!!
查看>>
手把手教你画嘴巴,以后再也不怕画嘴巴了
查看>>
selenium - webdriver - 截图方法get_screenshot_as_file()
查看>>
io.lettuce.core.RedisCommandTimeoutException: Command timed out
查看>>
种子填充算法描述及C++代码实现
查看>>
Kali渗透测试——快速查找Metasploit的模块
查看>>
如何生成项目的chm文档
查看>>
java封装httpClient工具(支持http和https,包含get和post请求)
查看>>
Rocket - diplomacy - LazyModuleImpLike
查看>>
Exchange Server 2016管理系列课件25.管理安全通讯组
查看>>
计算机科学,大一学生怎样来爱你(文&PPT)
查看>>
PHP 开发社区微信服务号实战图解
查看>>
Exchange Server 2013 系列八:邮箱服务器角色DAG实战
查看>>
php使用curl下载指定大小的文件
查看>>
VS2013创建Node.js C++ Addons的过程
查看>>
amaze ui中的icon button
查看>>
tcp 三次握手
查看>>