Fire Net

#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<cstdio>
#include<stack>
#include<queue>
#include<set>
using namespace std;
char mapp[5][5];
int best,n;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool canPut(int x,int y){
    //第x行,第y列
    for(int i=y-1;i>=0;i--){
        if(mapp[x][i]==‘1‘) return false;
        if(mapp[x][i]==‘X‘) break;
    }
    for(int i=x-1;i>=0;i--){
        if(mapp[i][y]==‘1‘) return false;
        if(mapp[i][y]==‘X‘) break;
    }
    return true;
}
void  dfs(int k,int current){
  //cout<<k<<"  "<<current<<endl;
    if(k==n*n){

        if(best<current){
            best=current;
           // cout<<best<<"  best"<<endl;
            return ;
        }
    }
    else{
    int x=k/n;
    int y=k%n;
     // if(k==2) cout<<x<<"  "<<y<<"  2"<<endl;
    if(mapp[x][y]==‘.‘&&canPut(x,y)){
        mapp[x][y]=‘1‘;
       // cout<<x<<"  "<<y<<"放置跑  "<<endl;
        dfs(k+1,current+1);
        mapp[x][y]=‘.‘;
    }
    dfs(k+1,current);
    }

}
int main(){
    while(cin>>n){
        memset(mapp,0,sizeof(mapp));
        if(n==0) break;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>mapp[i][j];
            }
        }

        best=0;
        dfs(0,0);
        cout<<best<<endl;

    /*深度优先搜索:每次遍历全局,注意回溯,规定放了炮弹的为1,不放的为0
    找到最大的数目。
    判断能不能放
    */
    }

   return 0;
}
时间: 2024-11-06 10:11:25

Fire Net的相关文章

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

ZOJ 3820 Building Fire Stations

Building Fire Stations Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 382064-bit integer IO format: %lld      Java class name: Main Special Judge Marjar University is a beautiful and peaceful place. There a

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

Fire! 又是图 bfs

Joe works in a maze.  Unfortunately, portions of the maze havecaught on  re, and the owner of the maze neglected to create a  reescape plan. Help Joe escape the maze.Given Joe's location in the maze and which squares of the mazeare on  re, you must d

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

FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this boar

Fire Maze(广度优先搜索)

Fire Maze Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 53(19 users) Total Accepted: 26(17 users) Rating: Special Judge: No Description After escaping from Figo's chase, Severus falls into a N * M maze designed by Figo. At first, Severus is

(简单) UVA 11624 Fire! ,BFS。

Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the maze are o

(简单) FZU 2150 Fire Game ,Floyd。

Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstl

uva11624 - Fire! 两次bfs

题目链接 Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the

ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】

题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题意:给出n个点,n-1条边的一棵树.然后要在两个点上建立两个消防站.让全部点的到消防站最大距离的点的这个距离最小. 分析:首先先求这个树的直径.然后在树的直径的中点处把树分成两棵树.然后在把两棵树分别取中点的最大值就是ans值. 这个题目数据有点水了感觉... AC代码: #include <cstdi