1504061021-hd-Red and Black
来源:互联网 发布:音乐品味分高低知乎 编辑:程序博客网 时间:2024/05/23 14:14
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11542 Accepted Submission(s): 7185
Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9....#......#..............................#@...#.#..#.11 9.#..........#.#######..#.#.....#..#.#.###.#..#.#..@#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###...@...###.###..#.#....#.#..0 0
Sample Output
4559613
解题思路
这是一道最基础的深搜,也是朋友教给我的,以前都是不懂。
深搜就是从一个点向上下左右四个方向拓展,然后再以拓展的点为基础向上下左右拓展判断。
所以需要两个数组,bianx[4]和biany[4]来存储x和y的对应变化,一定要注意x、y的变话要对照。
然后还需要判断地点有意义,即在范围之内1<=x<=n&&1<=y<=m
解题代码
#include<cstdio>#include<cstring>#include<iostream>//using namespace std;//C++要加这两个头文件 char map[22][22];int bx[5]={0,1,0,-1};int by[5]={1,0,-1,0};int sum;int n,m;bool judge(int a,int b){if(a<1||a>m||b<1||b>n) return false;else{if(map[a][b]!='#') return true;else return false;}}void dfs(int a,int b){int i;int nowa,nowb;sum++;map[a][b]='#';for(i=0;i<4;i++){//对定点进行上下左右四种变化 nowa=a+bx[i];nowb=b+by[i];if(judge(nowa,nowb)) dfs(nowa,nowb);//直接递归调用就好 else continue;}}int main(){//int n,m;int i,j,k;int stax,stay;while(scanf("%d%d",&n,&m),n+m){memset(map,0,sizeof(map));for(i=1;i<=m;i++) for(j=1;j<=n;j++) { cin>>map[i][j]; if(map[i][j]=='@') { stax=i; stay=j; } }sum=0;dfs(stax,stay);cout<<sum<<endl;//若输出需要换行,则加上<<endl }return 0;}
0 0
- 1504061021-hd-Red and Black
- Red and Black hd 1312
- Red and Black
- red and black
- HDUOJ1312 Red and Black
- Red and Black
- red and black
- Red and Black
- HDOJ Red and Black
- HDU1312( Red and Black )
- Red and Black
- hdu1312 Red and Black
- 深搜:Red and Black
- 1038 Red and Black
- HDU1312 Red and Black
- HDU Red and Black
- Red and Black
- HDU1312:Red and Black
- Cocos2d-js3.3 模态对话框的实现
- 初试牛刀——显示图像
- C.Talented Chef
- 两种改变matlab默认工作目录的方法
- TCP/IP入门(1) --链路层
- 1504061021-hd-Red and Black
- 博弈题集 [转自 夏天的风 ]
- GIT简明教程
- hdu 1576 A/B
- HeadFirst Python——在windows下学习Python
- Paint和canvas的用法
- hdu1224 Free DIY Tour(动态规划)
- 专业代呼/代呼公司/专业代呼公司/代呼电话/哪里有代呼/找代呼qq793255660
- SIT-系统集成测试