/*
ID: lucien23
PROG: castle
LANG: C++
*/
/************************************************************************/
/* 求图的连通域问题。利用广度扫描 */
/************************************************************************/
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <queue>
using namespace std; typedef struct Position
{
int row;
int col;
Position(){}
Position (int r, int c)
{
row = r;
col = c;
}
} Position;
bool isPrior(Position pos1, char direction1, Position pos2, char direction2);
int main()
{
ifstream infile("castle.in");
ofstream outfile("castle.out");
if(!infile || !outfile)
{
cout << "file operation failure!" << endl;
return -1;
} int M, N;
infile >> M >> N;
int ***walls = new int**[N];
for (int i=0; i<N; i++)
{
walls[i] = new int*[M];
for (int j = 0; j < M; j++)
{
walls[i][j] = new int[6]();
infile >> walls[i][j][0];
switch (walls[i][j][0])
{
case 0:
break;
case 1:
walls[i][j][1] = 1;
break;
case 2:
walls[i][j][2] = 1;
break;
case 3:
walls[i][j][1] = 1;
walls[i][j][2] = 1;
break;
case 4:
walls[i][j][3] = 1;
break;
case 5:
walls[i][j][1] = 1;
walls[i][j][3] = 1;
break;
case 6:
walls[i][j][2] = 1;
walls[i][j][3] = 1;
break;
case 7:
walls[i][j][1] = 1;
walls[i][j][2] = 1;
walls[i][j][3] = 1;
break;
case 8:
walls[i][j][4] = 1;
break;
case 9:
walls[i][j][1] = 1;
walls[i][j][4] = 1;
break;
case 10:
walls[i][j][2] = 1;
walls[i][j][4] = 1;
break;
case 11:
walls[i][j][1] = 1;
walls[i][j][2] = 1;
walls[i][j][4] = 1;
break;
case 12:
walls[i][j][3] = 1;
walls[i][j][4] = 1;
break;
case 13:
walls[i][j][1] = 1;
walls[i][j][3] = 1;
walls[i][j][4] = 1;
break;
case 14:
walls[i][j][2] = 1;
walls[i][j][3] = 1;
walls[i][j][4] = 1;
break;
case 15:
walls[i][j][1] = 1;
walls[i][j][2] = 1;
walls[i][j][3] = 1;
walls[i][j][4] = 1;
break;
default:
break;
}
}
} vector<int> components;
int cnt = 0;
for (int i=0; i<N; i++)
{
for (int j=0; j<M; j++)
{
if (walls[i][j][5] == 0)
{
cnt++;
components.push_back(0);
queue<Position> nodes;
nodes.push(Position(i, j));
while (!nodes.empty())
{
Position pos = nodes.front();
int m = pos.row;
int n = pos.col;
int *currentNode = walls[m][n];
nodes.pop();
if (currentNode[5] != 0)
continue;
components[cnt-1]++; currentNode[5] = cnt;
if (currentNode[1] == 0 && walls[m][n-1][5] == 0)
{
nodes.push(Position(m ,n-1));
}
if (currentNode[2] == 0 && walls[m-1][n][5] == 0)
{
nodes.push(Position(m-1, n));
}
if (currentNode[3] == 0 && walls[m][n+1][5] == 0)
{
nodes.push(Position(m, n+1));
}
if (currentNode[4] == 0 && walls[m+1][n][5] == 0)
{
nodes.push(Position(m+1, n));
}
}
}
}
} int newRoom = 0;
Position wallPos;
char dirction; for (int i=0; i<N; i++)
{
for (int j=0; j<M; j++)
{
if (walls[i][j][1] == 1 && j-1>=0 && walls[i][j][5] != walls[i][j-1][5])
{
int sum = components[walls[i][j][5]-1] + components[walls[i][j-1][5]-1];
Position newPos = Position(i, j-1);
if (sum > newRoom || (sum == newRoom && isPrior(newPos, 'E', wallPos, dirction)))
{
newRoom = sum;
wallPos = newPos;
dirction = 'E';
}
} if (walls[i][j][2] == 1 && i-1>=0 && walls[i][j][5] != walls[i-1][j][5])
{
int sum = components[walls[i][j][5]-1] + components[walls[i-1][j][5]-1];
Position newPos = Position(i, j);
if (sum > newRoom || (sum == newRoom && isPrior(newPos, 'N', wallPos, dirction)))
{
newRoom = sum;
wallPos = newPos;
dirction = 'N';
}
} if (walls[i][j][3] == 1 && j+1<M && walls[i][j][5] != walls[i][j+1][5])
{
int sum = components[walls[i][j][5]-1] + components[walls[i][j+1][5]-1];
Position newPos = Position(i, j);
if (sum > newRoom || (sum == newRoom && isPrior(newPos, 'E', wallPos, dirction)))
{
newRoom = sum;
wallPos = newPos;
dirction = 'E';
}
} if (walls[i][j][4] == 1 && i+1<N && walls[i][j][5] != walls[i+1][j][5])
{
int sum = components[walls[i][j][5]-1] + components[walls[i+1][j][5]-1];
Position newPos = Position(i+1, j);
if (sum > newRoom || (sum == newRoom && isPrior(newPos, 'N', wallPos, dirction)))
{
newRoom = sum;
wallPos = newPos;
dirction = 'N';
}
}
}
} int maxRoom = components[0];
for (int i=1; i<cnt; i++)
{
if (components[i] > maxRoom)
maxRoom = components[i];
}
outfile << cnt << endl << maxRoom << endl << newRoom << endl
<< wallPos.row+1 << " " << wallPos.col+1 << " " << dirction <<endl; return 0;
} bool isPrior(Position pos1, char direction1, Position pos2, char direction2)
{
if(pos1.col < pos2.col || (pos1.col == pos2.col && pos1.row > pos2.row)
|| (pos1.col == pos2.col && pos1.row == pos2.row && direction1 == 'N' && direction2 == 'E'))
{
return true;
}
return false;
}

