已转战浙大

题目 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. Locust源码目录结构及模块作用

    Locust源码目录结构及模块作用如下: 参考文章:https://blog.csdn.net/biheyu828/article/details/84031942

  2. shell map使用

    # 定义初始化map declare -A map=([") # 输出所有key echo ${map[@]} # 输出key对应的值 "]} # 遍历map for key in ...

  3. [Java基础]-- Java GC 垃圾回收器的分类和优缺点

    https://blog.csdn.net/high2011/article/details/80177473?utm_source=blogxgwz2 参考:elasticsearch实战-使用G1 ...

  4. RPC的故事

    今天我跟几个RPC框架之间发生了一些事,情节跌宕起伏一波三折,不吐不快,以至于我这个从来不写博客的人也忍不住写下来分享一下. 背景 主系统部署在Windows上(.NET 4.5),子系统(.NET ...

  5. js 打印指定页面部分打印

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  6. VS快捷键设置无效

    使用Resharper 后发现有些快捷键冲突,但是在工具-选项-键盘 设置后不管用,后来发现有一个移除功能,即移走原来的快捷键; 先选择下拉框1中自己用不到的快捷键,然后移除掉; 备注: 注意观察 快 ...

  7. HTTP 错误 404.11 - Not Found 请求筛选模块被配置为拒绝包含双重转义序列的请求。

    一些URL中可能会包含+号等符号,然后IIS7以上的版本会默认拒绝请求此URL,需要进行如下的修改. <configuration> <system.webServer> &l ...

  8. first post

    post

  9. spring-boot配置log4j日志

    spring boot默认使用logback日志记录工具,修改为log4j: <dependency> <groupId>org.springframework.boot< ...

  10. java.lang.UnsupportedClassVersionError: action/Login : Unsupported major.minor version 52.0 (unable to load class action.Login)异常

    用myeclipse新建一个web项目,用了struts2框架,tomcat启动的时候报了这个错误. 我的问题原因是tomcat7的运行环境不知道为什么设置成了myeclipse1.7的jre,我给它 ...