题目大意:有一个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. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 素数 难度:0

    http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool u ...

  2. MySql插入记录时判断

    我们在开发数据库相关的逻辑过程中, 经常检查表中是否已经存在这样的一条记录, 如果存在则更新或者不做操作, 如果没有存在记录,则需要插入一条新的记录. 这样的逻辑固然可以通过两条sql语句完成. SE ...

  3. AFNetworking、MKNetworkKit和ASIHTTPRequest对比

    之前一直在使用ASIHTTPRequest作为网络库,但是由于其停止更新,iOS7上可能出现更多的问题,于是决定更换网络库. 目前比较流行的网络库主要有AFNetworking和MKNetworkKi ...

  4. 简单的两数之和再次乱入<< Add Two Numbers >>

    请看题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored ...

  5. hql抓取要注意的点

    fetchtype是lazy,那就用到了在通过缓存中的关联去取,用不到不取:lazy遇到joinfetch就失去意义,但是由于hql语句是自己编写的,可以控制加不加fetch 所以如果主力是hql语句 ...

  6. springmvc----struts2比较

    method=requestMethod.GETorPOST  vs  addInput+add 用抛异常处理密码验证和用户名重复与否验证 +js303 @validate   判断输入格式(jque ...

  7. iOS应用崩溃日志分析

    转自raywenderlich   作为一名应用开发者,你是否有过如下经历?   为确保你的应用正确无误,在将其提交到应用商店之前,你必定进行了大量的测试工作.它在你的设备上也运行得很好,但是,上了应 ...

  8. poj2392 多重背包

    //Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...

  9. a various of context

    ContextWrapper.getApplicationContext():Return the context of the single, global Application object o ...

  10. BZOJ 1600 建造栅栏

    O(N)分成1,2与3,4两部分搞一搞. #include<iostream> #include<cstdio> #include<cstring> #includ ...