[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[][4], int n, int &min_x, int &min_y)
{
    int min_point = MAX_POINT; // 赋哨兵值
    min_x = min_y = 0;

    for (int y = 0; y < n; y++)
    {
        for (int x = 0; x < n; x++)
        {
            if ( map[y][x] == OPEN)
            {
                int curr_point = 1;

                // 向上
                for (int j = y - 1; j >= 0 && map[j][x] != WALL; j--)
                {
                    if ( map[j][x] == OPEN)
                        curr_point++;
                }

                // 向下
                for (int j = y + 1; j < n && map[j][x] != WALL; j++)
                {
                    if ( map[j][x] == OPEN)
                        curr_point++;
                }

                // 向左
                for (int j = x - 1; j >= 0 && map[y][j] != WALL; j--)
                {
                    if ( map[y][j] == OPEN)
                        curr_point++;
                }

                // 向右
                for (int j = x + 1; 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[][4], int n, int x, int y)
{
    map[y][x] = CASTLE;

    // 向上
    for (int j = y - 1; j >= 0 && map[j][x] != WALL; j--)
    {
        map[j][x] = BLOCK;
    }

    // 向下
    for (int j = y + 1; j < n && map[j][x] != WALL; j++)
    {
        map[j][x] = BLOCK;
    }

    // 向左
    for (int j = x - 1; j >= 0 && map[y][j] != WALL; j--)
    {
        map[y][j] = BLOCK;
    }

    // 向右
    for (int j = x + 1; j < n && map[y][j] != WALL; j++)
    {
        map[y][j] = BLOCK;
    }
}

int main ()
{
    int map[4][4];
    std::list<int> answer;
    int n;
    char c;

    // 读取数据
    std::cin>>n;
    while (n != 0)
    {
        // 读取地图数据
        for (int y = 0; y < n; y++)
        {
            for (int x = 0; x < n; x++)
            {
                std::cin>>c;
                if (c == ‘.‘)
                    map[y][x] = OPEN;
                else
                    map[y][x] = WALL;
            }
        }

        // 处理地图
        int castle_number = 0;
        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 0;
}
时间: 2024-10-12 16:33:19

[acm 1002] 浙大 Fire Net的相关文章

acm 1002 算法设计

最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是问题描述: Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.   Input The fir

杭电acm 1002 大数模板(一)

从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的,但是经过自己的使用与调试也明白了其中的内涵. 首先定义大数的结构体: struct BigNum{ static const int BASE=100000000; static const int WIDTH=8; vector<int> s; BigNum(long long num=0){

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

在做ACM 1002题时,整理得到. #include<stdio.h>#include<string.h>#define MAX 1000void zero(char *s,int len){ int i; for(i=0;i<len;i++) s[i]-='0';}void back(char *s,int len){ int i; for(i=0;i<len;i++) s[i]+='0';}int main(){ char a[20][MAX],b[20][MAX

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋子,也有墙,问最多能放多少棋子使得棋子两两不会袭击? 棋子袭击当且仅当处在同一行或者同一列上并且中间没有墙的阻隔. 真是好久没写过搜索题了..这道题都写了这么久!! 直接写dfs(x,y,now) 代表我现在站在x点,y点上,那么就只能产生两种状态,放棋子或者不放棋子. 然后写一个C(x,y)代表当

ZOJ 1002 Fire Net

columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting thro

DFS ZOJ 1002/HDOJ 1045 Fire Net

题目传送门 1 /* 2 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 3 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1)左下角走,x = cnt / n; y = cnt % n; 更新坐标, 4 直到所有点走完为止,因为从左边走到右边,只要判断当前点左上方是否满足条件就可以了 5 注意:当前点不能放炮台的情况也要考虑 6 g[x][y] == 'o'; 的错误半天才检查出来:) 7 */ 8 #inc

ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this boar

2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 240 Problem Description This is a simple problem. The teacher gives Bob a list of prob

ZOJ 1002 Fire Net(DFS啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a