题目大意:有一个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. Android 4.3正式发布:四大新功能一览

    在旧金山举行的新品发布会上,Google正式发布了Android 4.3,代号仍为“Jelly Bean”.此次更新并没有太大改变,只是紧跟4.1.4.2步伐, 新增了低功耗蓝牙.多用户登录等一系列功 ...

  2. 佳佳的魔杖 (vijos 1283)

    题目大意: 一根树枝有N段,每一段有一个分数,可以选取一些不完全包含(可以相交)的区间,每次选取可以得到区间里所有数之和的分数. 求最大得分. 解题过程: 1.很明显的dp,默认选取区间的顺序是从左往 ...

  3. 一模 (5) day2

    第一题: 题目大意:使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少? n<=2*10^9 解题过程: 1.以前看到过这题了,一个数x的位数=(int)lg(x)+1  换一下底就是 ...

  4. js 获取div 图片高度

    使用jquery获取网页中图片的高度其实很简单,有两种常用的方法都可以打到我们的目的 $("img").whith();(返回纯数字) $("img").css ...

  5. [开发笔记]-Visual Studio 2012中为创建的类添加注释的模板

    为类文件添加注释,可以让我们在写代码时能够方便的查看这个类文件是为了实现哪些功能而写的. 一:修改类文件模板 找到类模版的位置:C:\Program Files (x86)\Microsoft Vis ...

  6. Right-BICEP 测试四则运算二程序

    测试方法: Right-BICEP 测试计划: 1.Right-结果是否正确? 2.B-是否所有的边界条件都是正确的? 3.是否有乘除法? 4.是否有括号? 5.是否有输出方式? 6.是否可以选择出题 ...

  7. JS constructor

    1.每个对象都有一个constructor,都指向创建该对象的方法. 2.function对象的prototype的constructor也是指向该方法.如果对prototype进行重写,constr ...

  8. Be a person

    做人不能太实诚 尤其是干我们这行的 多久时间能做完 你自己心里要有个估算 然后把时间再往后延 别他妈给自己找罪受

  9. CWNP宣布中国首位CWNE获得者——朱志立(Kevin Zhu)

    CWNP宣布中国首位CWNE获得者——朱志立(Kevin Zhu)   Kevin Zhu获得了全球CWNE认证无线网络专家的第134号,CWNE被公认为IT行业最难获取的10大认证之一.     [ ...

  10. 重拾java系列一java基础(2)

    1.分支流程控制 if(布尔表达式/分支条件){  //语句块} if(布尔表达式/分支条件){  //语句块1}else{  //语句块2} if(条件1){  //语句块1}else if(条件2 ...