已转战浙大

题目 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的更多相关文章

  1. acm 1002 算法设计

    最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...

  2. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  3. 杭电ACM 1002题

    import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(S ...

  4. ZOJ 1002:Fire Net(DFS+回溯)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  5. C语言超大数据相加计算整理

    在做ACM 1002题时,整理得到. #include<stdio.h>#include<string.h>#define MAX 1000void zero(char *s, ...

  6. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

  7. [ZOJ 1002] Fire Net (简单地图搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...

  8. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  9. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

随机推荐

  1. ifconfig无输出的原因及解决办法

    问题 执行 ifconfig 命令无任何报错,也无任何输出信息 [root@linuxprobe ~]#  ifconfig[root@linuxprobe ~]# 排错 1. 检查PATH变量 [r ...

  2. Vue生命周期钩子详解【个人解读】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. java 的继承

    1 为什么要使用继承? 为了提取两个类中公共的代码,可以使用继承抽取重复性的代码到一个公共类中,这个公共的类称为父类(super class).继承于父类的类称为子类(sub class). java ...

  4. Python数据分析学习之Numpy

    Numpy的简单操作 import numpy #导入numpy包 file = numpy.genfromtxt("文件路径",delimiter=" ",d ...

  5. condition实现原理

    condition是对线程进行控制管理的接口,具体实现是AQS的一个内部类ConditionObject,主要功能是控制线程的启/停(这么说并不严格,还要有锁的竞争排队). condition主要方法 ...

  6. 10个重要的算法C语言实现源代码

    包括拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔 .都是经典的数学算法,希望能开托您的思路.转自kunli.info 1.拉格朗日插值多项式 ,用于离散数 ...

  7. PHP读取文件的多种方法

    1.传统的方法 fopen, fclose feof:file.end of file 例子: $file_handle = fopen("c:\\myfile.txt", &qu ...

  8. hdu 3794 Magic Coupon

    浙大计算机研究生保研复试上机考试-2011年  贪心: 注意:输入输出用scanf  printf 可以加快速度,用cin WA #include<iostream> #include&l ...

  9. KMeans实现

    KMeans实现 符号 \(K\): 聚类的个数 \(x^{(i)}\): 第i个样本 \(\mu_{1},\mu_{2},...\mu_{K}\): K个中心节点 \(c^{(i)}\): 第i个样 ...

  10. Golang教程:常量

    定义常量 常量(constant)表示固定的值,比如:5,-89,"I love Go",67.89 等等. 考虑如下程序: var b string = "I love ...