codeforce 6B dfs
来源:互联网 发布:php 上传文件进度 编辑:程序博客网 时间:2024/05/17 08:41
http://codeforces.com/problemset/problem/6/B
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length.
The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell.
The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100) — the length and the width of the office-room, and c character — the President's desk colour. The following n lines contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.
Print the only number — the amount of President's deputies.
3 4 RG.B..RR.TTT.
2
3 3 Z....H...Z
0
求与总统的办公桌挨着的办公桌的数目;
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;char a[105][105];int flag[105][105];int flag1[105];int sum,n,m;char c;int dx[4]= {1,-1,0,0};int dy[4]= {0,0,-1,1};void dfs(int x,int y){ for(int i=0; i<4; i++) { int xx=dx[i]+x; int yy=dy[i]+y; if(a[xx][yy]>='A'&&a[xx][yy]<='Z'&&xx>=0&&yy>=0&&xx<n&&yy<m&&flag[xx][yy]==0) { //printf("&&\n"); flag[xx][yy]=1; if(a[xx][yy]==c) dfs(xx,yy); else if(flag1[a[xx][yy]-'A']==0) { flag1[a[xx][yy]-'A']=1; sum++; } } }}int main(){ int x,y; while(cin >> n >> m>>c) { for(int i=0; i<n; i++) cin >>a[i]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(a[i][j]==c) { x=i,y=j; break; } } sum=0; memset(flag1,0,sizeof(flag1)); memset(flag,0,sizeof(flag)); dfs(x,y); printf("%d\n",sum); } return 0;}
- codeforce 6B dfs
- B. Sagheer, the Hausmeister(codeforce 417 div2 B, dfs)
- codeforce B. Preparing Olympiad (DFS+回溯)/(暴力状压)
- codeforce New Problem(dfs)
- *【codeforce】782C 【dfs】
- DFS&&codeforce 598d
- Codeforce # B Sequence Formatting
- codeforce 298 B Sail
- 【codeforce】B. Flag Day
- codeforce B. Road Construction
- CodeForce 126B
- codeforce #247 B(div2)
- codeforce 7B
- codeforce 9B
- codeforce 13B
- codeforce 15B
- codeforce 17 B
- codeforce 18B
- iOS—CABasicAnimation的基本使用方法
- POJ 3258 River Hopscotch
- MINA2.0用户手册中文版--第一章 MINA2.0入门
- NSTimer用法小结
- 11
- codeforce 6B dfs
- 12
- WebService概述和CXF入门小程序
- 516原版金蟾捕鱼,李逵劈鱼,大闹天宫,蛇形亿年,保卫钓鱼岛捕鱼,网狐6603笑傲江湖免费下载
- 11g bbed的安装
- JS的DOM操作
- Java中Lock的使用
- 13
- 仿射变换 affine transformation