HDU 1312
来源:互联网 发布:云计算案例 编辑:程序博客网 时间:2024/04/28 14:39
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12972 Accepted Submission(s): 8019
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
/**这是一个比较基础的搜索题,然而却用了我很多时间,费了很大的功夫才调试对,刚学搜索,用的还不熟练,这个题相当于一笔完成的那种趣味数学题,深搜就可以了*/#include<stdio.h>#include<string.h>int count = 0;int map[25][25];///地图int dir[4][2] = {{0,-1},{1,0},{0,1},{-1,0}};///定义四个方向int visited[25][25];///用于标记走过的点 初始化为0,走过的标志为1,int w, h, si, sj;///宽度,和高度,以及起点void dfs(int x, int y){ visited[x][y] = 1; for(int i = 0; i < 4; i++) { int new_x = x + dir[i][0]; int new_y = y + dir[i][1]; /**其实不需要复杂的剪枝,刚开始的时候,写了控制边界和特殊点的控制条件 以至于看起来很麻烦,后来一想,其实完全没有必要,满足条件的进行判断,不满足的 不判断就好,地图初始化一下就可以了,超出边界的坑定不满足条件了*/ if(map[new_x][new_y] == '.' && !visited[new_x][new_y]) { count++;///记录走过的点的个数 visited[new_x][new_y] = 1;///标记走过的点 dfs(new_x,new_y);///对新节点继续进行搜索 } }}int main(){ char temp; while(~scanf("%d%d",&w,&h)&&(w+h)) { memset(map,'\0',sizeof(map)); scanf("%c",&temp); for(int i = 1; i <= h; i++) { for(int j = 1; j <= w; j++) { scanf("%c",&map[i][j]); if(map[i][j] == '@') { si = i; sj = j; } } scanf("%c",&temp); } count = 0; memset(visited,0,sizeof(visited));///对标记数组初始化 dfs(si,sj); printf("%d\n",count+1); } return 0;}
0 0
- hdu 1312
- hdu 1312
- hdu 1312
- hdu 1312
- hdu 1312
- hdu-1312
- hdu 1312
- HDU 1312
- hdu 1312
- hdu 1312
- hdu 1312
- hdu 1312
- hdu-1312
- HDU 1312
- HDU(1312)
- HDU 1312
- HDU-1312
- hdu 1312
- Java IO学习【19】Java中字符编码解码小知识
- BC round 49 A Untitled
- Android Studio 1.2使用过程问题总结
- B - 皇马
- 超越系统权限----Android黑名单电话拦截
- HDU 1312
- 背包九讲——完全背包
- UML读书笔记——02组件依赖
- 使用Hadoop Streaming
- iOS键盘下落的方式
- stretchableImageWithLeftCapWidth 的使用
- sublime text 3 快捷键大全以及配置编译环境
- 猫猫学iOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现
- [LeetCode]231.Power of Two