• Fire Net

Time Limit:
2 Seconds      Memory Limit:
65536 KB


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

Source: Zhejiang University Local Contest 2001

初级搜索题,一般人都可以模拟出来。

可以想成简单的dfs加剪枝,而不是模拟所有情况在判断是否成立。

代码经过电脑处理后(其实:处理之前也是)感觉不好看



#include<cstdio>

#include<cstdlib>

#include<iostream>

using namespace std;

char c[6][6];

bool f[6][6];

int n,sum;

void solve(int x,int y,int ans)

{

 int i,j;

 bool ok=true;

 if(c[x][y]=='X') ok=false;

 for(i=x-1;i>=1;i--){

  if(c[i][y]=='X') break;

  if(f[i][y]) ok=false;

 }

 for(j=y-1;j>=1;j--){

  if(c[x][j]=='X') break;

  if(f[x][j]) ok=false;

   }

 if(ok) {

  f[x][y]=true;

  if(ans+1>sum) sum=ans+1;

  if(y+1<=n) solve(x,y+1,ans+1);

  else if(x+1<=n) solve(x+1,1,ans+1);

     f[x][y]=false;

 }

 if(y+1<=n) solve(x,y+1,ans); 

 else if(x+1<=n) solve(x+1,1,ans);

 

}

int main()

{

 int i,j;

 while(cin>>n){

     if(n==0) break;

     for(i=1;i<=n;i++)

   for(j=1;j<=n;j++)

    {cin>>c[i][j];f[i][j]=false;}

  sum=0; 

  solve(1,1,0);

  cout<<sum<<endl;

 }

 return 0;

}

 

zoj1002 Fire Net的更多相关文章

  1. ZOJ1002 —— 深度优先搜索

    ZOJ1002 —— Fire net Time Limit: 2000 ms Memory Limit: 65536 KB Suppose that we have a square city wi ...

  2. 关于SequeezeNet中的Fire Module

    在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...

  3. FZU 2150 Fire Game

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

  4. Fire

    Fire 分析: 首先,明确题意:b1,b2,--,bn 交换为b2,--,bn,b1,但这并不是意味着只能从b1开始交换,(这点从样例中可以看出),并且也不意味着交换的必须是连续的一串,可以是几个单 ...

  5. Android 轻量级输入校验库:Fire Eye

    Fire Eye是一款轻量级简单易用的Android校验库. FireEye 2.0 在 1.0 的基础上,全部重写了代码,并优化了架构,性能上和逻辑上都大大提升.只需要几行代码,即可验证用户输入,并 ...

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

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

  7. UVA 11624 Fire!(广度优先搜索)

    题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...

  8. Amazon的Fire Phone之于Android开发者

    在上周Amazon也耐不住加入了手机竞争行列之中,发布了自己的Fire Phone,于是Android家族又多了一位变种成员,Android系统的碎片化程度也进一步加剧.因为工作的关系,我有幸在上个月 ...

  9. Fire!(BFS)

    Fire! Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Descr ...

随机推荐

  1. NHibernate教程(7)--并发控制

    本节内容 什么是并发控制? 悲观并发控制(Pessimistic Concurrency) 乐观并发控制(Optimistic Concurrency) NHibernate支持乐观并发控制 实例分析 ...

  2. 初入PHP,(for循环~水仙花数)

    找出100-999之间的所有"水仙花数".所谓水仙花数是指一个三位 数,各位数字的立方和等于该数本身.(如153次方=1的3次方+5的3次方+3的3次方)并输出这些数字 想想153 ...

  3. 第二次项目冲刺(Beta阶段)5.24

    1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #63Web输出以文件名为标题 #63 ...

  4. 【C++】关于pow函数的用法

    在C++中,pow有多个重载函数: 在dev中,pow(int,int)可以执行,但是在别的地方是不可以被编译的:会提示 :error C2668: “pow”: 对重载函数的调用不明确 可以看见,是 ...

  5. 201521123062《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: public boolean contains(Object ...

  6. 201521123112《Java程序设计》第3周学习总结

    1.本周学习总结 使用工具:百度脑图 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int ...

  7. java程序设计 彩票购买抽奖程序 团队博客

    一.项目介绍 题目要求 功能要求: 模拟福利彩票36选7,实现彩票的抽奖与中奖通知功能. 1.允许注册用户,用户信息包括用户id,用户名,密码,账户金额,电话号码等属性. 2.允许注册用户购买彩票:手 ...

  8. php中获取当前系统时间、时间戳

    今天写下otime($time, $now)为将时间格式转为时间戳,$time为必填.清楚了这个,想了解更多,请继续往下看. 3. date($format)用法比如:echo date(‘Y-m-d ...

  9. Java :构造器中的显式参数和this隐式参数

    1.构造器 写一个Java类,首先要先从构造器开始,构造器与类同名,在构造类的对象时会先从构造器开始. 构造器总是伴随着new操作符的执行而被调用. 构造器主要是用来初始化类的实例域. 构造器的特点: ...

  10. Java:实现对象的比较 comparable接口和comparator接口

    在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...