ZOJ1002-Fire Net
2016-05-01 20:01
344 查看
Fire NetTime Limit: 2 Seconds Memory Limit: 65536 KBSuppose 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 shootingthrough 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 providedthat 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 picturesshow 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 apositive 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 spacesin 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.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0Sample output:
5 1 5 2 4
#include <iostream>#include <stdio.h>using namespace std;int maxn,n;char map[5][5];int judge(int row,int col){for(int i=row-1;i>=0;i--){if(map[i][col]=='0') return 0;if(map[i][col]=='X') break;}for(int i=col-1;i>=0;i--){if(map[row][i]=='0') return 0;if(map[row][i]=='X') break;}return 1;}void solve(int k,int coun){if(k==n*n){if(coun>maxn){maxn=coun;return ;}}else{int x=k/n;int y=k%n;if(map[x][y]=='.'&&judge(x,y)==1){map[x][y]='0';solve(k+1,coun+1);map[x][y]='.';}solve(k+1,coun);}}int main(){while(~scanf("%d",&n)){if(n==0) break;for(int i=0;i<n;i++){getchar();for(int j=0;j<n;j++)scanf("%c",&map[i][j]);}maxn=0;solve(0,0);printf("%d\n",maxn);}return 0;}
相关文章推荐
- linux mysql配置
- 在装有Nvidia独显的台式机上安装Ubuntu 14.04 TLS
- 突发奇想:消息机制,以及Windows自带控件,都可以到ReactOS里去寻找答案
- CoreThink主题开发(二)使用H-ui开发博客主题之把H-ui放入你的主题目录中
- nginx php-fpm中启用慢日志配置(用于检测执行较慢的PHP脚本)
- java二维数组
- HDU1242-Rescue
- leetcode——131——Palindrome Partitioning
- windows系统下自定义图标
- Andoird Studio 字体、主题简单设置
- 自己对Handler的一些理解
- 冲刺10
- leetcode 064 Minimum Path Sum
- HDU1272-小希的迷宫
- 调用QQ截图
- 学习进度条 第九周
- 算法的艺术
- Django build_in 登录注册模块 注销时页面是admin模块自带的注销页面的原因
- lightoj - 1414 February 29
- 冲刺9