hdu1045 2010.3.5
来源:互联网 发布:网络写手怎么赚钱 编辑:程序博客网 时间:2024/06/06 17:23
hdu1045 2010.3.5
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1253 Accepted Submission(s): 706
Problem Description
Suppose that we have a square city withstraight 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 hasfour openings through which to shoot. The four openings are facing North, East,South, and West, respectively. There will be one machine gun shooting througheach opening.
Here we assume that a bullet is so powerfulthat it can run across any distance and destroy a blockhouse on its way. On theother hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses ina city as possible so that no two can destroy each other. A configuration ofblockhouses is legal provided that no two blockhouses are on the samehorizontal row or vertical column in a map unless there is at least one wallseparating them. In this problem we will consider small square cities (at most4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures ofthe same board. The first picture is the empty board, the second and thirdpictures show legal configurations, and the fourth and fifth pictures showillegal configurations. For this board, the maximum number of blockhouses in alegal configuration is 5; the second picture shows one way to do it, but thereare several other ways.
Your task is to write a program that, givena description of a map, calculates the maximum number of blockhouses that canbe placed in the city in a legal configuration.
Input
The input file contains one or more mapdescriptions, followed by a line containing the number 0 that signals the endof the file. Each map description begins with a line containing a positiveinteger n that is the size of the city; n will be at most 4. The next n lineseach describe one row of the map, with a '.' indicating an open space and anuppercase 'X' indicating a wall. There are no spaces in the input file.
Output
For each test case, output one linecontaining the maximum number of blockhouses that can be placed in the city ina 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
Source
Zhejiang University LocalContest 2001
#include <stdio.h>#include <string.h>#define MAXN 10int map[MAXN][MAXN];int n,ans,goal;char ch;void outout(){/*/ int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%d",map[i][j]); printf("\n"); }*/}int find (int x,int y){ int i; for(i=x-1;i>=1;i--) { if (map[i][y]==0) break; if(map[i][y]==-1) return 0; } for(i=x+1;i<=n;i++) { if (map[i][y]==0) break; if(map[i][y]==-1) return 0; } for(i=y-1;i>=1;i--) { if (map[x][i]==0)break; if(map[x][i]==-1) return 0; } for(i=y+1;i<=n;i++) { if (map[x][i]==0) break; if(map[x][i]==-1) return 0; } return 1;}void deal(int x,int y){ int i,j; if (y==n) { for(i=x+1;i<=n;i++) for(j=1;j<=n;j++) if ((map[i][j]==1)&&(find(i,j)==1)) { map[i][j]=-1; ans++; if (ans>goal) { goal=ans; outout(); } deal(i,j); map[i][j]=1; ans--; } } else { i=x; for(j=y+1;j<=n;j++) if ((map[i][j]==1)&&(find(i,j)==1)) { map[i][j]=-1; ans++; if (ans>goal) { goal=ans; outout(); } deal(i,j); map[i][j]=1; ans--; } for(i=x+1;i<=n;i++) for(j=1;j<=n;j++) if ((map[i][j]==1)&&(find(i,j)==1)) { map[i][j]=-1; ans++; if (ans>goal) { outout(); goal=ans; } deal(i,j); map[i][j]=1; ans--; } }} void init(){ int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { ch=getchar(); if (ch=='.') map[i][j]=1; } getchar(); }}void main(){ int i,j; while (scanf("%d",&n),n) { getchar(); goal=0; memset(map,0,sizeof(map)); init(); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if ((map[i][j]==1)&&(find(i,j))) { ans=1; if (ans>goal) { outout(); goal=ans; } map[i][j]=-1; deal(i,j); map[i][j]=1; } } printf("%d\n",goal); }}
- hdu1045 2010.3.5
- HDU1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- hdu1045
- HDU1045
- hdu1045
- hdu1045
- hdu1045 (DFS)
- HDU1045 DFS
- hdu1045--搜索
- hdu1045【dfs】
- 贪心之hdu1045
- DFS之hdu1045
- java.util.concurrent - 1. Executor任务执行框架层次结构关系
- 14 jquery/js语法
- 简单DOM解析(一)
- 1004. 成绩排名
- hdu1071 2010.3.5
- hdu1045 2010.3.5
- Android- 友盟社会化分享【5.0版本集成文档】
- hdu1035 2010.3.5
- cron任务执行oracle冷备提示:Invalid format of Export utility name
- stm32 ADC的规则通道和注入通道混合使用 双adc dma tim触发 pwm 电机
- 买了移动硬盘,你知道怎么鉴别真伪,保养,分区吗???
- [Leetcode]166. Fraction to Recurring Decimal @python
- gradle编译时遇到的问题
- Arrays类