hdu1045 dfs

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9130    Accepted Submission(s): 5316


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 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.
 
Input
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.
 
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
 

Sample Output

5 1 5 2 4
 题目大意:给你一个地图,用'.'表示可以空地,用'X'表示围墙,让你在这个地图上放火炮,要求任意两个火炮不能攻击到彼此,即
火炮射程可以到一条直线上的无限远,但可以被围墙阻挡,问你最多可以放置多少个火炮。
思路分析:这是我做的第一道用dfs解决地图放置问题,大概是因为我对于状态的转移理解还不够深刻,做这道题的时候也出现了很多状况
。首先对于dfs开始的位置不是很明确,是直接从(0,0)开始,还是将每个点都作为起点遍历一遍,后来想明白了从(0,0)开始深搜就包
含了所有的情况,即从第一个点开始进行判断,首先判断其是否满足放炮台,判断的一条重要依据是他的左边和上边不能先出现另一个炮台,
如果出现该点就不满足放置炮台的条件,如果满足就标记该点,继续深搜,回溯时记得恢复该点的状态。对于如何判断深搜结束,一共有n*n
个位置,当位置达到n*n时,一条路径走完了,这时候将放置的炮台数量与保存的相比较,若大于,则取代保存值。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
const int maxn=10;
char maps[maxn][maxn];
int ans;
int n;
bool judge(int x,int y)
{
    for(int i=x;i>=0;i--)
    {
       if(maps[i][y]=='X')
       break;
       if(maps[i][y]=='*')
        return false;
    }
  for(int i=y;i>=0;i--)
 {
       if(maps[x][i]=='X') break;
       if(maps[x][i]=='*') return false;
 }
 return true;
}
void dfs(int k,int cnt)
{
     if(k==n*n)//一次深搜
     {
     if(cnt>ans)
        ans=cnt;
        return;
     }
    int nx=k/n,ny=k%n;
    if(maps[nx][ny]=='.'&&judge(nx,ny))
    {
        maps[nx][ny]='*';//放置炮台
        dfs(k+1,cnt+1);//步数加1,炮台数加1;
        maps[nx][ny]='.';//回溯时恢复该点状态
    }
    dfs(k+1,cnt);//若该点不放,步数加1,炮台数不变;
}
int main()
{
    int i,j;
    while(cin>>n&&n)
    {
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
          cin>>maps[i][j];
         ans=0;
         dfs(0,0);//第一个参数表示搜索到的位置,第二个参数
         cout<<ans<<endl;
    }
    return 0;
}
 
 
 
 
 
原文地址:https://www.cnblogs.com/xuejianye/p/5317469.html