hdu 1045

#include <stdio.h>
#include <string.h>

char map[5][5];
const int MIN=0;
int n,ans;

bool Check(int x,int y){//x代表横坐标,y代表纵坐标
    for(int i=x-1;i>=0;i--){
        if(map[i][y]==‘0‘)
            return false;
        if(map[i][y]==‘X‘)
            break;
    }
    for(int j=y-1;j>=0;j--){
        if(map[x][j]==‘0‘)
            return false;
        if(map[x][j]==‘X‘)
            break;
    }
    return true;
}
void DFS(int dex,int num){

    if(dex==n*n){
        ans=ans>num?ans:num;
        return ;
    }
    int p=dex/n,q=dex%n;
    if(map[p][q]==‘.‘&&Check(p,q)){
        map[p][q]=‘0‘;
        DFS(dex+1,num+1);
        map[p][q]=‘.‘;
    }
    DFS(dex+1,num);
    return ;
}

int main(){

    while(scanf("%d ",&n)==1&&n){
        for(int i=0;i<n;i++)
            gets(map[i]);
        ans=MIN;
        DFS(0,0);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-05 05:06:59

hdu 1045的相关文章

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

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

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

Fire Net HDU 1045

简单深搜,可以完全暴力,不会超时的. #include<iostream> #include<cstring> #include<cmath> using namespace std; #define MAX(a,b) (a>b?a:b) char maze[10][10]; int n, maxn; void DFS(int step,int count); int cheak(int x, int y); int main() { int i; while(s

HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, e

HDU 1045 Fire Net 贪心

Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through wh

hdu 1045 Fire Net(最小覆盖点+构图(缩点))

http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description Suppose that we have a square city with straight streets. A map of a city

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

HDU 1045 Fire Net 状压暴力

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8073    Accepted Submission(s): 4626 Problem Description Suppose that we have a squar

HDU 1045 (DFS搜索)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个O. 解题思路: 题目规模比较小(4*4),可以DFS解决. 对于一个点,要么放,要么不放. 放的话条件必须是上下左右四个方向扫到边界且不首先碰到X. 可以只对放的点进行标记,而对于不放的点不进行标记,这样当dep>n*n的时候及时return就行了. 注意每次dfs只需要按顺序考虑一个点,而不要同

HDU - 1045 Fire Net(二分匹配)

Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to s