[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 ...
随机推荐
- final学习
类加载过程 1.装载:查找和导入Class文件 2.链接:其中解析步骤是可以选择的 (a)检查:检查载入的class文件数据的正确性 (b)准备:给类的静态变量分配存储空间 (c)解析:将符号引用转成 ...
- 集合 相关 深浅copy
'' 集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. {} ''' # set1 = set({1,2,3}) # set2 = {1,2,3,[2,3],{'name':' ...
- eureka 和zookeeper 区别 优势
作为服务注册中心,Eureka比Zookeeper好在哪里 著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的, ...
- C# 直接创建一个DataTable,并为之添加数据(自定义DataTable) 转
DataTable dt=new DataTable("cart"); DataColumn dc1=new DataColumn("prizename",Ty ...
- openerp学习笔记 模块结构分析
以OpenERP7.0中的 hr_expense 模块为例: 如图中代码所示: __init__.py :和普通 Python 模块中的__init__.py 作用相同,主要用于引用模块根目录下的.p ...
- Django 配置访问静态文件
1.settings.py 首先在 settings 文件中,引用 os 模块: import os 定义根目录: BASE_DIR = os.path.dirname(os.path.dirna ...
- line-height详解
line-height详解 要说line-height就必须要知道这几个概念了: 顶线.中线.基线.底线. 这也就是在vertical-align中可能用到的top,middle,baseline和b ...
- Git 学习之关于版本库
记得在第一次接触代码的时候,当对一些改动不是很确定的时候,我的做法就是在我的电脑上保留多个文件,分别以不同的名字来保存,以便于以后发现某个地方的带动是错误的好做修改,现在想想真是好笑啊. 慢慢的在工作 ...
- 用泛型T替代object做为万能参数传递
using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public cla ...
- 使用SubstanceDesign和Unity插件ShaderForge制作风格化火焰
使用 SubstanceDesign 软件可以制作shader用的特殊图片,原来真有这种软件,一直好奇这种图片怎么做的 https://www.kancloud.cn/hazukiaoi/sd_sf_ ...