/*
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. AngularJS学习篇(十二)

    AngularJS SQL ASP.NET 中执行 SQL 获取数据 <!DOCTYPE html> <html> <head> <meta charset= ...

  2. header操作cookie

    root@kl20080094:~# curl -I "http://www.xxx.com" HTTP/1.1 200 OK Server: nginx/0.8.53 Date: ...

  3. POI不同版本替换Word模板时的问题

    一.问题描述 通过POI,把Word中的占位符替换为实际的值,以生成复杂结构的业务报告. 在POI 3.9上,功能正常.由于某些原因升级到POI 3.10.1后,项目组反馈说Word模板出错,无法生成 ...

  4. 11) 十分钟学会android--Intent消息处理与传递详解

    一个Android app通常都会有多个activities. 每个activity的界面都扮演者用户接口的角色,允许用户执行一些特定任务(例如查看地图或者是开始拍照等).为了让用户能够从一个acti ...

  5. Lucene全文检索引擎

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. Spring+Spring MVC+MyBatis框架集成

    目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六.JUnit测试数据访问 七.完成Spring整合My ...

  7. 关于 ElesticSearch 安装

    ElesticSearch windows 下安装步骤 1. 配置 JAVA_HOME 环境变量,因为作者是一个java开发人员,这是基本配置,就不多做赘述 2. 安装ElasticSearch 从官 ...

  8. Windows命令行command的Shell命令详细解析和语法

    CMD命令大全及详细解释和语法 Microsoft Windows XP [版本 5.1.2600] 有关某个命令的详细信息,请键入 HELP 命令名 ASSOC    显示或修改文件扩展名关联. A ...

  9. Linux系列教程(二十一)——Linux的bash基本功能

    上篇博客我们介绍了什么是shell,以及编写shell脚本的两种执行方式.我们知道在敲命令的时候,有很多快捷键,比如tab键能补全命令,在比如为什么我们直接敲 ll 命令能显示目录的长格式,其实这是b ...

  10. 结合程序崩溃后的core文件分析bug

    引言     在<I/O的效率比较>中,我们在修改图1程序的BUF_SIZE为8388608时,运行程序出现崩溃,如下图1:          图1. 段错误     一般而言,导致程序段 ...