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 ...
随机推荐
- mysql查询锁表及解锁
SHOW PROCESSLIST; KILL ; 锁表网上解释: 这牵涉到mysql的事务,简单通俗的话,就这样给你解释有一个任务序列控制sql语句的执行,第一次有select的语句查询表a,mysq ...
- C#基本功之委托和事件
定义:委托是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用. 在实例化委托时,你可以将其实例与任何具有兼容签名和返回类型的方法相关联 目的:方法声明和方法实现的分离,使得程序更容易扩展 一 ...
- spring+struts2+hibernate整合
web.xml需要配置 <context-param> <param-name>contextConfigLocation</param-name> <par ...
- 数据库连接(Oracle/sqlServer增、删、改、查)
一.把数据库包导入项目中,并且Build path(oracle例如ojdbc6.jar;sqlServer例如sqljdbc.jar) 二.开始操作 1.普通连接: String className ...
- Python待分析的模块
fcntl 文件控制模块 http://docs.python.org/2.7/library/fcntl.html#module-fcntl struct 二进制文本处理模块 http://docs ...
- PHP制作API接口
需要单一入口文件,可以使用autoload来加载 <?php //获取get参数 $m = !empty($_GET['m'])?$_GET['m']:'Index'; $a = !empty( ...
- 2)C语言的基本知识(C自考学习)
字符集 在C语言程序中允许出现的所有基本字符的组合称为C语言的字符集.C语言的字符集就是ASCII字符集.主要包含一下几类: 1)大小写英文字母A~Z,a~z(52个) 2)数字0-9(10个) 3) ...
- shopxx------list列表回显修改尝试
需求:在商品列表展示页面增加一列 一.修改模板 1.列表页面对应的freemarker模板位置 /shopxx/WebContent/WEB-INF/template/admin/product/li ...
- Shell脚本数据备份
- react入门到进阶(一)
一.何为react Facebook在F8会议上首次提出这个概念,一套全新的框架就此诞生. React 不是一个完整的 MVC.MVVM 框架,其只负责 View 层 React 跟 Web Comp ...