ACM学习之路__HDU 1045
Fire Net
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.
Input
Output
Sample Input
Sample Output
#include <iostream>
#include <cstdio> using namespace std; char str[][];
int dir[][]={{,},{,-},{,},{-,}};// up down left right 四个方向
int ans,n;
int judge(int r,int c)// 判断当前位置能否放炮台
{
int i;
bool flag=;//默认当前位置符合条件
for(i=;i<;i++)//对当前位置,跑完它的四个方位
{
int rr,cc;
rr=r;
cc=c;
while()
{
rr=rr+dir[i][];
cc=cc+dir[i][];
if(rr< || cc< || rr>n || cc>n)//当到达边界时跳出
break;
else if( str[rr][cc] == '@')
{
flag=;
break;
}//当前位置所在行,列已有炮台,不符合条件
else
{
if(str[cc][rr] == 'X')
break;
}//遇到碉堡,跳出
}//当前位置是否符合要求
}
return flag;
}
void dfs(int res)
{
for(int i= ; i<=n ; i++)
{
for(int j= ; j<=n ; j++)
{
if(str[i][j]=='.' && judge(i,j))
{
str[i][j]='@';//当前位置是空地,且可以放炮台
dfs(res+);//当前位置符合要求,进行下一层搜索
str[i][j]='.';//记得还原
}
}
}
if(res>ans)
ans=res;//当前最优解
return;
}
int main()
{
int i,j;
while(~scanf("%d%*c",&n) && n)
{
ans=;
for(i= ; i<=n ; i++)
{
for(j= ; j<=n ; j++)
cin>>str[i][j];
}
dfs();
cout<<ans<<endl;
}
return ;
}
ACM学习之路__HDU 1045的更多相关文章
- ACM学习之路___HDU 5723(kruskal + dfs)
Abandoned country Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...
- ACM学习之路___HDU 1385(带路径保存的 Floyd)
Description These are N cities in Spring country. Between each pair of cities there may be one trans ...
- ACM学习之路___HDU 2066 一个人的旅行
Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...
- ACM学习之路————一个大整数与一个小整数不得不说得的秘密
这个相对于两个大整数的运算来说,只能说是,low爆了. 只要利用好除法的性质,这类题便迎刃而解.O(∩_∩)O哈哈~ //大整数除一个int数 #include<iostream> #in ...
- ACM学习之路
2018-10-18 11:03:00 今天开始踏上实现梦想的道路,希望自己不要懈怠. 坚持做简单的事,坚持下来就会变得不简单.
- <2014 05 09> Lucida:我的算法学习之路
[转载] 我的算法学习之路 关于 严格来说,本文题目应该是我的数据结构和算法学习之路,但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法(例如算法导论指的实际上是数据结构和算法导论),所以 ...
- jQuery学习之路(1)-选择器
▓▓▓▓▓▓ 大致介绍 终于开始了我的jQuery学习之路!感觉不能再拖了,要边学习原生JavaScript边学习jQuery jQuery是什么? jQuery是一个快速.简洁的JavaScript ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
- RPC远程过程调用学习之路(一):用最原始代码还原PRC框架
RPC: Remote Procedure Call 远程过程调用,即业务的具体实现不是在自己系统中,需要从其他系统中进行调用实现,所以在系统间进行数据交互时经常使用. rpc的实现方式有很多,可以通 ...
随机推荐
- Ta-lib函数功能列表
import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...
- 虚拟机下安装VMware Tools
使用虚拟机安装第二操作系统,是我们经常使用到的一种方法,但是为了能够在虚拟机与本地操作系统间进行文件共享,我们可以借助一款非常实用的工具VMware Tools.下面我们就来说一下怎样安装VMware ...
- 线性代数-矩阵-【1】矩阵汇总 C和C++的实现
矩阵的知识点之多足以写成一本线性代数. 在C++中,我们把矩阵封装成类.. 程序清单: Matrix.h//未完待续 #ifndef _MATRIX_H #define _MATRIX_H #incl ...
- Spring整合Redis(spring-data-redis)
历经几天看了大量的博客资料,差不多算是搞定了,目前只是针对单个数据源,集群暂时没研究 maven依赖 <properties> <!-- redis 版本 --> <re ...
- POI设置excel某列值为文本格式
excel单元格格式默认为[常规],当某列限定必须从下拉框选择一个纯数字文本的时候,必须将单元格格式设置为[文本]. 否则即使输入的值和下拉框的一致,excel都认为输入的值是常规类型,而下拉框的值为 ...
- javascript学习笔记-4
document.getElementByTagName返回的是一个NodeList,这个NodeList和js数组很类似,都可以使用下标读取,如:array[0],但他们也有不同,不同在于不能对No ...
- Oracle:解锁scott用户及设置密码
关于Oracle 10g scott用户解锁的方法两则 解决方法一. 首先确认已经安装oracle 数据库和客户端 在客户端DOS下执行如下语句: 注意提示符号 c:\sqlplus /nolog s ...
- 通过业务系统的重构实践DDD
最近新接了一个业务系统——社区服务系统,为了快速熟悉和梳理老系统的业务逻辑和代码,同时对老系统代码做一些优化,于是打算花上一个月时间不间断地对老系统服务进行重构.同时,考虑到社区业务的复杂性,想起了之 ...
- CSS display和visibility的用法和区别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt408 大多数人很容易将CSS属性display和visibility混淆,它 ...
- CSS背景效果
前面的话 本文将详细介绍CSS背景效果 条纹背景 [双条纹背景] background:linear-gradient(#fb3 50%, #58a 50%); background-size: 10 ...