HDU:1312 Red and Black
来源:互联网 发布:多益网络待遇五险一金 编辑:程序博客网 时间:2024/06/05 02:48
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17076 Accepted Submission(s): 10383
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.
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)
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题目大意:就是给出一个M*N规模的矩阵,@符号为起始位置,以起始位置开始,符号'.'是可以行走的区域,符号#是不能行走的区域,且在每个地方只向上下左右四个方向行走。题目要求的是从@开始走所能到达的全部方块,如第一组测试数据。左下角,右下角。和右上角的点都是被包围了的,不可能走到那个地方,再加上有#的地方不能走,不能到达的地方有9个,总共有6*9,54个方格,不能到达的有九个,所以有四十五个是可以到达的,所以第一组测试数据所对应的答案是45.解题思路:这道题使用的是广搜广搜都可以做,深搜就相当于油田问题。广搜:即从起始位置开始进行广搜,先让起始位置进队,然后让他出队搜它四周的四个方向,按照从当前所在位置的正上方开始按逆时针方向搜索,如果发现这个位置是可以行走的,且这个位置的坐标没有越界,那么我们将这个位置标记为#,代表这个位置已经走过了,并让这个位置进队,让累加的sum+1,代表可达到的区域加1,然后循环这个过程,最终结果是所有可到达的区域都会被赋值为#,此时若队列中还有元素,则会继续循环,队中元素依次出队,当队列为空时,返回sum的值即为答案。题目代码:#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>#include<stdlib.h>#include<string.h>#include<queue> //上方全为头文件using namespace std;typedef struct //结构体,代表坐标{int x; //横坐标int y; //纵坐标}position;int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //四个方向int line,row,sum; //line代表列数,row代表排数,sum为可到达的方块的个数char cmap[21][21]; //定义一个规模为21*21的矩阵,因为题目说明列数和行数都不会超过20position temp; //临时存储起始位置坐标的结构体变量int bfs() //用广搜{int i; //循环变量iqueue<position>Q; //定义一个结构体类型的队列,名字为Qposition cur,next; //定义两个结构体变量,cur代表当前位置,next代表下一个位置cmap[temp.x][temp.y]='#'; //先将起始位置标记,代表走过了Q.push(temp); //标记过后让起始位置入队while(1) //永循环{if(Q.empty()) //如果队列为空,就结束循环,代表所有的能走的位置都被统计了break;cur=Q.front(); //当前位置为队首元素代表的位置Q.pop(); //让队首元素出队for(i=0;i<4;i++) //对上下左右四个方向进行搜索{next.x=cur.x+dir[i][0]; //下一位置的横坐标next.y=cur.y+dir[i][1]; //下一位置的纵坐标if(cmap[next.x][next.y]=='.'&&next.x>=0&&next.x<row&&next.y>=0&&next.y<line) //如果这个位置可以行走,且这个位置没有越界{cmap[next.x][next.y]='#'; //先将这个位置标记为#,代表已走过sum++; //让可走的方块数+1Q.push(next); //让这个位置入队}}}return sum; //最后返回sum值即为答案}int main(){int i,j,ans; //循环变量i,j,ans用于存放答案while(~scanf("%d%d",&line,&row)) //多实例测试,输入列,行(这个题目有一个陷阱,就是先输入的是列数,后输入的是行数{if(line==0&&row==0) //行数列数为0,结束循环break;sum=1; //之所以将sum初始化为1是因为起始位置也算可达到的区域for(i=0;i<row;i++) //for循环输入字符for(j=0;j<line;j++){scanf(" %c",&cmap[i][j]);if(cmap[i][j]=='@') //如果发现当前输入的字符为@则将这个字符的位置记录下来,为起始位置{temp.x=i;temp.y=j;}}ans=bfs();printf("%d\n",ans); //输出答案}return 0;}
0 0
- Red and Black hdu 1312
- HDU 1312 Red and Black
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- Hdu 1312 - Red and Black
- hdu-1312-Red and Black
- hdu 1312Red and Black
- hdu 1312 Red and Black
- hdu - 1312 - Red and Black
- hdu 1312 Red and Black
- hdu 1312 Red and Black
- HDU-1312(red and black)
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- hdu 1312 Red and Black
- hdu 1312 Red and Black
- 初学安卓路之初识Notification(通知)
- Android对数据库的基本操作
- [C++]Swap 整数交换;指针
- [Pa2013]Karty 解题报告
- leetcode 29. Divide Two Integers
- HDU:1312 Red and Black
- Eclipse使用Maven搭建Web项目
- Linux安装Docker
- Android之TypedValue.applyDimension()方法的使用
- linux---TOP命令下参数说明
- WebView获取网页
- 在循环过程中删除List的元素
- python基础-区分数组、矩阵
- 单元测试——基于JUnit