ZOJ 1002 Fire Net

来源:互联网 发布:网络共享盘打不开 编辑:程序博客网 时间:2024/05/23 19:13

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002


题目:

C - Fire Net
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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 islegal 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.

Sample input:

4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0

Sample output:

51524

题目大意:

两个炮台在同一水平和同一竖直线上可以互相攻击,有墙可以阻挡。求,在给出的地图的情况下,最多可以放多少台大炮。


解题思路:

遍历所有的点,考虑放置和不放置两种情况,找出最优解。很简单的深搜问题。

代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;void bfs(int x, int y); //深搜int canput(int x, int y); //判断是否可以把炮台放在相应的位置int tot = 0, m; //地图范围以及可以放置的炮台总数char rec[10][10]; //保存地图int main(){  while(scanf("%d", &m) != EOF && m)  {    tot = 0;  for(int i = 0; i < m; i++)    {    memset(rec[i], ‘\0’, sizeof(rec[i]));//初始化    scanf(“%s”, rec[i]); //输入地图    }    bfs(0, 0); //调用    printf(“%d\n”, tot); 输出  }  return 0;}void bfs(int x, int y) //用x记录二维数组的坐标,用y记录可以放下的炮台的个数{  if(x == m*m) //出口  {  if(tot < y)//筛选最小  {  tot = y;  return ;    }  }  else{    int col = x/m; //用一个数字表示二维数组    int row = x%m;    if(rec[col][row] == ‘.’ && canput(col,row)) //如果可以放下    {      rec[col][row] = ‘0’;//放下了的坐标用0标记    bfs(x+1, y+1); //递归    rec[col][row] = ‘.’;//标记之后再还原    }    bfs(x+1, y); //搜寻下一个,不放炮台       }}int canput(int col, int row) //可以放return1,不可以放return0.{  for(int i = m-1; i >= 0; i--)  {    if(rec[col][i] == 'X')    {    break;    }    if(rec[col][i] == '0')    {    return 0;    }  }  for(int i = m-1; i >= 0; i--)  {    if(rec[i][row] == '0')    {    return 0;    }    if(rec[i][row] == 'X')    {    break;    }  }  return 1;}



0 0