题目大意:有一个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. 用PHP的socket实现客户端到服务端的通信

    服务端 <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); //本地IP $address = 'loca ...

  2. 详解centos用户&组权限&添加删除用户

    1.Linux用户操作系统 Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种: 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还有组账户所谓组账户就是用户账户的集合,ce ...

  3. Enumeration 接口

    Enumeration是遍历集合元素的一种方法. Enumeration中只有两个方法: 1.hasMoreElements()  测试此枚举是否包含更多的元素. 2.nextElement()  如 ...

  4. SharePoint 沙盒解决方案 VS 场解决方案

    博客地址 http://blog.csdn.net/foxdave 最近看书正好看到了关于沙盒解决方案的介绍,便整理记录一下. 虽然沙盒解决方案已经在最新的SharePoint开发中被否决弃用了(被A ...

  5. 虚拟机centos配置ip

    涉及到三个配置文件,分别是: /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/resolv.conf /et ...

  6. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

  7. JVM值内存垃圾回收监控之jstat

    如何判断JVM垃圾回收是否正常?一般的top指令基本上满足不了这样的需求,因为top主要监控的是总体的系统资源,很难定位到java应用程序. Jstat是JDK自带的一个轻量级小工具.全称“Java ...

  8. java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

    java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

  9. apache http client vs urlconnection

    Google has deprecated HttpClient Choose an HTTP Client Most network-connected Android apps use HTTP ...

  10. 修改主机名Ubuntu

    主机名存放在/etc/hostname 修改保存即可