hdu1045 2010.3.5
2016-02-05 15:08
288 查看
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
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 10 int 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); } }
相关文章推荐
- hdu1035 2010.3.5
- android底部导航的实现调查
- golang语法学习(一):变量,常量以及数据类型
- 京东价格监控软件开发技术探讨五:京东验证码的获取
- 从头认识Spring-1.16 SpEl对集合的操作(2)-查询集合以及投影元素集合
- C语言 加法 大数加法
- 将.stl文件转化为.dae并动态加载到SceneKit显示(ios中显示3d模型)
- C语言获取系统时间的函数
- C语言获取系统时间的函数
- Android网络开源库Android-async-http 的基本用法(二)
- 后缀数组 nbut1171 后缀数组
- 1004. 成绩排名
- C语言获取系统时间的函数
- hdu1150 2010.3.5
- hdu1071 2010.3.5
- 14 jquery/js语法
- kNN(K-Nearest Neighbor)最邻近规则分类(转)
- 算术表达式转换为后缀表达式
- 封装实现UIButton左文字右图片
- URI API——公交、驾车、步行导航