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 ...
随机推荐
- php 取小数
- Ural 1197 - Lonesome Knight
The statement of this problem is very simple: you are to determine how many squares of the chessboar ...
- printk
printk的日志级别定义如下(在linux/kernel.h中): #define KERN_EMERG "<0>"/*紧急事件消息,系统崩溃之前提示,表示系统不可用 ...
- java的new BufferedReader(new InputStreamReader(System.in))
流 JAVA /IO 基本小结 通过一行常见的代码讨论:new BufferedReader(new InputStreamReader(System.in)) /*** *** 看到这篇文章挺好的, ...
- [置顶] 浏览器模式和标准对于javascript的影响
今天在编写代码的时候遇到了一个莫名其妙的问题,请看下面 <html> <head> <title> Test </title> <!--<m ...
- 鹅厂揭秘——高端大气的App电量測试
怎样评价我们开发出来的应用是耗电还是不耗电,怎样測试?这就是我们今天讨论的主题--电量測试,一个在移动应用中新出现的測试类型. 作者简单介绍 watermark/2/text/aHR0cDovL2Js ...
- NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them
NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them,没有网格法线,打开UI Root上 UIPanel组建上的 Nor ...
- Linux fstab 参数详解
[root@qs-wg-db1 /]# cat /etc/fstab LABEL=/ / ext3 defaults ...
- [转载] iOS开发分辨率那点事
1 iOS设备的分辨率 iOS设备,目前最主要的有3种(Apple TV等不在此讨论),按分辨率分为两类 iPhone/iPod Touch 普屏分辨率 320像素 x 480像素 Retina ...
- 01-Foundation简介、NSObject、copy、NSString
目录: 一.Foundation常用类 二.Foundation简介 三.NSObject 四.NSString 回到顶部 一.Foundation常用类 1 NSObject.NSString.NS ...