Fire Net(hdu1045)

Fire Net

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

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.

Output

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

....

2

XX

.X

3

.X.

X.X

.X.

3

...

.XX

.XX

4

....

....

....

....

0

Sample Output

5

1

5

2

4

//题意是一个地图,要在城市放尽可能多的碉堡,但是碉堡不能在同一行,或者同一列,在同一行必须有个坚固的墙

问最多可以放多少碉堡

//简单的DFS的应用,代码里写的很详细了。。

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 using namespace std;
  5
  6 char mp[4][4];
  7 int vis[4][4];
  8 int n;
  9 int ans;
 10
 11 void fang(int x,int y)//放下
 12 {
 13     vis[x][y]=1;
 14     int i;
 15     i=x;
 16     while (i+1<n&&mp[i+1][y]==‘.‘)
 17     {
 18         i++;
 19         vis[i][y]++;
 20     }
 21     i=x;
 22     while (i-1>=0&&mp[i-1][y]==‘.‘)
 23     {
 24         i--;
 25         vis[i][y]++;
 26     }
 27     i=y;
 28     while (i+1<n&&mp[x][i+1]==‘.‘)
 29     {
 30         i++;
 31         vis[x][i]++;
 32     }
 33     i=y;
 34     while (i-1>=0&&mp[x][i-1]==‘.‘)
 35     {
 36         i--;
 37         vis[x][i]++;
 38     }
 39 }
 40
 41 void che(int x,int y)//撤走
 42 {
 43     vis[x][y]=0;
 44     int i;
 45     i=x;
 46     while (i+1<n&&mp[i+1][y]==‘.‘)
 47     {
 48         i++;
 49         vis[i][y]--;
 50     }
 51     i=x;
 52     while (i-1>=0&&mp[i-1][y]==‘.‘)
 53     {
 54         i--;
 55         vis[i][y]--;
 56     }
 57     i=y;
 58     while (i+1<n&&mp[x][i+1]==‘.‘)
 59     {
 60         i++;
 61         vis[x][i]--;
 62     }
 63     i=y;
 64     while (i-1>=0&&mp[x][i-1]==‘.‘)
 65     {
 66         i--;
 67         vis[x][i]--;
 68     }
 69 }
 70
 71 int dfs(int l,int cnt)//去l行放置
 72 {
 73     if (cnt>ans)
 74     {
 75         ans=cnt;
 76     }
 77     if (l>=n) return 0; //出界
 78
 79     int i;
 80     for (i=0;i<n;i++)
 81     {
 82         if (mp[l][i]==‘.‘&&vis[l][i]==0)
 83         {
 84             fang (l,i);
 85             dfs(l,cnt+1);   //继续在这行放置,因为可能还可以放
 86             che(l,i);
 87         }
 88     }
 89     dfs(l+1,cnt);           //这一行都放完了就去下一行
 90     return 0;
 91 }
 92
 93 int main()
 94 {
 95     while (cin>>n&&n)
 96     {
 97         getchar();
 98         int i,j;
 99         for (i=0;i<n;i++)
100         {
101             for (j=0;j<n;j++)
102                 cin>>mp[i][j];
103             getchar();
104         }
105         ans=0;
106         memset(vis,0,sizeof(vis));
107         dfs(0,0);           //从第0行开始放,放置的个数初始值为 0
108         cout<<ans<<endl;
109     }
110     return 0;
111 }

时间: 2024-07-30 20:22:55

Fire Net(hdu1045)的相关文章

HDU1045 Fire Net 【DFS】

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

hdu1045 Fire Net

Fire Net Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 65   Accepted Submission(s) : 39 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Suppose that we have a squar

HDU1045 Fire Net(DFS)

题目链接: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): 8185    Accepted Submission(s): 4691 Problem Description Suppose that we have a squa

HDU1045 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): 12920    Accepted Submission(s): 7840 Problem Description Suppose that we have a squa

hdu1045 Fire Net(DFS枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=1045 这是在贪心分类里面的题目,然而我第一眼看到时候还是想到了dfs,毕竟只有4*4……数据小,枚举也能AC 用DFS枚举所有状态…… #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cma

【HDU-1045,Fire Net-纯暴力简单DFS】

原题链接:点击! 大致题意:白块表示可以放置炮台的位置--每个炮台可以攻击到上下左右的直线上的炮台(也就是说在它的上下左右直线上不可以再放置炮台,避免引起互相攻击),黑块表示隔离墙的位置--不可放置并且可以阻挡炮火:求对于一个最大4*4的格子来说,最大的放置炮台的个数是多少. 简单分析: 简单的dfs();由于本题地图很小,不用考虑太多,尽情暴力即可.先把DFS枝干画出来,然后枝叶就用函数来具体实现. AC代码:(附注释) 1 #include <iostream> 2 #include<

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