题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡。子弹不能穿透墙壁。问最多可以放置几个碉堡,保证它们不会相互误伤。

解法:从左上的顶点开始遍历,如果这个点不是墙,做深度优先搜索,算出这种情况下的最多碉堡数。每做一次DFS,更新一次最大值。

参考代码:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std; char city[6][6];
int i,j,n,ans,maxi,visited[6][6];
void DSF();
int find(int x,int y); int main(){
while(cin>>n&&n){
memset(city,'X',sizeof(city)); //this is included in <string.h>
memset(visited,0,sizeof(visited));
ans=maxi=0;
getchar(); //this is included in <cstdio.h>
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
cin>>city[i][j];
getchar();
}
DSF();
cout<<ans<<endl;
} return 0;
} void DSF(){
int r,c;
if(ans<maxi) ans=maxi;
for(r=1;r<=n;r++){ //intially it is a loop to sweep all the blocks in the city
for(c=1;c<=n;c++){
if(!visited[r][c]&&city[r][c]!='X'){
if(find(r,c)){ // check if can put a castle in this block
visited[r][c]=1; //put a castle and the number of castle increase by one
maxi++;
DSF(); //continue search, similar to pushing this block into stack
visited[r][c]=0; //return and number of castle minus one
maxi--;
}
}
}
}
}
int find(int x,int y){
int i;
for(i=x-1;i>0;i++){ //check if there is castle in front, from the nearest to furthest
if(visited[i][y]==1) return 0;
if(city[i][y]=='X') break; //if there has a wall, no problem, continue to next direction
}
for(i=y-1;i>0;i++){ //left
if(visited[x][i]==1) return 0;
if(city[x][i]=='X') break;
}
for(i=x+1;i<=n;i++){ //back
if(visited[i][y]==1) return 0;
if(city[i][y]=='X') break;
}
for(i=y+1;i<=n;i++){ //right
if(visited[x][i]==1) return 0;
if(city[x][i]=='X') break;
}
return 1;
}

ZOJ 1002 Fire Net的更多相关文章

  1. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  2. [ZOJ 1002] Fire Net (简单地图搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...

  3. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  4. zoj 1002 Fire Net 碉堡的最大数量【DFS】

    题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...

  5. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

  6. [ZJU 1002] Fire Net

    ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we ha ...

  7. ZOJ 1002:Fire Net(DFS+回溯)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  8. [ACM_图论] Fire Net (ZOJ 1002 带障碍棋盘布炮,互不攻击最大数量)

    Suppose that we have a square city with straight streets.  A map of a city is a square board with n ...

  9. ZOJ(ZJU) 1002 Fire Net(深搜)

    Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...

随机推荐

  1. Fragment在xml中但作用不是显示view

    2013-12-17 有时候会发现在xml文件中有使用fragment,但是却不是为了显示View,代码如下: <FrameLayout xmlns:android="http://s ...

  2. [开发笔记]-多线程异步操作如何访问HttpContext?

    如何获取文件绝对路径? 在定时器回调或者Cache的移除通知中,有时确实需要访问文件,然而对于开发人员来说, 他们并不知道网站会被部署在哪个目录下,因此不可能写出绝对路径, 他们只知道相对于网站根目录 ...

  3. [开发笔记]-获取歌曲ID3信息

    ID3介绍: ID3,一般是位于一个mp3文件的开头或末尾的若干字节内,附加了关于该mp3的歌手,标题,专辑名称,年代,风格等信息,该信息就被称为ID3信息,ID3信息分为两个版本,v1和v2版. 获 ...

  4. POJ 3691 AC自动机上的dp

    题目大意: 给定一些不合理的DNA序列,再给一段较长的dna序列,问最少修改几次可以使序列中不存在任何不合理序列,不能找到修改方法输出-1 这里你修改某一个点的DNA可能会影响后面,我们不能单纯的找匹 ...

  5. [vijos P1595] 学校网络

    有生以来做的第二道IOI题目居然也是96'的,又是一道比我还老的题目. 纯属复习或者说再学一遍Tarjan算法,本题的主要算法就是Tarjan+缩点,对于两个子问题的答案,根据解题:强连通缩点为拓扑图 ...

  6. android Xutils dbutils 注解

    xUtils DbUtils 关于实体类注解 汇总 RockyZhang 发布于 1年前,共有 0 条评论 先来官方demo DbUtils db = DbUtils.create(this);    ...

  7. 使用plsql连接别人的oracle(转)

    文章来源:http://www.linuxidc.com/Linux/2013-04/82738.htm oracle服务有时候我们觉得太大,所以我们只需要在本机上装一个oracle客户端和plsql ...

  8. 微软.NET Framework 4.5.2 RTM正式版

    今天,微软.NET开发团队发布.NET Framework 4.5.2 RTM正式版.新版框架继续高度兼容现有的.NET Framework 4.4.5.4.5.1等版本,该版本框架与旧版的.NET ...

  9. JSON解析和XML解析

    一. XML:用到一个开源解析类,GDataXMLNode(将其加入项目中),添加libxml2.dylib框架 经常用到的方法: 1.- (id)initWithXMLString:(NSStrin ...

  10. passivedns 安装指南

    install passivedns on ubuntu Passive DNS对安全研究非常重要,因为它可以在前期帮助我们构建出目标的基础设施结构,并且可以得到以下三方面的答案:该域名曾经绑定过哪些 ...