主要题意是:大海之间有岛,有的岛之间有桥,问你岛的个数,桥的个数,以及没有桥联通岛的个数,其中最后一次输入的没有回车,不注意的话最后一次会被吞,第二,桥的两端的标记是“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的更多相关文章

  1. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

  2. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  3. 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, ...

  4. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  5. Problem J: 求个最大值

    Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web ...

  6. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. JMS和消息驱动Bean(MDB)

    一.说明 本示例使用的ActiveMQ作为消息中间件,服务器为Glassfish,使用JMS发送消息,在MDB接收到消息之后做打印输出. 二.ActiveMQ安装配置 1.安装console war包 ...

  2. Android 改变窗口标题栏的布局

    Android改变窗口标题栏的布局  第一种方式 --在XML文件里面引入配置文件作为标题. 第二种方式  --动态的代码加入进去. 第三种方式(网上的): 一. 重点 一般应用的Title都是建立应 ...

  3. Http的操作(不传递参数)

    ttpResponse  httpResponse = null;       HttpEntity  httpEntity = null; HttpGet httpGet = new HttpGet ...

  4. typedef 总结

    其实在正儿八经学C语言的时候typedef用的不是很多,记得书上对它的介绍只是一笔带过.的确它的用法是很简单,但这不代表在使用的过程中不会出错,今天来个彻底的总结. 作用:用来建立新的数据类型名.(注 ...

  5. typeof 使用介绍

    JS中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据. typeof 可以用来检测给定变量的数据类型,可能的返回值:1. 'undefined' --- 这个值未定义: 2. 'boole ...

  6. 用css实现列表菜单的效果

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Union - Find 、 Adjacency list 、 Topological sorting Template

    Find Function Optimization: After Path compression: int find(int x){ return root[x] == x ? x : (root ...

  8. SignalR系列教程:服务器广播与主动数据推送

    本篇是本系列入门篇的最后一遍,由于工作关系,接触SignalR的时间不是很多.等下次有空的话我会写一个利用“SignalR”开发一个在线聊天室的系列博文.近期的话我更偏向于更新框架设计相关的文章,到时 ...

  9. 杭电OJ——1007 Quoit Design(最近点对问题)

    Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...

  10. codility上的问题(26) Hydrogenium 2013

    题目,挺无聊的.一个裸的最短路.n个点,你住在0,要去买东西,每个点有一个关门时间,问能最早买到食物的时间.有两点注意 (1)有重边 (2) 原图是dicrect连接...但不是有向边,被这个误导了. ...