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(1) * 4;
using namespace std;
char str[100][100];
bool visit1[100][100];
bool visit2[100][100];
int cnt ;
int len;
int bridge;
int dr[] = {1,-1,0,0};
int dc[] = {0,0,-1,1};
void dfs1(int r,int c){
    visit1[r][c] = 1;
    for(int i = 0;i < 4;i++){
        int xx = r + dr[i];
        int yy = c + dc[i];
        if(xx >= 0 && yy >= 0 && 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] = 1;
    for(int i = 0;i < 4;i++){
        int xx = r + dr[i];
        int yy = c + dc[i];
        if(xx >= 0 && yy >= 0 && 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 = 0;
                visit2[xx][yy] = 1;
                bridge++;
                while(1){
                    j++;
                    int tt1 = xx + j * dr[i];
                    int tt2 = yy + j *  dc[i];
                    if(tt1 < 0 || tt2 < 0 || tt1 >= cnt || tt2 >= len)
                        break;
                    visit2[tt1][tt2] = 1;
                    if(str[tt1][tt2] == ‘X‘){
                        dfs2(tt1,tt2);
                        break;
                    }
                }
            }
        }
    }
}
int main()
{
    //freopen("input.in","r",stdin);
    //freopen("output.in","w",stdout);
    cnt = 0;
    int cas = 1;
    memset(str,0,sizeof(str));
    while(fgets(str[0],sizeof(str[0]),stdin) != NULL){
        if(cas != 1)
            cout << endl;
        len = strlen(str[0]) - 1;
        while((fgets(str[++cnt],sizeof(str[0]),stdin) )!= NULL){
            if(str[cnt][0] == 10){
                break;
            }
        }
        bridge = 0;
        int bus = 0;
        int island = 0;
        memset(visit1,0,sizeof(visit1));
        memset(visit2,0,sizeof(visit2));
        for(int i = 0;i <= cnt;i++){
            for(int j = 0;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 = 0;
        memset(str,0,sizeof(str));
    }
    return 0;
}

时间: 2024-12-13 18:35:18

Problem J: Island Buses的相关文章

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 secondsMemory limit: 256 mebibytesN programmers from M teams are waiting at the terminal of airport. There are two shuttles at the exitof terminal, each shuttle

Problem J: 求个最大值

Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web Board] Description 定义MaxValue类,用于求一系列非零整数的最大值.其中: 1. 数据成员elements用于存储所有输入的非零整数. 2. void append(int)用于向elements中添加一个新数据. 3. int getMax()用于求出elements中的

山科SDUST OJ Problem J :连分数

Problem J: 连分数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2723  Solved: 801[Submit][Status][Web Board] Description 一个高为n的连分数定义为 . 给出2个数,一个用p/q的方式表达,另一个用高度为n的连分数来表示,请你判断他们是否相等. Input 输入有多组,每组包含两部分用来表示两种形式的分数:第一部分是p和q(1 ≤ q ≤ p ≤ 10^18),表示分数p/q:然后是一

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

#define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web Board ProblemSet Standing Status Statistics Problem J: 动物爱好者 Problem J: 动物爱好者 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 509  Solved: 376[Submit][Status

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

Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The travel agency “Four Russians” is offering the new service for their clients. Unlike other agencies that only suggest one-way

华农oj Problem J: 幻化【贪心/抽屉原理】

Problem J: 幻化 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 18 Solved: 3 [Submit][Status][Web Board] Description 遇见你是我一世的春暖花开, 从此清风明月浩瀚星海. 不论结局,很高兴认识你. 她给了他一个长度为n的整数序列a[],他还给了她另外一个长度为n的整数序列b[],现在他想通过每次交换a[i],a[j]使序列a变成b,但是每次交换的代价是|j-i|. 请问最少的代价是多少呢?

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/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 717 Problem Description Sudoku i

Problem J. Journey with Pigs

Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪的重量也生序排列此时价值最大 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include&l

UESTC_Islands 2015 UESTC Training for Data Structures&lt;Problem J&gt;

J - Islands Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a re