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 ...
随机推荐
- jq商品展示图放大镜 and 原生js和html5写的放大镜效果 ~~效果不错
<!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8&qu ...
- PrintDocument组件打印
运行效果: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
- Rabbit.Rpc
.NET轻量级RPC框架:Rabbit.Rpc 最近准备写一个.NET的管理平台应用在公司,由于存在大量的Client => Server,Server => Client的请求需求在加上 ...
- 你知道hover、active这四个伪类为什么要按顺序写吗
刨根问底,你知道:hover等4个伪类为什么要按顺序排列吗 引言 :link,:visited,:hover,:active这4个伪类大家都不陌生,4个伪类要按照LvHa这个爱恨原则来排(外国友人起的 ...
- 国产CPU走到十字路口:谁来取代英特尔芯片?(少写了一个OpenPower)
国内的几支CPU研发力量各自选择的指令体系都有自己的优点和问题,选择其中的哪一支都会有对应的成本和风险.最终谁能担大任,且拭目以待. 文 | 瞭望智库特约科技观察员 王强 用上内置国产CPU的个人电脑 ...
- qt执行cmd命令
源地址:http://blog.csdn.net/hn307165411/article/details/6858614 运行 route.ipconfig 肯定没问题 Copy code QProc ...
- 列表标题栏添加CheckBox(自定义HanderView的时候实现)
前段时间项目上的要求,要实现一个列表(见下图1).类似网页上的列表,可以通过选中标题栏的复选框,实现全选或者全不选的功能.但是看了很久,都没看到Qt哪个方法可以实现在标题栏添加控件. 图1 要实现这样 ...
- UTL_RAW
The UTL_RAW package provides SQL functions for manipulating RAW data types. 该包的功能其实可以用来加密: SELECT ...
- java调用163邮箱发送邮件
1:注册一个163邮箱,http://mail.163.com 调用发送邮件代码,查询smtp.163.com,作为发送邮件的服务器ip,类似的邮箱服务器应该也可以. MailSenderInfo m ...
- Flex中怎么给表格中的滚动栏定位
1.问题背景 假设表格中的字段过多,会出现滚动栏,在将滚动栏滚到一定的位置时,又一次刷新表格.滚动栏会回到原处,原来查看的字段还得继续滚动,才干查看到. 2.实现实例 <? xml versio ...