USACO Section 2.1 The Castle
/*
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的更多相关文章
- USACO Section 2.1 The Castle 解题报告
题目 题目描述 有一个城堡,城堡中有若干个房间,房间与房间之间用墙来进行分隔.现在我们需要统计这个城堡有多少个房间,并且还要找出最大的房间的面积是多少(一个单元格就代表一个单元面积).城堡的主人现在想 ...
- [USACO Section 2.1]城堡 The Castle (搜索)
题目链接 Solution 比较恶心的搜索,思路很简单,直接广搜找联通块即可. 但是细节很多,要注意的地方很多.所以直接看代码吧... Code #include<bits/stdc++.h&g ...
- 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&& ...
- 【USACO 2.1】The Castle
/* TASK: castle LANG: C++ SOLVE: 深搜,注意每个方向对应值.枚举去掉的墙,然后再dfs,注意墙要复原,并且dfs里要判断是否超出边界. */ #include<c ...
- USACO Section 3.3: Riding the Fences
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
- USACO Section 3.3 Camlot(BFS)
BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...
- [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...
- 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 ...
- USACO Section 1.3 Prime Cryptarithm 解题报告
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
随机推荐
- C# Post和Get请求
Get请求: /// <summary> /// 调用ToxyzAPI /// </summary> /// <param name="requetid&quo ...
- 统一addEventListener与attachEvent中this指向问题
1.this指向问题 使用addEventListener注册的事件,事件处理函数中 this指向目标元素: 使用attachEvent注册的事件,事件处理函数中 this指向window对象 要想将 ...
- display:none,float小秘密
一个元素不管是块元素还是行内元素 在添加了 display:none 之后,就变成了不可见的块元素,可以给他添加长度和高度 在float之后内联元素也会隐性成为 inline-block ...
- [DeeplearningAI笔记]ML strategy_2_4端到端学习
机器学习策略-端到端学习 End-to-end deeplearning 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.9 什么是端到端学习-What is End-to-end dee ...
- java中表示二进制、八进制、十进制、十六进制
1.进制 进制是一种记数方式 ,可以用有限的数字符号代表所有的数值.由特定的数值组成. 2.进制的表现形式 二进制: 由0和1两个数字组成. 八进制: 由0-7数字组成,为了区分与其他进制的数字区别, ...
- JDBC连接数据库的几种方法
一. 最古老的方法(通过 Driver 接口直接连接数据库) 首先创建一个 Driver 实现类的对象 Driver dirver = new com.mysql.jdbc.Driver(); 准备连 ...
- 大话git中的撤销操作
下面以现实场景作为情境. 基础知识,理解git中的几个区域 本地代码已经add,未commit 修改本地工作目录中的readme.md,添加文字"第一次修改" 然后查看下状态 ➜ ...
- Linux网络中接收 "二进制" 流的那些事 --- 就recv的返回值和strlen库函数进行对话
1. 前言 很多朋友在做网络编程开发的时候可能都遇到这样的问题,在进行接收二进制流的数据的时候,使用strlen库函数来得到 二进制数据长度的时候并不准确.为什么呢??首先,使用strlen进行 ...
- mysql数据库事务详细剖析
在写之前交代一下背景吧! 做开发也好久了,没怎么整理过知识,现在剖析一下自己对数据库事务的认识,以前用sqlserver,现在转java后又用mysql.oracle.我这块就主要解释一下mysql数 ...
- Grafana+Prometheus系统监控之webhook
概述 Webhook是一个API概念,并且变得越来越流行.我们能用事件描述的事物越多,webhook的作用范围也就越大.Webhook作为一个轻量的事件处理应用,正变得越来越有用. 准确的说webho ...