HDU Fire Net (贪心)
来源:互联网 发布:java的多态 编辑:程序博客网 时间:2024/06/05 01:16
Fire Net
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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......2XX.X3.X.X.X.X.3....XX.XX4................0
Sample Output
51524
Source
Zhejiang University Local Contest 2001
AC代码:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;int map[4][4];int my[4][4];int out;void fun(int n){ memset(my,0,sizeof(my)); int i,j,k,t,ii,jj,tt; for(i=0;i<n;++i){ for(j=0;j<n;++j){ if(map[i][j]==0){ my[i][j]++; for(k=i-1;k>=0&&map[k][j]==0;k--)my[i][j]++; for(k=i+1;k<n&&map[k][j]==0;k++)my[i][j]++; for(k=j-1;k>=0&&map[i][k]==0;k--)my[i][j]++; for(k=j+1;k<n&&map[i][k]==0;k++)my[i][j]++; } } } out=0; /*for(ii=0;ii<n;++ii){ for(jj=0;jj<n;jj++) cout<<my[ii][jj]<<' '; cout<<'\12'; }*/ for(i=0;i<n;++i){ t=8; for(j=0;j<n;++j){ if(my[i][j]>0&&my[i][j]<=t){ t=my[i][j]; ii=i; jj=j; } } if(t!=8){ for(k=ii-1;k>=0&&map[k][jj]==0;k--)my[k][jj]=8; for(k=ii+1;k<n&&map[k][jj]==0;k++)my[k][jj]=8; for(k=jj-1;k>=0&&map[ii][k]==0;k--)my[ii][k]=8; for(k=jj+1;k<n&&map[ii][k]==0;k++)my[ii][k]=8; my[ii][jj]=8; out++; i--; } }}int main(){ int n,i,j; char c; while(cin>>n&&n){ memset(map,0,sizeof(map)); int s1=0,s2=0; for(i=0; i<n; ++i){ for(j=0; j<n; ++j){ cin>>c; map[i][j]=(c=='.')?0:1; if(map[i][j])s1++; } } if(s1==0){ cout<<n<<'\12'; continue; } if(s1==n*n){ cout<<0<<'\12'; continue; } out=0; fun(n); /*for(i=0; i<n; ++i){ for(j=0; j<n; ++j)cout<<map[i][j]<<' '; cout<<'\12';}*/ printf("%d\n",out); } return 0;}
0 0
- HDU Fire Net (贪心)
- hdu 1045 Fire Net(贪心)
- HDU 1045 Fire Net(贪心)
- HDU 1045 Fire Net 贪心
- 【hdu】 Fire Net (dfs)
- hdu Fire Net(DFS)
- Fire Net 搜索 贪心
- HDU 1045 Fire Net(搜索/贪心/二分图最大匹配)
- Hdu1045 - Fire Net - 贪心算法
- 贪心算法-HDU1045-Fire Net
- HDU 1045 Fire Net(DFS)
- hdu 1045 Fire Net (DFS)
- hdu 1045 Fire Net(回溯搜索)
- HDU 1045 Fire Net(DFS)
- hdu 1045 Fire Net(DFS)
- hdu 1045 Fire Net(最大流)
- HDU 1045 Fire Net(二分匹配)
- HDU 1045 Fire Net(搜索剪枝)
- Java学习笔记(九):Map.keyset()
- hackerrank maxsum mod
- Android Scroller简单用法
- HDOJ 1022 Train Problem I
- XML解析类CMarkup的使用方法
- HDU Fire Net (贪心)
- Mysql常用命令
- 冒泡排序—do-while语句实现
- TCP/IP协议族-----5、IPv4地址
- softether vpn client 连不上VPN
- Android NDK开发之 NDK 局部 全局引用
- P、*P和&P三者的区别
- IRP和IO_STACK_LOCATION结构的关联
- linux环境编程之线程基础知识