ACM-Fire Net
输入
- 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.
输出
- For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
样例输入
- 4
- .X..
- ....
- XX..
- ....
- 2
- XX
- .X
- 3
- .X.
- X.X
- .X.
- 3
- ...
- .XX
- .XX
- 4
- ....
- ....
- ....
- ....
- 0
样例输出
- 5
- 1
- 5
- 2
- 4
这道题看着麻烦,实际就是讲了棋盘放位置的策略。总体和n皇后问题很类似。
- // Fire Net.cpp : 定义控制台应用程序的入口点。
- //
- //解题思路:
- //1.类似n皇后问题,上下左右不能有重复,只检测上方和左方即可,下面的没有放到,不需要检查。
- //2.走斜角坐标,x = pos/n,y = pos % n
- //3.每个坐标存在能放和不能放两种情况
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #define M 10
- int n,ans;
- char map[M][M];
- int check(int x, int y)
- {
- if (map[x][y] == 'X') return ;
- //左方
- for (int col = y - ; col >= ; col--)
- {
- if (map[x][col] == 'X')
- break;
- if (map[x][col] == '#')
- return ;
- }
- //上方
- for (int row = x - ; row >= ; row--)
- {
- if (map[row][y] == 'X')
- break;
- if (map[row][y] == '#')
- return ;
- }
- return ;
- }
- void DFS(int pos, int sum)
- {
- int x = pos / n, y = pos % n;//x,y 的位置
- if (pos == n * n)
- {
- ans = ans > sum? ans:sum;
- return;
- }
- if (check(x, y))
- {
- map[x][y] = '#';
- DFS(pos + , sum + );
- map[x][y] = '.';
- }
- DFS(pos + , sum);
- }
- int main()
- {
- while (~scanf("%d", &n) && n)
- {
- memset(map, '\0', sizeof(map));
- for (int i = ; i < n; i++)
- scanf("%s", &map[i]);
- ans = ;
- DFS(,);
- printf("%d\n", ans);
- }
- return ;
- }
ACM-Fire Net的更多相关文章
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 【Acm】算法之美—Fire Net
题目概述:Fire Net Suppose that we have a square city with straight streets. A map of a city is a square ...
- [acm 1002] 浙大 Fire Net
已转战浙大 题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 浙大acm 1002 #include <iostre ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- ACM -二分图题目小结
暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063 过 ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- zoj 3820 Building Fire Stations 树的中心
Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...
- HDU-1045 Fire Net
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) Me ...
- (FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
随机推荐
- SqlSession为什么可以提交事务
本应在开始读MyBatis源码时首先应该了解下MyBatis的SqlSession的四大对象:Executor.StatemenHandler.ParameterHandler.ResultHandl ...
- 【剑指Offer面试编程题】题目1390:矩形覆盖--九度OJ
题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入 ...
- IDEA 服务器热部署详解(On Update action/On frame deactivation)
https://blog.csdn.net/w15321271041/article/details/80597962 场景:一般服务器(比如tomcat,jboss等)启动以后,我们还需要进一步修改 ...
- windows下用libevent 开发一个echo服务
#include <stdio.h> #include <string.h> #include <errno.h> #include <iostream> ...
- python学习笔记(三)---高级特性
一.切片 取无数多个list元素 不用一个个取得笨方法就用切片 对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python提供了切片(Slice)操作符,能大大简化这种操作. 对应上面的问题, ...
- 「JSOI2007」建筑抢修
传送门 Luogu 解题思路 显然先把所有楼按照报废时间递增排序. 然后考虑 \(1\cdots i-1\) 都能修完, \(i\) 修不完的情况. 显然我们在这 \(i\) 个里面至多只能修 \(i ...
- IOS pin约束问题 存在间隙
今天在为自己的view添加约束 对比以前添加的约束时,发现有有两层淡红色线框一条实线和一条虚线,而以前一个demo中添加的则只有一个蓝色实线框. 今天添加的约束如图1所示: 图1 而以前添加约束如图2 ...
- List循环添加数据覆盖问题
问题:java开发时,当我们使用List.add();循环添加数据,有时会出现前面添加的数据会被后面覆盖的现象.这是怎么回事尼? 会覆盖数据的代码 package com.boot.test; imp ...
- 2.13 阶段实战 使用layui重构选课系统
一.说在前面 昨天 学习表单校验插件validate,并使用ajax 自定义校验规则 今天 使用layui重构选课系统 二.题目要求 1.项目需求: 本项目所开发的学生选课系统完成学校对学生 ...
- POJ 3268:Silver Cow Party 求单点的来回最短路径
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15989 Accepted: 7303 ...