USACO Section 2.1 The Castle的更多相关文章

  1. USACO Section 2.1 The Castle 解题报告

    题目 题目描述 有一个城堡,城堡中有若干个房间,房间与房间之间用墙来进行分隔.现在我们需要统计这个城堡有多少个房间,并且还要找出最大的房间的面积是多少(一个单元格就代表一个单元面积).城堡的主人现在想 ...

  2. [USACO Section 2.1]城堡 The Castle (搜索)

    题目链接 Solution 比较恶心的搜索,思路很简单,直接广搜找联通块即可. 但是细节很多,要注意的地方很多.所以直接看代码吧... Code #include<bits/stdc++.h&g ...

  3. USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)

    usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...

  4. 【USACO 2.1】The Castle

    /* TASK: castle LANG: C++ SOLVE: 深搜,注意每个方向对应值.枚举去掉的墙,然后再dfs,注意墙要复原,并且dfs里要判断是否超出边界. */ #include<c ...

  5. USACO Section 3.3: Riding the Fences

    典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...

  6. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  7. [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

    nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...

  8. USACO Section 5.3 Big Barn(dp)

    USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1  (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...

  9. USACO Section 1.3 Prime Cryptarithm 解题报告

    题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...

随机推荐

  1. Python通过跳板机链接MySQL的一种方法

  2. ABP框架个人开发实战(1)_环境搭建

    前言 之前关注ABP框架有一阵子了,一直没有潜下心来实际研究一下.最近想自己建站,以后有自己的功能开发项目,可以在自己的站点上开发,并一步步的完善,所以找个比较好用的框架迫在眉睫,选来选去,决定用AB ...

  3. 数组去重+indexOf()应用

    说起数组去重大家都不陌生,去重也有好多种方法,这里介绍很好理解的两种. 第一种 首先说一下第一种的逻辑,就是先拿第一个去跟第二个比,再跟第三个比,再跟第四个比--只要发现有相等的,可以用splice( ...

  4. 关于 innodb_stats_on_metadata 的设置问题

    [问题背景] 线上使用osc进行表修改的时候出现SQL执行过长被kill的问题

  5. Liunx vi编辑器一些指令

    最近几天学习了Liunx vi编辑器 的使用,感觉还比较容易.总结的一点心得: vi分为3个模式,命令模式,尾行模式,编辑模式. 1. 命令模式 与 编辑模式切换 a:光标向后移动一位进入编辑模式 i ...

  6. array_unique和array_flip 实现去重间的区别

    array_unique和array_flip 实现去重间的区别 ​php有内置函数array_unique可以用来删除数组中的重复值, phperz~com (PHP 4 >= 4.0.1,  ...

  7. linux 运维常用工具表

    https://code.google.com/p/httperf/  ※测量Web服务器的性能 ./configure   make &&make install http://ww ...

  8. Celery 源码解析八:State 和 Result

    在前面几篇解析中,我们已经看过了 Worker 是如何运行的,Task 是如何创建的,以及怎么被路由到 Worker 中,除了这些之外,我们还对流量限制,Worker 控制和 Task/Worker ...

  9. Ionic3学习笔记(十一)实现省市区三级联动

    本文为原创文章,转载请标明出处 目录 安装 ion-multi-picker 导入 app.module.ts 创建 provider 创建 page 一个坑 更多 效果图 1. 安装 ion-mul ...

  10. geotrellis使用(三十五)Cesium加载geotrellis TMS瓦片

    前言 做任何事情都不是想象中的那么简单.好久没有更新技术博客了,跟最近瞎忙有很大关系,虽说是瞎忙也抽空研究了些技术. 主要是前端渲染,像原生的WebGL和Cesium.WebGL写了几篇博客,自我感觉 ...