Problem J: Island Buses
主要题意是:大海之间有岛,有的岛之间有桥,问你岛的个数,桥的个数,以及没有桥联通岛的个数,其中最后一次输入的没有回车,不注意的话最后一次会被吞,第二,桥的两端的标记是“X”(X也代表陆地),“X”的四周都可以有“B”形成的桥,一开始没写好,后来根据“X”标记所有的桥只能走一次然后标记……总之,虽然是水题,写出来还是蛮开心的……
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cctype> const double Pi = atan() * ;
using namespace std;
char str[][];
bool visit1[][];
bool visit2[][];
int cnt ;
int len;
int bridge;
int dr[] = {,-,,};
int dc[] = {,,-,};
void dfs1(int r,int c){
visit1[r][c] = ;
for(int i = ;i < ;i++){
int xx = r + dr[i];
int yy = c + dc[i];
if(xx >= && yy >= && xx < cnt && yy < len){
if(!visit1[xx][yy] && (str[xx][yy] == '#' || str[xx][yy] == 'X' )){
dfs1(xx,yy);
}
}
}
}
void dfs2(int r,int c){
visit2[r][c] = ;
for(int i = ;i < ;i++){
int xx = r + dr[i];
int yy = c + dc[i];
if(xx >= && yy >= && xx < cnt && yy < len){
if(!visit2[xx][yy] && (str[xx][yy] == '#' || str[xx][yy] == 'X')){
dfs2(xx,yy);
}
else if(str[xx][yy] == 'B' && str[r][c] == 'X' && !visit2[xx][yy]){
int j = ;
visit2[xx][yy] = ;
bridge++;
while(){
j++;
int tt1 = xx + j * dr[i];
int tt2 = yy + j * dc[i];
if(tt1 < || tt2 < || tt1 >= cnt || tt2 >= len)
break;
visit2[tt1][tt2] = ;
if(str[tt1][tt2] == 'X'){
dfs2(tt1,tt2);
break;
}
}
}
}
}
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.in","w",stdout);
cnt = ;
int cas = ;
memset(str,,sizeof(str));
while(fgets(str[],sizeof(str[]),stdin) != NULL){
if(cas != )
cout << endl;
len = strlen(str[]) - ;
while((fgets(str[++cnt],sizeof(str[]),stdin) )!= NULL){
if(str[cnt][] == ){
break;
}
}
bridge = ;
int bus = ;
int island = ;
memset(visit1,,sizeof(visit1));
memset(visit2,,sizeof(visit2));
for(int i = ;i <= cnt;i++){
for(int j = ;j < len;j++){
if( (str[i][j] == '#' || str[i][j] == 'X') && !visit1[i][j]){
island++;
dfs1(i,j);
}
if( (str[i][j] == '#' || str[i][j] == 'X')&& !visit2[i][j]){
bus++;
dfs2(i,j);
}
}
}
cout << "Map " << cas++ << endl;
cout << "islands: " << island << endl;
cout << "bridges: " << bridge << endl;
cout << "buses needed: " << bus << endl;
cnt = ;
memset(str,,sizeof(str));
}
return ;
}
Problem J: Island Buses的更多相关文章
- 实验12:Problem J: 动物爱好者
#define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- Problem J: 求个最大值
Problem J: 求个最大值 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 871 Solved: 663[Submit][Status][Web ...
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题
Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
随机推荐
- c#语法与c++ 及 java语法的对比分析
早期开发的时候一直用c/c++,后来主要用的是java.最近需要用下c#. 熟悉了下c#,发现c#语言在对c/c++基础上做了很多简化,同时参考了很多java的语法习惯,本来在语法上c/c++就有很多 ...
- php 解析url 和parse_url使用
通过url进行传值,是php中一个传值的重要手段.所以我们要经常对url里面所带的参数进行解析,如果我们知道了url传递参数名称,例如 /index.php?name=tank&sex=1#t ...
- 基于html5 localStorage , web SQL, websocket的简单聊天程序
new function() { var ws = null; var connected = false; var serverUrl; var connectionStatus; var send ...
- cocos2d-实现读取.plist文件(使用数组CCArray)
学习札记之cocos2d-x2.1.1实现读取.plist文件(使用数组CCArray) <?xml version="1.0" encoding="UTF-8&q ...
- GitHub 使用教程图文详解
大纲: 一.前言 二.GitHub简介 三.注册GitHub账号 四.配置GitHub 五.使用GitHub 六.参与GitHub中其它开源项目 七.总结 注,GitHub官网:https://git ...
- 2015.7.17( NOI2015 day1 )
今天早起做了NOI2015网络同步赛.... 最近NOI是越来越向NOIP靠拢了....但是我还是不会做..... 第一题:程序自动分析 先离散化一下..然后最多就剩20w个数 , 不等于就存起来.. ...
- Perl概述
@home = ("couch", "chair", "table", "stove"); 数组 数组.一个数组是多个标 ...
- haroxy hdr
ACL derivatives :ACL的衍生物 hdr([<name>[,<occ>]]) : exact string match 字符串精确匹配 hdr_beg([< ...
- C#基础 大盘点
类型转换 tryParse 字符和整形转换 转译符: /' 单引号 /" 双引号 // 反斜杠 /0 空字符 /a 感叹号( ...
- javascript每日一练(四)——DOM二
一.DOM的创建,插入,删除 createElement(标签名) appendChild(节点) insertBefore(节点,原有节点) removeChild(节点) <!doctype ...