[acm 1002] 浙大 Fire Net
已转战浙大
题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2
浙大acm 1002
#include <iostream>
#include <cstdlib>
#include <cmath> #include <list> #define OPEN 1
#define CASTLE 2
#define BLOCK 3
#define WALL 4 // summary: 用贪心原理找到一个影响地图最小的城堡的位置
// param in: map[] 输入的地图. n 地图大小.
// param out: min_x,min_y 得出的放置的城堡的x,y坐标
// return: true 找到了合适的位置. false 没有可以放置城堡的地方了
#define MAX_POINT 10000 // 哨兵数
bool FindMinInfluencePosition(int map[][], int n, int &min_x, int &min_y)
{
int min_point = MAX_POINT; // 赋哨兵值
min_x = min_y = ; for (int y = ; y < n; y++)
{
for (int x = ; x < n; x++)
{
if ( map[y][x] == OPEN)
{
int curr_point = ; // 向上
for (int j = y - ; j >= && map[j][x] != WALL; j--)
{
if ( map[j][x] == OPEN)
curr_point++;
} // 向下
for (int j = y + ; j < n && map[j][x] != WALL; j++)
{
if ( map[j][x] == OPEN)
curr_point++;
} // 向左
for (int j = x - ; j >= && map[y][j] != WALL; j--)
{
if ( map[y][j] == OPEN)
curr_point++;
} // 向右
for (int j = x + ; j < n && map[y][j] != WALL; j++)
{
if ( map[y][j] == OPEN)
curr_point++;
} if (curr_point < min_point)
{
min_point = curr_point;
min_x = x;
min_y = y;
}
}
}
} // 检测是否放置了城堡
if (min_point != MAX_POINT)
return true;
else
return false; } // summary: 根据位置放置城堡,在地图上标记出来
// param in: map[] 输入的地图. n 地图大小. x, y 放置的城堡的位置
void PlaceCastle(int map[][], int n, int x, int y)
{
map[y][x] = CASTLE; // 向上
for (int j = y - ; j >= && map[j][x] != WALL; j--)
{
map[j][x] = BLOCK;
} // 向下
for (int j = y + ; j < n && map[j][x] != WALL; j++)
{
map[j][x] = BLOCK;
} // 向左
for (int j = x - ; j >= && map[y][j] != WALL; j--)
{
map[y][j] = BLOCK;
} // 向右
for (int j = x + ; j < n && map[y][j] != WALL; j++)
{
map[y][j] = BLOCK;
}
} int main ()
{
int map[][];
std::list<int> answer;
int n;
char c; // 读取数据
std::cin>>n;
while (n != )
{
// 读取地图数据
for (int y = ; y < n; y++)
{
for (int x = ; x < n; x++)
{
std::cin>>c;
if (c == '.')
map[y][x] = OPEN;
else
map[y][x] = WALL;
}
} // 处理地图
int castle_number = ;
int min_x, min_y;
while (FindMinInfluencePosition(map, n, min_x, min_y) == true )
{
castle_number++;
PlaceCastle(map, n, min_x, min_y);
} // 记录数据
answer.push_back(castle_number); // 输入下一个数
std::cin>>n;
} // 输出数目
for (std::list<int>::iterator it=answer.begin() ; it != answer.end(); ++it)
std::cout <<*it<<"\n"; return ;
}
[acm 1002] 浙大 Fire Net的更多相关文章
- acm 1002 算法设计
最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- 杭电ACM 1002题
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(S ...
- ZOJ 1002:Fire Net(DFS+回溯)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- C语言超大数据相加计算整理
在做ACM 1002题时,整理得到. #include<stdio.h>#include<string.h>#define MAX 1000void zero(char *s, ...
- DFS ZOJ 1002/HDOJ 1045 Fire Net
题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...
- [ZOJ 1002] Fire Net (简单地图搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- zoj 1002 Fire Net (二分匹配)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
随机推荐
- PIE SDK与GeoServer结合说明文档
1. GeoServer简介 GeoServer是OpenGIS Web服务器规范的J2EE实现的社区开源项目,利用GeoServer可以方便的发布地图数据,允许用户对特征数据进行更新.删除.插入操 ...
- SQL chema的新增和修改
1.先要创建你自己的schema create schema myschema 2. alter schema myschema transfer ado.User --执行完后,User表就 ...
- .Net C# 泛型序列化和反序列化JavaScriptSerializer
项目添加引用System.Web.Extensions /// <summary> /// 泛型序列化 /// </summary> public class JsonHelp ...
- 一篇在一个Excel表中创建多个sheet的代码
package projectUtil; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermod ...
- es与hive整合
在hive classpath中添加elasticsearch-hadoop.jar,以下方法任一种均可: 1.启动hiveserver2 前,在hive-site.xml文件中更改hive.aux. ...
- 07-oracle多表查询
--笛卡尔积,多表查询时,n张表中的行数相乘(本例中14*4=56)--多表查询时笛卡尔积无法消除,即使使用了限定条件(where)也只是不显示而已,实际上笛卡尔积仍存在 --只能使用合理的做法来处理 ...
- maven打包时跳过测试类
记录 修改pom.xml文件 <project> [...] <build> <plugins> <plugin> <groupId>org ...
- ScriptManager.RegisterStartupScript失效的解决方案
在项目中一个页面使用System.Web.UI.ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success ...
- orcale 之函数
我们知道存储过程的调用是一条 PL/SQL 语句.那么对于一些表达式我们用什么呢?这里函数的功能就会体现出来了. 函数和过程在创建方式上有很多的类似地方,也是编译后放入内存中以供用户使用,只不过函数调 ...
- PHP中的PHP_EOL和DIRECTORY_SEPARATOR
1. PHP_EOL是php中的换行符,跨平台 1.1.换行符: unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 ...