nyoj 587 blockhouses(DFS)

blockhouses

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述
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 shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through
each opening.

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal
row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show
illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.

Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

输入
The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the
city; n will be at most 4. The next n lines each describe one row of the map, with a ‘.‘ indicating an open space and an uppercase ‘X‘ indicating a wall. There are no spaces in the input file.
输出
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
样例输入
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0

样例输出

5

1

5

2

4

AC代码:

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std ;

char mp[100][100] ;
int n,maxn ;

int check(int r,int c)
{
    int flag = 1 ;
    for(int i = r ;i>=0 ;i--)
    {
        if(mp[i][c] == 'X')
            break ;
        if(mp[i][c] == 'b')
        {
            flag = 0 ;
            break ;
        }
    }
    for(int j = c ;j>=0 ;j--)
    {
        if(mp[r][j] == 'X')
            break ;
        if(mp[r][j] == 'b')
        {
            flag = 0 ;
            break ;
        }
    }
    return flag ;
}

void dfs(int pos,int sum)
{
    int r = pos/n ,c=pos%n;
    if(pos == n*n)
    {
        if(sum > maxn)
           maxn = sum ;
        return ;
    }
    if(mp[r][c] == '.')
    {
        if(check(r,c))
        {
            mp[r][c] = 'b' ;
            dfs(pos+1,sum+1) ;
            mp[r][c] = '.' ;
        }
    }
    dfs(pos+1,sum) ;
}

int main()
{
    while(scanf("%d",&n)!=EOF && n)
    {
        memset(mp,0,sizeof(mp)) ;
        for(int i = 0 ;i<n ;i++)
            for(int j = 0 ;j<n ;j++)
        {
            cin>>mp[i][j] ;
        }
        maxn = 0 ;
        dfs(0,0) ;
        printf("%d\n",maxn) ;
    }
    return 0 ;
}
</span>
时间: 2024-08-26 13:36:41

nyoj 587 blockhouses(DFS)的相关文章

NYOJ 587 blockhouses 【DFS】

blockhouses 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 th

nyoj素数环(dfs)

素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输入 有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束. 输出 每组第一行输出对应的Case序号,从1开始. 如果存在满足题意叙述的素数环,从小到大输出. 否则输出No Answer. 样例输入 6 8 3 0 样

NYOJ 迷宫寻宝(一)深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 做这道题的时候迷迷糊糊的,,果然比较难..最后也是没有做出来..请教了一下学长,学长说我基础还不好..基础果然重要,这道题是一道搜索题,我没有考虑钥匙在门后面的情况,比如aBbSAG 多亏学长指教,通过这道题对深搜的理解又加深了一步~ #include <iostream> #include <cstdio> #include <cstring> using

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

nyoj 12 喷水装置(二)【贪心】+【区间完全覆盖覆盖】

题意:... 这道题就是区间问题三种中的区间完全覆盖问题,不懂的可以看我上一篇也是区间完全覆盖. 直接上代码: #include <stdio.h> #include <math.h> #include <algorithm> using std::sort; struct node{ double le, ri; }s[1005]; int cmp(node a, node b) { return a.le < b.le; } int main() { int

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in